Hangzhou Electric ACM 1210 Eddy ' s shuffle problem

Source: Internet
Author: User
Tags shuffle

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

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.