题目链接 : HDU3195
Interesting Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 84 Accepted Submission(s): 41
Problem Description
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.
Here comes the problem. What the i-th number of this kinds of the number? Suppose that 0 is the first one.
Input
There are exactly 26 cases. Process to the end of file
Each case begin with N (1 <= N <= 26) in one line.
Each case begin with N (1 <= N <= 26) in one line.
Output
Output the N-th number each in one line. It is promised that the number would be fit the 64-bit integer.
Sample Input
1 2 3
Sample Output
0 1 5
Author
ZSTU
Source
Recommend
lcy
一个interesting number应该有如下性质
假设他是一个n位 那么他的后n-1,n-2,n-3,....,1为一定单独也是interesting number
所以就可以通过低位向高位递推 如果找到一个m位的interesting number
那么枚举第m+1位上的数 来找是否存在interesting number
如果不存在就结束。
#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;
}