Digital Square
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 830 Accepted Submission(s): 349
Problem Description
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....)
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.
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
Source
Recommend
zhuyuanchen520
题意是给你一个N,让你输出最小的M使得 M^2 % 10^x = N
我们从最低位向高位去搜,如果 M^2 % 10^y = N % 10^y 则将之加入队列。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 50;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x3f3f3f3f;
const int IMIN = 0x80000000;
const double E = 2.718281828;
#define eps 1e-8
#define DEBUG 1
#define mod 100000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
// #pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];
struct Node
{
__int64 m , x;
Node()
{
m = 0;
x = 0;
}
};
__int64 ten[10];
void init()
{
__int64 num = 1;
FOR(i , 0 , 10)
{
ten[i]= num;
num *= 10;
}
}
__int64 getlength(__int64 x)
{
if(x == 0)return 1;
int num = 0;
while(x > 0)
{
x /= 10;
num++;
}
return num;
}
int main()
{
int t;
__int64 n;
cin >> t;
init();
while(t--)
{
scanf("%I64d" , &n);
int len = getlength(n);
queue <Node> q;
q.push(Node());
// outstars
while(!q.empty())
{
Node u = q.front();
// q.pop();
// outstars
if(u.x == len)
{
break;
}
// outstars
FOR(i , 0 , 10)
{
Node v ;
v.m = u.m + ten[u.x] * i;
v.x = u.x + 1;
// outstars
if((v.m * v.m) % ten[v.x] == n % ten[v.x])
{
// outstars
q.push(v);
// outstars
}
}
q.pop();
// outstars
}
// outstars
__int64 ans = INF;
while(!q.empty())
{
Node u = q.front();
q.pop();
if(ans > u.m)ans = u.m;
}
if(ans == INF)printf("None\n");
else printf("%I64d\n" , ans);
}
return 0;
}