Digital square (hdu4394) Search

Source: Internet
Author: User
Digital Square

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1310 accepted submission (s): 501


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 has an integer T (t <= 1000), 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 The question is very simple. M2 % 10x = n (x =, 2, 3...) won't be too long, Ideas: You can use DFS; (in fact, it is brute force + pruning)


For example, if n is 21, we start from single digit 1 and 1*1 = 1, 9*9 = 81. Their single digit is 1, the numbers 1 and 9 can be used as the single digits of the number we are looking.
Then we can remember what we found and put it in ans.
Next we start to search for the second digit. we search for AB a (where B is a bit that we have remembered ).
AB * AB % 100 = 21; so, is our 2 caused by (B * B/10 + a * B * 2) % 10 ;). If the unknown number is only a, then we will find a and find a again, and we will find C in the same way (if there is C ). In this way, the pruning is completed.
Finally, if the n we are looking for has x digits, then we are looking for M * m %? M = n can only contain x digits at most. So far, it is over.

 

PS: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4394

For details, see the code.

 

# Include <cstdio> # include <iostream> # define INF 0 xfffffff # define ll _ int64using namespace STD; ll T, digit [20], num, N, ANS, R, final; ll min (ll a, LL B) {return A> B? B: A;} void get (LL t) // obtain the number of digits num = n. What are the digits of each digit in digit. {Num = 0; while (t) {digit [++ num] = T % 10; T/= 10 ;}} void solve (ll p, ll W, ll ans) {If (P> num) {final = min (final, ANS); Return ;}for (ll k = 0; k <10; k ++) {If (ANS * ANS/W + R * K % 10) % 10 = digit [p]) // (B * B/10 + a * B * 2) % 10, r = 2 * ans solve (p + 1, w * 10, K * w + ans);} int main () {scanf ("% i64d", & T); While (t --) {scanf ("% i64d", & N); get (N ); if (digit [1] = 2 | digit [1] = 3 | digit [1] = 7 | digit [1] = 8) {puts ("NONE"); continue;} final = inf; For (ll I = 0; I <10; I ++) {if (I * I % 10 = digit [1]) {r = I <1; // R is the remainder 2 * ans solve (, I );}} if (final = inf) puts ("NONE"); else printf ("% i64d \ n", final) ;}return 0 ;}

 

 

 

Of course, you don't have to worry about direct DFS.

It's just a matter of time.

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <queue> 5 # include <cmath> 6 7 8 using namespace STD; 9 typedef _ int64 ll; 10 11 12 13 struct node14 {15 ll num; 16 int Len; // length 17 bool operator <(const node & P) const18 {19 Return p. num <num; 20} 21}; 22 ll N, ans; 23 24 ll kpow (int x) 25 {26 ll KK = 1; 27 for (INT I = 0; I <X; I ++) 28 KK = KK * 10; 29 return KK; 30} 31 bool BFS () 32 {33 priority_queu E <node> q; 34 node p, q; 35 p. num = 0, P. Len = 0; 36 Q. Push (p); 37 while (! Q. empty () 38 {39 p = Q. top (); 40 Q. pop (); 41 ll tmp = kpow (P. len); 42 if (P. num * P. num % TMP = N) 43 {44 ans = P. num; 45 return true; 46} 47 // extended 48 for (INT I = 0; I <10; I ++) 49 {50 Q. len = P. len + 1; 51 Q. num = P. num + I * TMP; 52 If (Q. num * q. num % (TMP * 10) = n % (TMP * 10) 53 Q. push (Q); 54} 55} 56 return false; 57} 58 59 60 int main () 61 {62 int t; 63 scanf ("% d", & T ); 64 While (t --) 65 {66 scanf ("% i64d", & N); 67 int temp = n % 10; 68 if (temp = 2 | temp = 3 | temp = 7 | temp = 8) 69 {70 puts ("NONE"); 71 continue; 72} 73 If (BFS () 74 printf ("% i64d \ n", ANS); 75 else76 puts ("NONE"); 77} 78 return 0; 79}
View code

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.