POJ 1721 CARDS (replacement)

Source: Internet
Author: User
CARDS
Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:1033   Accepted:544

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 46312475

Sample Output

4756123

Source

CEOI 1998 means that each cycle will return to the original State for a certain number of times.
/*POJ  1721 CARDS*/#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int MAXN=1010;int a[MAXN];int b[MAXN];int c[MAXN];int n,m;int solve(){    int j;    int cnt=0;    while(1)    {        for(int i=1;i<=n;i++)           b[i]=c[c[i]];        cnt++;        for(j=1;j<=n;j++)          if(b[j]!=a[j])            break;        if(j>n)break;        for(int i=1;i<=n;i++)           c[i]=b[i];    }    return cnt;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            c[i]=a[i];            b[i]=a[i];        }        int cnt=solve();        m%=cnt;        m=cnt-m;        while(m--)        {            for(int i=1;i<=n;i++)              b[i]=a[a[i]];            for(int i=1;i<=n;i++)               a[i]=b[i];        }        for(int i=1;i<=n;i++)          printf("%d\n",b[i]);    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.