"BFS" hdu 1104 remainder

Source: Internet
Author: User

"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
    • ReferenceCtrl + Q
    • Insert LinkCtrl + L
    • Inserting codeCtrl + K
    • Insert PictureCtrl + G
    • Promote titleCtrl + H
    • Ordered listCtrl + O
    • Unordered listCtrl + U
    • LineCtrl + R
    • RevokeCtrl + Z
    • RedoCtrl + Y

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"BFS" hdu 1104 remainder

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.