uva 10624 Super Number 回溯

  暴力回溯,用位运算加个奇偶剪枝

  注意取余的效率,因为取余实在太慢了,所以要最少的进行取余运算,所以就是每19位进行次取余,这样1s内就过了

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <stdio.h>
int ans[50];
char nok(int now)
{
    int i,t;
    unsigned long long r;
    for(i=r=t=0;i<now;i++,t++)
    {
        r=r*10+ans[i];
        if(t==18)//19位
        {
            t=0;
            r%=now;
        }
    }
    return r%now;
}
int n,m;
char dfs(int now)
{
    if(now>m)return 1;
    int i=(now==1),f=now&1,t=now-1;
    for(;i<10;i++)
    {
        ans[t]=i;
        if(now>=n&&((!f&&(i&1))||nok(now)))continue;//奇偶
        if(dfs(now+1))return 1;
    }
    return 0;
}
int main()
{
    int T,C=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        printf("Case %d: ",++C);
        if(!dfs(1))printf("-1\n");
        else
        {
            int i;
            for(i=0;i<m;i++)
               printf("%d",ans[i]);
            puts("");
        }
    }
}
时间: 2024-12-21 08:19:12

uva 10624 Super Number 回溯的相关文章

uva 10624 - Super Number

点击打开链接 题目意思:  给定n和m 现在要求找到一个m位数的树使得,对于m的前i位数都是i的倍数,n <= i <= m, 而且这个数的第一位数不能为0,如果找到就输出这个数,否则输出-1 解题思路:    1  目前只懂得用深搜回溯做,只是这一题的数据没有想象中的那么大,所以加上一些剪枝即可水过(数据强的话肯定TLE)                       2  我们知道这个解空间树有m层,每一层的可能取值有10种(第一个除外),那么最大的m = 29 ,那么时间复杂都就是 10^

UVa 10624:Super Number, Rujia Liu的神题

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=112&page=show_problem&problem=1565 原题: Don't you think 162456723 very special? Look at the picture below if you are unable to find its speciality. (a | b mea

UVa 129 Krypton Factor:回溯好题

129 - Krypton Factor Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=65 You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high ment

UVa 10013 Super long sums:简单高精度

10013 - Super long sums Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=954 The Problem The creators of a new programming language D++ have found out that

UVa 10706:Number Sequence

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1647 原题: A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of n

uva 10591 - Happy Number hash

   非常简单的题,易知平方和后不超过800,开个800的数组,预处理,然后直接定址就可以了,不过要注意a和an的区别 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #in

uva 11218 - KTV 简单回溯

   感觉有比回溯更加高效的方法,但是水平不够,只会回溯 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include

uva 10591 - Happy Number

点击打开链接 题目意思:  给定一个数n,然后进行操作,先求出这个数每一位的平方和,然后这个和替代n继续做这个操作,知道当前的n为1 或 n之前以经出现过 ,如果n等于则是happy number ,反之不是. 解题思路:  暴力搜素+状态判重.                      对于这一题一个状态就是这个数字n,由于最大的n是10^9,那么平方和最大不超过1000,我们开一个vis[1000]数组来做为标记数组即可,当遇到n 初始值 或 vis[n] = 1则退出,初始化vis[1]

UVa 10591:Happy Number

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1532 类型: 哈希表 原题: Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let