1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <Set>7#include <sstream>8 using namespacestd;9 /*int next1 (int n,int k)Ten { One StringStream SS; A ss<< (long Long) k*k; - string S=ss.str (); - if (s.length () >n) s=s.substr (0,n);//result too long, take only the first n bits the int ans; - StringStream SS2 (s); - ss2>>ans; - return ans; + }*/ - intbuf[ -]; + intNEXT2 (intNintk) A { at if(!k)return 0; - Long LongK2= (Long Long) k*K; - intL=0; - while(k2>0){ -buf[l++]=k2%Ten; k2/=Ten; - } in if(n>l) n=L; - intans=0; to for(intI=0; i<n;i++) +ans=ans*Ten+BUF[--L];//re-assemble the former min{n,l} bit - returnans; the } * /*int main () $ {Panax Notoginseng int T; - cin>>t; the While (t--) + { A int n,k; the cin>>n>>k; + set<int>s; - int ans=k; $ While (!s.count (k)) $ { - S.insert (k); - if (K>ans) ans=k; the K=next1 (n,k); - }Wuyi cout<<ans<<endl; the } - return 0; Wu } - */ About intMain () $ { - intT; -Cin>>T; - while(t--) A { + intn,k; theCin>>n>>K; - intans=K; $ intK1=k;intK2=K; the Do{ thek1=next2 (N,K1); theK2=NEXT2 (N,K2);if(K2>ans) ans=K2; theK2=NEXT2 (N,K2);if(K2>ans) ans=K2; -} while(k1!=K2); incout<<ans<<Endl; the } the return 0; About}
Suppose there are two children running on an "infinitely forward" roundabout, while one of the children is twice times faster than the other, then the fast-running child will chase the slow-running child,
Commented out code more time Next1 more than 4 seconds, next2 to 1 seconds.
UVA11549 Computer Puzzles (Floyd algorithm)