Eddy's 洗牌问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3281 Accepted Submission(s): 2197
Problem Description
Eddy是个ACMer,他不仅喜欢做ACM题,而且对于纸牌也有一定的研究,他在无聊时研究发现,如果他有2N张牌,编号为1,2,3..n,n+1,..2n。这也是最初的牌的顺序。通过一次洗牌可以把牌的序列变为n+1,1,n+2,2,n+3,3,n+4,4..2n,n。那么可以证明,对于任意自然数N,都可以在经过M次洗牌后第一次重新得到初始的顺序。编程对于小于100000的自然数N,求出M的值。
Input
每行一个整数N
Output
输出与之对应的M
Sample Input
20 1
Sample Output
20 2
Author
Eddy
Source
Recommend
不多说了,代码如下:
#include<iostream>
#include<cstdio>
#include<string>
#include<stack>
#include<functional>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 200001;
int f[MAXN];
int main()
{
int n, m;
int i;
freopen("in.txt", "r", stdin);
while (cin >> n){
for (i = 1; i <= n; i++){
f[i] = 2 * i;
f[n+i] = 2 * i - 1;
}
int x = 1;
int num = 0;
while (x != (n + 1)&& x != 2 * n){
x = f[x];
num++;
}
if(x==(n+1))
cout<<num+1<<endl;
else
cout<<2*num<<endl;
}
return 0;
}