fudq and Digital
时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交:18 测试通过:2
总提交:18 测试通过:2
描述
The other day, AC Goddness asked fudq for help as she was in trouble in a easy problem.The problem requires to find the minimum nonnegative integer M when given an integer N.M meets this condition:
(M^2)%(10^x)=N (x=0,1,2,3...).
fudq don't want to lose face in front of AC Goddness.So can you help fudq?
输入
The first line has an integer T(T <= 1000), the number of test cases.
For each case, each line contains one integer N(0 <= N <= 1,000,000,000), indicating the given number.
输出
For each case output the answer if it exists, otherwise print “Sorry, I tried.”.
样例输入
3
3
21
25
样例输出
Sorry, I tried.
11
5
提示
M^2 = M*M;
Code:
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #define NN 299999999 int flag;int ll;__int64 wei;int jie; void FF(int len,__int64 M,__int64 t,__int64 N){ int i; while(len--){//len代表位数; //if(flag==1) break; for(i=0;i<=9 ;i++){ if(((M+i*t)*(M+i*t))%(t*10) == (N)%(t*10)){ M=M+i*t;t=t*10; if(M*M%(wei)==N) {if(M<jie)jie=M; flag=1;break;} else { t=t/10;M=M-i*t; // 这里需要注意! 在参数中改变传入值,但M的值不能动。 //否则递归时因为M变化会出错! FF(len,M+i*t,t*10,N); } } } if(i>=10 ){if(flag!=1){flag=0;return;} break;} } } int main() { freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); __int64 i,j,k;__int64 a,t,num,n,m,N,T,M; scanf("%I64d",&T); while(T--){ int len; scanf("%I64d",&N); a=N%10; flag=-2; if(a==2 || a==3 || a==7 || a==8) flag=0; else{ len=0; M=N; while(M){len++;M=M/10;} ll=len; wei=1;for(i=1;i<=ll;i++) wei=wei*10; t=1;M=0; jie=NN; FF(len,M,t,N); } if(flag==1) if(jie>NN) printf("Sorry, I tried.\n"); else printf("%I64d\n",jie); else if(flag==-1) printf("0\n"); else{ if(N==0) printf("0\n"); else printf("Sorry, I tried.\n"); } } return 0; }