hdu 4259 Double Dealing(循环节+最小公倍数)



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 题目大意:n张不同的卡片,k个玩家,卡片轮流发给1~k个人,每个人将得到的卡片按先后顺序从上向下叠放,第二轮将1~k个人的卡片依次按从下往上顺序依次取出排成一列, 然后重复操左...问几次操作后序列第一次变为初序列。 例如有10张牌,3个玩家,初始序列 1 2 3 4 5 6 7 8 9 10 第一次发放 person1 p2 p3 第二次发放 p1 p2 p3 三次 p1 p2 p3 四次 p1 p2 p3 1 2 3 10 7 4 3 2 1 4 7 10 4 5 6 1 8 5 10 9 8 3 6 9 7 8 9 2 9 6 7 6 5 2 5 8 10 3 4 1 第四次后依次取出得序列 1 2 3 4 5 6 7 8 9 10,故输出4。 分析: 可以预处理当前处序列中第x个,发放一次后下一次的位置next[x]。 那么就可以推算出每个x位置再次回到x位置所需步骤数,很显然,求出最小公倍数即可。 如果对于每个点都转一圈求步骤数求一次显然比较赘余,复杂度大约O(n^2),在此题中会超时。在x回到x位置的过程中到达过的位置都和x位置有一样的回转 周期(为什么?)那么每个点只需访问一次,复杂度降为O(n)。 对于当前第x位置,他在如上图所示的矩阵中(x轴方向从上往下,y轴方向从左向右)x坐标dx=ceil(1.0*x/k),y坐标dy=c%k(if(c%k==0)dy=k),即下一次 的发放后坐标。而对于(x,y)点,他下一次在序列中的序号是 前y列个数-x+1 。那么对于一个x位置,先转化为坐标位置,再计算出下一次的线性位置,成功 转换。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int n,k;
int next[900],person[900],persons[900];
char vis[900];
LL gcd(LL a,LL b)
{
    LL c;
    while(c=a%b)
    {
        a=b;
        b=c;
    } return b;
}
/*  数据量有点大,写个挂跑起来更爽一点
bool scan()  
{
    n=k=0;
    char c;
    while((c=getchar())!=' ')n=n*10+c-'0';
    while((c=getchar())!='\n')k=k*10+c-'0';
    return n==0 && k==0;
}  */
int main()
{
    int i,j,m;
    while(scanf("%d%d",&n,&k)==2 && (n||k))
    {
        int ave=n/k;
        int ys=n%k;
        int p=0;
        persons[0]=0;
        for(i=1;i<=k;i++)
            person[i]=ave+(i<=ys?1:0),          
            persons[i]=persons[i-1]+person[i];  //前i个人牌数
        for(i=1;i<=n;i++)
        {
            int r=i%k?i/k+1:i/k;      // 行数
            int c=i%k; if(c==0) c=k;  // 列数
            next[i]=persons[c]-r+1;   //下次位置
        }
        memset(vis,0,sizeof(vis));
        LL ans=1;
        for(i=1;i<=n;i++)
           {
               if(vis[i]) continue;
               int tm=i;
               LL cnt=0;
               while(!vis[tm])
               {
                   cnt++;
                   vis[tm]=1;
                   tm=next[tm];
               }
               if(cnt>1) ans=ans/gcd(ans,cnt)*cnt;
           }
        printf("%I64d\n",ans);
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值