[POJ 1721]CARDS

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. 

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 4ios

6dom

3spa

1code

2orm

4blog

7ip

5get

Sample Output

4input

7string

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 }
相關文章
相關標籤/搜索