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 3
第二次
3 2 1 10 9 8 7 6 5 4
第三次
4 7 10 3 6 9 2 5 8 1
第四次
1 2 3 4 5 6 7 8 9 10

在第四次可以回到原来的顺序。

其循环节是:

位置的变化有如下规律:

1->4->3->10->1;  循环节是4

2->7->2;  循环节是2

5->6->9->8->5; 循环节是4

其最大的公约数是4.

故其答案是4.

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define LL long long
using namespace std;
int a[1000], vis[1000];
LL gcd(LL a,LL b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    int i,j,n,k,t;
    while(~scanf("%d%d",&n,&k)){
        LL ans,sum=1;
        if(n==0&&k==0)
            break;
        t=0;
        for(i=0; i<k&&i<n; i++)
            for(j=(n-i-1)/k*k+i; j>=0; j-=k)
                a[t++]=j;

        for(int i=0;i<=n;i++) cout<<a[i]<<endl;

        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++){
            int x=i;
            ans=0;
            while(!vis[x]){
                ans++;
                vis[x]=1;
                x=a[x];
            }
            if(ans)
                sum=sum/gcd(sum,ans)*ans;
        }
        printf("%I64d\n",sum);
    }
}
时间: 2024-10-26 20:43:23

HDU 4259 Double Dealing【简单群置换】的相关文章

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,

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

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-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(

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

HDU 1018 Big Number (简单数学)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25649    Accepted Submission(s): 11635 Problem Description In many applications very large integers numbers are required. Some of these

HDU 1908 Double Queue&lt;Set&gt;

Problem Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of th

hdu 1312 DFS算法简单题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 此题与油田那题很像是练习深搜的好题,题意是从"@"开始,遍历整个图,找到连接的 "."有多少个 但要考虑变化,简单处理下就行了,主要是斜角的不要了,而且判断结束方式也不一样 具体看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32