首页 > ACM题库 > HDU-杭电 > HDU 3195-Interesting Numbers-递推-[解题报告]HOJ
2014
03-06

HDU 3195-Interesting Numbers-递推-[解题报告]HOJ

Interesting Numbers

问题描述 :

  To play with the numbers is always interesting. Here is just one! Look at the number: 9376. We notice that: 9376 * 9376 = 87909376 with the last four digits the same as the number itself!
  Here comes the problem. What the i-th number of this kinds of the number? Suppose that 0 is the first one.

输入:

There are exactly 26 cases. Process to the end of file
Each case begin with N (1 <= N <= 26) in one line.

输出:

There are exactly 26 cases. Process to the end of file
Each case begin with N (1 <= N <= 26) in one line.

样例输入:

1
2
3

样例输出:

0
1
5

#include<stdio.h>   
#include<string.h>   
#include<stdlib.h>   
#include<algorithm>   
#include<math.h>  
using namespace std;   


long long num[27];
void cal()
{
    int front=1,tail;
    long long wei=1;
    num[0]=0;
    while(1)
    {
        int k=front;
        tail=0;
        for(int i=1;i<10;i++)
        {
            for(int j=0;j<k;j++)
            {
                long long b=num[j]+i*wei,c=0;
                for(long long m=wei;m>0;m/=10)
                {
                    long long d=(b/m-(b/(10*m))*10)*m,e=b%(10*wei/m);
                    c+=(d*e);
                }
                c%=(wei*10);
                if(c==b) 
                {
                    num[front++]=b;
                    if(front>25) return;
                }            
            }
        }
        wei*=10;
    }
}
int main()
{
    cal();
    int n;
    while(scanf("%d",&n)==1)
    {
        printf("%I64d\n",num[n-1]);
    }
    return 0;
}

参考:http://blog.csdn.net/u010536683/article/details/9121271