Eddy ' s Shuffle problem
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5504 Accepted Submission (s): 3597
Problem Description
Eddy is a acmer, he not only like to do ACM problem, but also for the cards have certain research, he was bored when the study found that if he has 2N cards, numbered 1,2,3..n,n+1,.. 2n. This is also the order of the original cards. The sequence of cards can be changed to n+1,1,n+2,2,n+3,3,n+4,4..2n,n by one shuffle. It can be proved that for any natural number n, the first order can be re-obtained after M shuffle. Programming for a natural number less than 100000 n, the value of M is calculated. Input per line an integer n output with the corresponding M sample Input201 sample Output202 See this problem I, the first feeling is cumbersome, the second feeling is, haha, my cycle can be useful. So I simulated the number of these numbers go home, and note the number of shuffle. However, this practice is timed out. How can the ACM question be so simple, I am careless. The arrangement of this problem is actually hidden mystery, each time the number in the X-position moved to the position of X*2 mod (2*n+1). and moving P-Times is the formula that executes P times. So as long as X*2^p mod (2*n+1) equals the original number, p is the number of shuffle.
#include <iostream>using namespacestd;intMain () {intn,p; while(cin>>N) {intx=1; for(p=1;; p++) {x= (x*2)%(2*n+1); if(x==1) Break; } cout<<p<<Endl; }}
Hangzhou Electric ACM 1210 Eddy ' s shuffle problem