hdu4259 Double Dealing----置换群

Double Dealing

Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 290    Accepted Submission(s): 132


Problem Description
Take a deck of n unique cards. Deal the entire deck out to k players in the usual way: the top card to player 1, the next to player 2, the k th to player k, the k+1 st 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?

Input
There will be multiple test cases in the input. Each case will consist of a single line with two integers, n and k (1≤ n≤800, 1≤ k≤800). The input will end with a line with two 0s.

Output
For 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 3 10 3 52 4 0 0

Sample Output
   
   
   
   
1 4 13

Source
The University of Chicago Invitational Programming Contest 2012

Recommend
liuyiding
 
 
关于洗牌的题其实一般都与置换群有关,比赛的时候直想睡觉,只是闪过了一下置换群的概念,没怎么想。
正好在网络赛之前复习下置换群。
假如n是10,k是3
最开始的序列是1 2 3 4 5 6 7 8 9 10
经过一次变换后10 7 4 1 8 5 2 9 6 3
1->10->3->4-1周期为4 ,所以1 10 3 4 的周期是一样的,走过的位置标记一下
2->7->2周期为2
……
然后求出最小公倍数即可。
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
#define ll __int64
using namespace std;
int n,k;
int a[810][810];
bool f[810];
int num[810];
ll gcd(ll a,ll b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        if(n==0&&k==0) break;
        if(n<k){puts("1"); continue;}
        int oo=1;
        for(int i = 1; i <= k && i <= n; i++)
        for(int j = (n - i) / k * k + i; j > 0; j -= k)
        num[oo++] = j;
        int x;
        ll res=1;
        memset(f,false,sizeof(f));
        for(int i=1;i<=n;i++)
        {
            x=i;
            if(f[i]) continue;
            int count=0;
            while(1)
            {
                f[x]=true;
                x=num[x];
                count++;
                if(x==i) break;
            }
            res=res/gcd(res,count)*count;
        }
        cout<<res<<endl;

    }
}

 

你可能感兴趣的:(hdu4259 Double Dealing----置换群)