您的位置:首页 > 其它

Digital Square HDU - 4394 (数论)(DFS)

2017-10-16 00:18 549 查看
Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow condition:

M2mod10x=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<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 50000
#define INF 10000000000
#define mod 19940417
using namespace std;
typedef long long  ll;
ll n;
ll ans;
void dfs(ll cur,ll now)
{
if(cur>n)
return;
ll c=cur*10;
for(int i=0;i<10;i++)
{
ll temp=i*cur+now;
if(temp*temp%c==n%c)
if(temp*temp%c==n)
ans=min(ans,temp);
else
dfs(c,temp);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
if(n==0)
{
printf("0\n");
continue;
}
ans=INF;
dfs(1,0);
if(ans==INF)
printf("None\n");
else
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: