HDOJ 4259 Double Dealing

Source: Internet
Author: User

HDOJ 4259 Double Dealing


Find the cyclic section of each digit and find the lcm


Double Dealing Time Limit: 50000/20000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1806 Accepted Submission (s): 622


Problem DescriptionTake a deck NUnique cards. Deal the entire deck out KPlayers in the usual way: the top card to player 1, the next to player 2, KTh to player K, K+ 1st to player 1, and so on. Then pick up the cards-place player 1's cards on top, then player 2, and so on, so that player K'S cards are on the bottom. Each player's cards are in reverse order-the last card that they were dealt is on the top, and the first on the bottom.
How many times, including the first, must this process be repeated before the deck is back in its original order?
InputThere will be multiple test cases in the input. Each case will consist of a single line with two integers, NAnd K(1 ≤ N≤ 800, 1 ≤ K≤ 800). The input will end with a line with two 0 s.

OutputFor each test case in the input, print a single integer, indicating the number of deals required to return the deck to its original order. output each integer on its own line, with no extra spaces, and no blank lines between answers. all possible inputs yield answers which will fit in a signed 64-bit integer.
Sample Input
1 310 352 40 0

Sample Output
1413

SourceThe University of Chicago Invitational Programming Contest 2012


#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     using namespace std;int n,m;typedef long long int LL;int next[880],to[880];bool vis[880];LL gcd(LL a,LL b){    if(b==0) return a;    return gcd(b,a%b);}LL lcm(LL a,LL b){    return a/gcd(a,b)*b;}int get_int(){    char ch;    int ret=0;    while(ch=getchar())    {        if(ch>='0'&&ch<='9')        {            ret=ret*10+ch-'0';        }        else break;    }    return ret;}int main(){    while(true)    {        n=get_int();m=get_int();        if(n==0&&m==0) break;        if(n<=m)        {            puts("1"); continue;        }        ///mo ni yi chi        for(int i=1;i<=n;i++)            next[i]=i;        for(int i=1;i<=m;i++)        {            to[i]=n/m;            if(i<=n%m) to[i]++;            to[i]+=to[i-1];        }        for(int i=1;i<=n;i++)        {            next[i]=to[(i-1)%m+1]--;        }        LL ans=1;        memset(vis,false,sizeof(vis));        for(int i=1;i<=n;i++)        {            if(vis[i]) continue;            int t=next[i];            LL temp=1;            while(t!=i)            {                vis[t]=true;                t=next[t];                temp++;            }            ans=lcm(ans,temp);        }        printf("%I64d\n",ans);    }    return 0;}
    
   
  
 



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.