poj 1721 CARDS 【置换群 】【求最小循环节 再由末序列 求 初序列】

CARDS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1516 Accepted: 810

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 a i+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 i th 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
题目意思:给出一个n个数的序列和它已经历的变换次数s,让你求变换前的序列。变换规则:如果i位置是牌j,j位置是牌k,那么i位置就为牌k。
思路:求出循环节t,则我们知道该序列是由前 s % t 个序列变换而来,由于从后推前不好推,那我们继续往后推 可以求出原序列就是当前序列后面第t - s % t个序列。
#include <cstdio>
#include <cstring>
#define MAX 1000+10
using namespace std;
int last[MAX];//最后序列 
int mid[MAX];//中间数组 媒介 
int first[MAX];//起始序列
int main()
{
	int n, s;
	int i, j, k;
	int t;//循环节
	int exist;//判断是否找到循环节 
	while(scanf("%d%d", &n, &s) != EOF)
	{
		for(i = 1; i <= n; i++)
		{
			scanf("%d", &last[i]);
			mid[i] = last[i];
		}
		t = 0;
		/*找循环节*/
		while(1)
		{
			for(i = 1; i <= n; i++)
			first[i] = mid[mid[i]];
			t++;
			exist = 1;
			for(i = 1; i <= n; i++)//判断是否出现相同序列 
			{
				if(first[i] != last[i])//有一个数不同即 不同序列 
				{
					exist = 0;
					break;
				}
			}
			if(exist) break;//找到循环节
			memcpy(mid, first, sizeof(first));//没找到 继续下一洗牌 把first数组复制到mid里面 
	    }
	    t = t - s % t;//求出是当前数列后面第几个数列  前推后比较简单  
		memcpy(mid, last, sizeof(last));//把 末序列复制给mid 
	    while(t--)//t次向后洗牌
	    {
	    	for(i = 1; i <= n; i++) 
	    	first[i] = mid[mid[i]];
	    	memcpy(mid, first, sizeof(first));//继续下一洗牌 把first数组复制到mid里面 
	    }
	    for(i = 1; i <= n; i++)
	    printf("%d\n", first[i]);
	} 
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值