[poj1721]置換羣循環節

CARDS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1415   Accepted: 750

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

Source



#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1005
int num[N], array[N][N], cycle[N];
//初始數組,循環hash數組,循環次數
void func(const int &n)
{
    memset(array, 0, sizeof(array));
    memset(cycle, 0, sizeof(cycle));
    for (int i = 0; i < n; i++)
        array[0][i] = num[i];
    int tmp = n;
    int tm = 1;//循環次數
    while (tmp)
    {
        for (int i = 0; i < n; i++)
            if (!cycle[i])//沒有完成計算循環進行hash操作
                array[tm][i] = array[tm - 1][array[tm - 1][i]];
        for (int i = 0; i < n; i++)//完成循環記錄
            if (!cycle[i] && array[tm][i] == array[0][i])
            {
                cycle[i] = tm;
                --tmp;
            }
        ++tm;//循環+1
    }
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int n, k;
    while (scanf("%d%d", &n, &k) + 1)
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &num[i]);
            num[i]--;
        }
        func(n);
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            cnt = max(cycle[i], cnt);
            //這題取最大最小循環節都一樣,目測都是同一個數
        }
        k %= cnt;
        k = cnt - k;
        int b[N];
        while (k--)
        {
            for (int i = 0; i < n; i++)
                b[i] = num[num[i]];
            for (int i = 0; i < n; i++)
                num[i] = b[i];
        }
        for (int i = 0; i < n; i++)
        {
            printf("%d\n", b[i] + 1);
        }
    }

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章