CARDS
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 872 | Accepted: 437 |
Description
Alice and Bob have a set of N cards labelled with numbers 1 ... N (so that no two cards have the same label) and a shuffle machine. We assume that N is an odd integer.
The shuffle machine accepts the set of cards arranged in an arbitrary order and performs the following operation of double shuffle : for all positions i, 1 <= i <= N, if the card at the position i is j and the card at the position j is k, then after the completion of the operation of double shuffle, position i will hold the card k.
Alice and Bob play a game. Alice first writes down all the numbers from 1 to N in some random order: a1, a2, ..., aN. Then she arranges the cards so that the position ai holds the card numbered a i+1, for every 1 <= i <= N-1, while the position aN holds the card numbered a1.
This way, cards are put in some order x1, x2, ..., xN, where xi is the card at the i th position.
Now she sequentially performs S double shuffles using the shuffle machine described above. After that, the cards are arranged in some final order p1, p2, ..., pN which Alice reveals to Bob, together with the number S. Bob's task is to guess the order x1, x2, ..., xN in which Alice originally put the cards just before giving them to the shuffle machine.
The shuffle machine accepts the set of cards arranged in an arbitrary order and performs the following operation of double shuffle : for all positions i, 1 <= i <= N, if the card at the position i is j and the card at the position j is k, then after the completion of the operation of double shuffle, position i will hold the card k.
Alice and Bob play a game. Alice first writes down all the numbers from 1 to N in some random order: a1, a2, ..., aN. Then she arranges the cards so that the position ai holds the card numbered a i+1, for every 1 <= i <= N-1, while the position aN holds the card numbered a1.
This way, cards are put in some order x1, x2, ..., xN, where xi is the card at the i th position.
Now she sequentially performs S double shuffles using the shuffle machine described above. After that, the cards are arranged in some final order p1, p2, ..., pN which Alice reveals to Bob, together with the number S. Bob's task is to guess the order x1, x2, ..., xN in which Alice originally put the cards just before giving them to the shuffle machine.
Input
The first line of the input contains two integers separated by a single blank character : the odd integer N, 1 <= N <= 1000, the number of cards, and the integer S, 1 <= S <= 1000, the number of double shuffle operations.
The following N lines describe the final order of cards after all the double shuffles have been performed such that for each i, 1 <= i <= N, the (i+1) st line of the input file contains pi (the card at the position i after all double shuffles).
The following N lines describe the final order of cards after all the double shuffles have been performed such that for each i, 1 <= i <= N, the (i+1) st line of the input file contains pi (the card at the position i after all double shuffles).
Output
The output should contain N lines which describe the order of cards just before they were given to the shuffle machine.
For each i, 1 <= i <= N, the ith line of the output file should contain xi (the card at the position i before the double shuffles).
For each i, 1 <= i <= N, the ith line of the output file should contain xi (the card at the position i before the double shuffles).
Sample Input
7 4
6
3
1
2
4
7
5
Sample Output
4
7
5
6
1
2
3
本题的算法主要是置换群的开方运算,由于置换群的幂运算我们是知道的
比如对于置换a=T,a'=T^k,且gcd(size(a),k)=1,那么a'[i]=a[i*k%size(a)],这里面0<=i<=size(a)-1
而且置换a的第一个元素即a[0]=1,我们得到的a'的第一个元素a'[0]=1
有上面的公式我们可以得到:如果a',k是已知的,那么a'[i]这个元素在a中的位置就是(i*k%size(a)),这也就是置换群的开方运算了
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream>
2 #include<memory.h>
3 #include<algorithm>
4 using namespace std;
5 #define N 20000
6 int input[N+10];
7 int circle[N+10];
8 int vis[N+10];
9 int ans[N+10];
10 int len[N+10][2];
11 int main()
12 {
13 int size,k;
14 while(cin>>size>>k)
15 {
16 memset(vis,0,sizeof(vis));
17 memset(len,0,sizeof(len));
18 for(int i=1;i<=size;i++)
19 cin>>input[i];
20 circle[1]=1;
21 int sub=1;
22 int cnt=1;
23 while(input[sub]!=1)
24 {
25 int tmp=input[sub];
26 circle[++cnt]=tmp;
27 sub=tmp;
28
29 }
30 int cnter=1;
31 for(int i=1;i<=k;i++)
32 {
33 cnter=2*cnter;
34 if(cnter>size) cnter-=size;//注意这里最好不要用模运算,因为会出现cnter=0的可能性
35 }
36 input[1]=1;
37 sub=1;
38 for(int i=2;i<=size;i++)
39 {
40 sub+=cnter;
41 if(sub>size) sub-=size; //也不用模运算
42 input[sub]=circle[i];
43 }
44 for(int i=1;i<size;i++)
45 ans[input[i]]=input[i+1];
46 ans[input[size]]=1;
47 for(int i=1;i<=size;i++)
48 cout<<ans[i]<<endl;
49 }
50 return 0;
51 }
我的wa代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream>
2
3 using namespace std;
4
5 const int N=1010;
6 int a[N],b[N];
7 int main()
8 {
9 int n,s;
10 while(cin>>n>>s)
11 {
12 for(int i=1;i<=n;i++)
13 cin>>a[i];
14 for(int i=0;i<s;i++)
15 {
16
17 for(int j=1;j<=n;j++)
18 {
19 int pos=j;
20 for(int k=0;k<s;k++)
21 pos=a[pos];
22 b[j]=pos;
23 }
24 for(int j=1;j<=n;j++)
25 {
26 a[j]=b[j];
27 }
28 }
29 for(int j=1;j<=n;j++)
30 cout<<a[j]<<endl;
31 }
32 return 0;
33 }