"BFS" hdu 1104 remainder
Title Link: Hdu 1104 remainder
Very good search topic, but there are a few key issues to be aware of.
- Shortest path, decisive bfs+queue
- Path storage problem, only wanted to store the results of each step in the queue (int) Q, and later found that the path can not be recorded, select the way to store nodes and string to save the path, queue (node) q, open a temporary node nodes p, Each time the operation is updated its path string+ ' op ', the final output must be the full path!!
- but the key is to take the mold!!!!!
discuss district has a very good explanation: as followed
n After some columns of the operation, it can be very large, so overflow problem, need to consider full.
because the last comparison is whether the two numbers are equal to the k, so the value after the k is stored in the queue ,
All kinds of articles talk about ((n op1 m)%k op2 m)%k is not equal to (n op1 m op2 m)%k
for OP operations +-* is established, but% participates when the result is unequal. For example:
Remember n = 2, M = 8, K =3.
then ((n * m)%k% m)%k = 1
and (n * m% m)%k = 0.
Word,% does not meet the linear congruence, all the process of%k calculation results encountered%m%k is not correct!!!!
"Key" solution has two, or is not in the process of any operation, but the data can easily overflow, in time to open a long long (tried to cross WA, 6 times the multiplication will explode)
There is also a method best, each step with% (k*m) to replace%k (specific proof of your mathematical basis O (∩_∩) o), so that the results can be controlled in a certain range to avoid overflow, but also a good solution to%m%k nonlinear congruence, double benefit! ~
Reference Code
/*author:hacker_vision*/#include <bits/stdc++.h>#define CLR (k,v) memset (k,v,sizeof (k) )typedef Long Longllusing namespace STD;Const int_max=1e6+Ten;intn,m,k,res,deep,km;BOOLVis[_max];structnode{ll num;stringstr;}; queue<node>Q;voidBFS () {node q; Q.num=n; Q.str=""; Q.push (q); vis[(n%k+k)%k]=true; while(! Q.empty ()) {Q=q.front (); Q.pop ();if((q.num%k+k)%k==res) {cout<<q.str.size () <<endl;//cout<<q.num<<endl; cout<<q.str<<endl;return; } node p;p.num=q.num+1; for(inti =0; I <4; + + i) {//Traverse child node if(i==0) {p.num= (q.num+m)%km; p.str=q.str+' + '; }Else if(i==1) {p.num= (q.num-m)%km; p.str=q.str+'-'; }Else if(i==2) {p.num= (q.num*m)%km; p.str=q.str+' * '; }Else{p.num= (q.num%m+m)%m)%km; p.str=q.str+'% '; }if(!vis[(p.num%k+k)%k]) {//Operation Limited, the nodes visited do not have to queueQ.push (P); vis[(p.num%k+k)%k]=true; } } }puts("0");}intMain () {//Freopen ("Input.txt", "R", stdin); while(Cin>>n>>k>>m, (n| | m| | k) {res= (n+1)%k+k)%k;//computer% and the theoretical mod discrepancies, whether it is not a negative all such treatment, omitted to judge theCLR (Vis,0); Km=k*m;//%km avoids data overflow and can meet% of linear congruence while(! Q.empty ()) Q.pop ();//library function can not write a q.clear () it!!! /(ㄒoㄒ)/~~BFS (); }return 0;}
- Bold
Ctrl + B
- Italic Body
Ctrl + I
- Reference
Ctrl + Q
- Insert Link
Ctrl + L
- Inserting code
Ctrl + K
- Insert Picture
Ctrl + G
- Promote title
Ctrl + H
- Ordered list
Ctrl + O
- Unordered list
Ctrl + U
- Line
Ctrl + R
- Revoke
Ctrl + Z
- Redo
Ctrl + Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"BFS" hdu 1104 remainder