POJ 1721 CARDS (置换群)

来源:互联网 发布:知乎童谣事件始末 编辑:程序博客网 时间:2024/05/21 22:37


CARDS
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1920 Accepted: 985

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

题目大意:

第i个位置的牌是a[i],一次交换后第i个位置的牌变成a[a[i]]。序列所有位置经过一次交换为一次交换,

已知交换m次之后的序列,求原先序列

思路:之前做的置换群都是换成有序的,那样很简单,就看当前位置是不是枚举的i,然后lcm就行了,这个题指定了某一个序列,只能枚举循环节了。。。找出循环节k后,原序列通过s%k到达指定序列,在一个长k的区间,指定序列肯定通过k-s%k到达原序列。。从中间点到终点用a,那么从起点到中间点肯定是长度-a啊。。。


【考虑一般情况。因为原序列为1~N个不同的数,则序列能构成至少一个置换群,则经过多次变换后肯定

会出现循环,现在我们求出置换的循环节,已知新的序列,和进行交换的次数,则再进行res - s % res

次交换即可得到原序列】

【置换群是个封闭的运算,最多n!个置换,然后如果p是生成元,那么最后一个置换p^(n!),所以可以直接暴力做,因为这个平方运算每次是p的2次幂,然后最多暴力跑lg(n!)次,所以直接暴力跑,找到循环节cnt,m%=cnt,然后m=cnt-m,把q跑m次就得到p了】


#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int maxn = 1e3 + 5;int temp[maxn], cur[maxn], pre[maxn], a[maxn], n, s;int calc(){    int res = 0;    while(1)  //枚举指定序列到指定序列的循环节    {        int flag = 0;        for(int i = 1; i <= n; i++)            pre[i] = cur[i];        for(int i = 1; i <= n; i++)            cur[i] = pre[pre[i]];        for(int i = 1; i <= n; i++)            if(cur[i] != a[i])            {                flag = 1;                break;            }        res++;        if(!flag) break;    }    return res;}int main(){    while(~scanf("%d%d", &n, &s))    {        for(int i = 1; i <= n; i++)            scanf("%d", &a[i]), cur[i] = a[i];        int k = calc();        int cnt = k - s%k;        for(int i = 1; i <= n; i++)            cur[i] = a[i], pre[i] = a[i];        while(cnt--)  //剩下的模拟就好了        {            for(int i = 1;i <= n; i++)                cur[i] = pre[pre[i]];            for(int i = 1; i <= n; i++)                pre[i] = cur[i];        }        for(int i = 1; i <= n; i++)            printf("%d\n", cur[i]);    }    return 0;}



0 0
原创粉丝点击