TOJ 4351 HDU 4259 Double Dealing

Double Dealing

时间限制(普通/Java):20000MS/60000MS     运行内存限制:65536KByte
 

描述

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, thekth 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 playerk’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?

输入

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.

输出

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.

样例输入

1 3
10 3
52 4
0 0

样例输出

1
4
13

首先模拟一次 然后求从1 到n 每个数的循环节 所有循环节的最小公倍数就是答案

代码队友提供

 

#include<stdio.h>
#include<string.h>
__int64 gcd(__int64 a, __int64 b) 
{ 
    return b?gcd(b,a%b):a; 
}
int a[1000],f[1000];
int main()
{
	int i, j, n, k, t;
    while(scanf("%d %d", &n, &k),n,k)
    {	
		memset(f, 0, sizeof(f));
        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;
        __int64 ans, lcmnum = 1; 
        for(i = 0; i < n; i++)
        {
            int x = i;
            ans = 0; 
            while(!f[x])
            {
                ans++;
                f[x] = 1; 
                x = a[x];
            }
            if(ans) 
                lcmnum=lcmnum/gcd(lcmnum, ans)*ans;
        }
        printf("%I64d\n", lcmnum);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值