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 易做图(__int64 a, __int64 b) { return b?易做图(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/易做图(lcmnum, ans)*ans; } printf("%I64d\n", lcmnum); } }
补充:软件开发 , C++ ,