HDU - 4394Digital Square

Given an integer N,you should come up with the minimum nonnegativeinteger M.M meets the follow condition: M 2%10 x=N (x=0,1,2,3....)

Input

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 <=10 9), indicating the given number.

Output

For each case output the answer if it exists, otherwise print “None”.

Sample Input

3
3
21
25

Sample Output

None
11
5
#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;
}

猜你喜欢

转载自blog.csdn.net/weixin_41988545/article/details/81698846