POJ 1721 CARDS(置换+循环节)

来源:互联网 发布:avena 36一盒粉末淘宝 编辑:程序博客网 时间:2024/06/14 23:51
CARDS
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2008 Accepted: 1034

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 46312475

Sample Output

4756123

Source

CEOI 1998

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


题意:给你一个长度为n且已经置换s次的序列,让你求原序列。置换的规则为a[i]=a[a[i]]。

题解:题目贼难翻译,但是题目不难,我们可以直接求循环节长度,然后再置换len-s%len次就能转换成原序列。

#include<set>      #include<map>         #include<stack>                #include<queue>                #include<vector>        #include<string>     #include<time.h>    #include<math.h>                #include<stdio.h>                #include<iostream>                #include<string.h>                #include<stdlib.h>        #include<algorithm>       #include<functional>        using namespace std;                #define ll long long          #define inf 1000000000         #define mod 1000000007             #define maxn  1505    #define lowbit(x) (x&-x)                #define eps 1e-9 int a[maxn],b[maxn],c[maxn],n;void work(){int i;for(i=1;i<=n;i++)c[i]=b[b[i]];for(i=1;i<=n;i++)b[i]=c[i];}bool check(){int i;for(i=1;i<=n;i++)if(b[i]!=a[i])return 0;return 1;}int main(void){int s,i,j,k,len;while(scanf("%d%d",&n,&s)!=EOF){for(i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i];for(len=1;;len++){work();if(check())break;}len=len-s%len;for(i=1;i<=len;i++)work();for(i=1;i<=n;i++)printf("%d\n",b[i]);}return 0;}


原创粉丝点击