hdu acm 4259 Double Dealing (置换群)

Double Dealing

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


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
 

置换群水题,主要是置换映射不好表示
如10 3
  1 2 3 4 5 6 7 8 9 10->
10 7 4 1 8 5 2 9 6 3  .

for (int i = 1;i <= K;i++)
{
 for (int j = (N- i)/K *K + i;j > 0;j -= K)
{ a[num++] = j; }
}

只要知道对于第i列,它拥有的元素个数是(N-i)/K +1,就很好理解了。

下面是完整代码

 

 1 /*
 2 杭电 acm 4259 Double Dealing 
 3                                     简单置换群
 4 */
 5 #include <iostream>
 6 #include <stdio.h>
 7 #include <cstring>
 8 #include <algorithm>
 9 
10 using namespace std;
11 #define maxn 880
12 int a[880],low[880];//a数组判断是否找过   low存置换后的数
13 int gcd(long long a,long long b)
14 {
15     if(b==0)
16         return a;
17     return gcd(b,a%b);
18 }
19 int main()
20 {
21     int n,k;
22     while(scanf("%d %d",&n,&k)!=EOF)
23     {
24         if(n==0||k==0)
25             break;
26         long long ans=1;
27         int i,M=0;
28         memset(a,1,sizeof(a));
29         for(i = 0; i < k && i < n; i++)
30             for(int j=(n-i-1)/k*k+i; j>=0; j-=k)
31                 low[M++] = j;
32         for( i=0; i<n; i++)
33         {
34             if(a[i]==0)
35                 continue;
36             int t=0,x=i;
37             while(a[x])
38             {
39                 a[x]=0;
40                 x=low[x];
41                 t++;
42             }
43             if(t)
44                 ans=(ans/gcd(ans,t))*t;
45         }
46         printf("%I64d\n",ans);
47     }
48     return 0;
49 }

 

 

 

转载于:https://www.cnblogs.com/vivider/p/3681242.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值