2012 Multi-University Training Contest 10
1005 questions
Given N, find the smallest M, and make M ^ 2% 10 ^ x = N.
N <= 10 ^ 9. If no output exists, None is output.
From the low position to the high position, if the current situation meets M ^ 2% 10 ^ y = N % 10 ^ y, continue to the downward search.
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <queue>
# Include <algorithm>
Using namespace std;
Const _ int64 INF = 00000000000ll;
Struct Node
{
_ Int64 m, x;
Node ()
{
M = 0;
X = 0;
}
};
_ Int64 ten [10];
Void init ()
{
_ Int64 num = 1;
For (int I = 0; I <10; ++ I)
{
Ten [I] = num;
Num * = 10;
}
}
Int getLength (_ int64 x)
{
If (x = 0)
{
Return 1;
}
Int len = 0;
While (x> 0)
{
X/= 10;
++ Len;
}
Return len;
}
Int main ()
{
Int caseNumber;
_ Int64 n;
Init ();
Scanf ("% d", & caseNumber );
While (caseNumber --)
{
Scanf ("% I64d", & n );
Int len = getLength (n );
Queue <Node> q;
Q. push (Node ());
While (! Q. empty ())
{
Node u = q. front ();
If (u. x = len)
{
Break;
}
For (int I = 0; I <10; ++ I)
{
Node v;
V. m = u. m + ten [u. x] * I;
V. x = u. x + 1;
If (v. m * v. m) % ten [v. x] = n % ten [v. x])
{
Q. push (v );
}
}
Q. pop ();
}
_ Int64 ans = INF;
While (! Q. empty ())
{
Node node = q. front ();
If (ans> node. m)
{
Ans = node. m;
}
Q. pop ();
}
If (ans = INF)
{
Printf ("None \ n ");
}
Else
{
Printf ("% I64d \ n", ans );
}
}
Return 0;
}