Digital Square
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1882 Accepted Submission (s): 741
Problem Descriptiongiven An integer n,you should come up with the minimum
nonnegativeInteger M.M meets the follow Condition:m2%10x=n (x=0,1,2,3 ...)
Inputthe first line have an integer T (t< = +), the number of test cases.
For each case, each line contains one integer n (0<= n <=109), indicating the given number.
Outputfor each case output the answer if it exists, otherwise print "None".
Sample Input332125
Sample OutputNone115
Source multi-university Training Contest 10 parsing: Set m = abc, i.e. m = 100*a+10*b+c, then M2 = 10000*a2 + 1000*2*a*b + 100* (b2+2* A*C) + 10*2*b*c + c2. It is easy to know that the bits of M2 are only related to the last 1 bits of M, M2 10 are only related to the last 2 bits of M, and the hundred M2 are only related to the last 3 bits of M ... We can start from the single-digit search, a bit to meet N. If there is a workable solution, find the smallest solution in this layer and output it, otherwise the output is "None".
1#include <cstdio>2#include <queue>3 using namespacestd;4 5 structnode{6 Long LongValue,place;7 };8 9 voidBFsLong LongN)Ten { One node tmp; ATmp.value =0; -Tmp.place =1; -Queue<node>Q; the Q.push (TMP); - BOOLFindans =false; - Long LongAns =0xFFFFFFFF; - while(!Q.empty ()) { +TMP =Q.front (); - Q.pop (); + node now; ANow.place = tmp.place*Ten; at for(inti =0; i<Ten; ++i) { -Now.value = tmp.value+i*Tmp.place; - if(Now.value*now.value%now.place = = n%now.place) { - if(!Findans) - Q.push (now); - if(Now.value*now.value%now.place = = N && now.value<ans) { inFindans =true; -Ans =Now.value; to } + } - } the } * if(ans = =0xFFFFFFFF) $printf"none\n");Panax Notoginseng Else -printf"%i64d\n", ans); the } + A intMain () the { + intT; -scanf"%d",&t); $ while(t--){ $ Long LongN; -scanf"%i64d",&n); - BFS (n); the } - return 0;Wuyi}
The code above finds a workable solution that needs to be compared to find the optimal solution, we can optimize it, change the queue to Priority_queue, let Priority_queue help us do this work, so that the first feasible solution to find is to satisfy the test instructions optimal solution.
1#include <cstdio>2#include <queue>3 using namespacestd;4 5 structnode{6 Long LongValue,place;7 BOOL operator< (Constnode& b)Const8 {9 returnValue>B.value;Ten } One }; A - voidBFsLong LongN) - { the node tmp; -Tmp.value =0; -Tmp.place =1; -Priority_queue<node>Q; + Q.push (TMP); - while(!Q.empty ()) { +TMP =q.top (); A Q.pop (); at if(Tmp.value*tmp.value%tmp.place = =N) { -printf"%i64d\n", tmp.value); - return ; - } - node now; -Now.place = tmp.place*Ten; in for(inti =0; i<Ten; ++i) { -Now.value = tmp.value+i*Tmp.place; to if(Now.value*now.value%now.place = = n%now.place) { + Q.push (now); - } the } * } $printf"none\n");Panax Notoginseng } - the intMain () + { A intT; thescanf"%d",&t); + while(t--){ - Long LongN; $scanf"%i64d",&n); $ BFS (n); - } - return 0; the}
HDU 4394 Digital Square