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 ai+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 ith 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).
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).
Sample Input
7 4
6
3
1
2
4
7
5
Sample Output
4
7
5
6
1
2
3
题目大意:
给你一个置换p(p只有一个环),然后每次都对自己做平方运算,就是变成p^2,p^4这样,然后给你一个置换q,它是开头p做了m次这样的运算得到的,就是q=p^(2^m),求p
题解:
首先题目有个条件,置换的长度n为奇数,也就是$gcd(n,2)=1$。(英文题目就是烦)那么就相当于题目保证$gcd(n,2^m)=1$。
根据置换的性质,置换p无论平方多少次,这个环都不会分裂。
因为数据比较小,我们考虑先找到环,然后倒退即可。
1 //Never forget why you start 2 #include<iostream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<cmath> 7 #include<algorithm> 8 using namespace std; 9 int n,a[1005],tmp[1005],m,cnt,ans[1005],pos; 10 int suan(){ 11 int ans=1; 12 for(int i=1;i<=n-1;i++){ 13 ans=(ans*2)%n; 14 if(ans==1)return i; 15 } 16 } 17 int q_pow(int a,int b){ 18 int ans=1;a%=n; 19 while(b){ 20 if(b&1)ans=ans*a%n; 21 a=a*a%n; 22 b>>=1; 23 } 24 return ans; 25 } 26 int main(){ 27 int i,j,k; 28 while(scanf("%d%d",&n,&m)!=EOF){ 29 for(i=1;i<=n;i++)scanf("%d",&a[i]); 30 int round=suan(); 31 tmp[0]=1; 32 cnt=0;k=1; 33 while(a[k]!=1){ 34 tmp[++cnt]=a[k]; 35 k=a[k]; 36 } 37 m=m%round; 38 m=round-m; 39 m=q_pow(2,m); 40 ans[0]=tmp[0]; 41 pos=0; 42 for(i=1;i<n;i++) 43 ans[i]=tmp[pos=(pos+m)%n]; 44 for(i=1;i<=n;i++) 45 a[ans[i-1]]=ans[i%n]; 46 for(i=1;i<=n;i++) 47 printf("%d\n",a[i]); 48 } 49 return 0; 50 }
原文地址:https://www.cnblogs.com/huangdalaofighting/p/8343202.html