Cardstime limit: 1000 ms memory limit: 10000 ktotal submissions: 1452 accepted: 776 description
Alice and Bob have a set of N cards labeled 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 saved med 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 shoshould 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 shoshould 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
Source
Ceoi 1998
Question:
The card at the I position is a [I]. After an exchange, the card at the I position becomes a [A [I]. All the positions in the sequence are exchanged once,
Know the sequence after M switching, find the original sequence
Thought: I thought about it for a day and never understood what it meant. However, you must note that
Instead of directly exchanging the original sequence.
For example: 4 7 5 6 1 2 3
Perform a transformation of 6 3 1 2 4 7 5
Instead, you cannot directly change the first position of the card to 6 3 1 2 6 3 5
This means that the new sequence type does not change the value of the original sequence during the exchange process on the basis of the original sequence to obtain
New sequence.
Now we consider the relationship between the number of transformations and the original sequence.
Consider 4 7 5 6 1 2 3
Perform a transformation of 6 3 1 2 4 7 5
Perform Two Transformations 7 1 6 3 2 5 4
Perform three Transformations 4 7 5 6 1 2 3
The original sequence encountered a loop during the Third transformation.
Consider the general situation. Because the original order column is 1 ~ If there are n different numbers, then the sequence can constitute at least one replacement group, after multiple transformations
There will be a loop. Now we can find the cyclic section of the replacement, know the new sequence, and the number of exchanges, and then perform res-S % res
The original sequence can be obtained by secondary swapping. // RES is a cyclic section, and s is the number of transformations given
#include <stdio.h>#include <string.h>int Array[1010],ArrayA[1010],ArrayB[1010];void GetNext(int n){ for(int i = 1; i <= n; i++) ArrayB[i] = ArrayA[ArrayA[i]]; for(int i = 1; i <= n; i++) ArrayA[i] = ArrayB[i];}bool IsSame(int n){ for(int i = 1; i <= n; i++) if(ArrayB[i]!=Array[i]) return false; return true;}int main(){ int n,s; while(~scanf("%d%d",&n,&s)) { for(int i = 1; i <= n; i++) { scanf("%d",&Array[i]); ArrayA[i] = Array[i]; } int res; for(res = 1;;res++) { GetNext(n); if(IsSame(n)) break; } int m = res - s % res; int num = 0; while(num < m) { GetNext(n); num++; } for(int i = 1; i <= n; i++) printf("%d\n",ArrayB[i]); } return 0;}
Poj1721_cards [replacement Group]