HDOJ 4259 Double Dealing

找每一位的循环节,求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 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 kth to player k, the 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?

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

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;
}

HDOJ 4259 Double Dealing,布布扣,bubuko.com

时间: 2024-10-15 16:11:47

HDOJ 4259 Double Dealing的相关文章

HDOJ 4259 Double Dealing(置换群)

Double Dealing Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1893    Accepted Submission(s): 672 Problem Description Take a deck of n unique cards. Deal the entire deck out to k players in

HDU 4259 Double Dealing【简单群置换】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4259 题目大意:给出n张卡片以及k个人,现在对卡片进行分堆,然后分发(这样卡片改变了顺序),依次这样问多少次后卡片顺序回到原来一样. 比如给出的10,3. 第一次分堆是这样的 第一人:1,4,7,10 第二人:2,5,8 第三人:3,6,9 其顺序由1 2 3 4 5 6 7 8 9 10 变成了 10 7 4 1 8 5 2 9 6 3 即: 10 3 第一次 10 7 4 1 8 5 2 9 6

HDU 4259 Double Dealing(置换群啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4259 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 kth to player k, the k+1st to player 1,

HDU 4259(Double Dealing-lcm(x1..xn)=lcm(x1,lcm(x2..xn))

Double Dealing Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1924    Accepted Submission(s): 679 Problem Description Take a deck of n unique cards. Deal the entire deck out to k players in

hdu 4529 Double Dealing (置换群)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; __int64 gcd(__int64 a,__int64 b) { if(b==0) return a; return gcd(b,a%b); } int main() { int n,k,i,j,vis[810],m,num[810],x; __int64 res,cot; while(~scanf(

JVMS Specification(3)-The class File Format

Subsections 3 The class File Format 3.1 The ClassFile Structure 3.2 The Internal Form of Names 3.2.1 Binary Class and Interface Names 3.2.2 Unqualified Names 3.3 Descriptors and Signatures 3.3.1 Grammar Notation 3.3.2 Field Descriptors 3.3.3 Method D

HDOJ 1217 Floyed Template

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <cstring> 5 #include<map> 6 using namespace std; 7 8 map<string,int>name; 9 const int INF = 1000000; 10 const int MAXSIZE = 1005; 11 const int

HDOJ 1823 Luck and Love

二维线段树模版题... Luck and Love Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5397    Accepted Submission(s): 1348 Problem Description 世界上上最远的距离不是相隔天涯海角 而是我在你面前 可你却不知道我爱你 ―― 张小娴 前段日子,枫冰叶子给Wiskey做了

HDOJ 4968 Improving the GPA

枚举... Improving the GPA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 158    Accepted Submission(s): 126 Problem Description Xueba: Using the 4-Point Scale, my GPA is 4.0. In fact, the AVER