zoukankan      html  css  js  c++  java
  • 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 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
     

    置换群水题,主要是置换映射不好表示
    如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
    ",ans);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    通过构造函数检查生成对象个数
    动手动脑二
    产生随机数的几种方法
    素数输出
    递归实现回文串
    java的方法重载
    统计单词频率
    四则运算和随机验证码
    微信小程序--家庭记账本开发--04
    微信小程序--家庭记账本开发--03
  • 原文地址:https://www.cnblogs.com/vivider/p/3681242.html
Copyright © 2011-2022 走看看