Given an integer N,you should come up with the minimum
nonnegative integer M.M meets the follow condition: M
2%10
x=N (x=0,1,2,3....)
For each case, each line contains one integer N(0<= N <=10 9), indicating the given number.
3 3 21 25
None 11 5
求一个数m有以下关系成立 m*m%10^x=n
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define Inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll n,ans;
ll Dfs(ll step,ll pre){//setp当前最高位 pre 前一个数字
if(pre>n) return 0;
int i;
for(i=0;i<10;i++){
ll a=i*step+pre;//当前数字
ll b=a*a%(step*10);
ll c=n%(step*10);
if(b==c){
if(b==n) ans=min(ans,a);
else Dfs(step*10,a);
}
}
return 0;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
if(n==0){
printf("0\n");
continue;
}
if(n==1000000000){
printf("None\n");
continue;
}
ans=Inf;
Dfs(1,0);
if(ans!=Inf) printf("%d\n",ans);
else printf("None\n");
}
return 0;
}