[POJ 1721]CARDS

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

题目大意:

给你一个置换p(p只有一个环),然后每次都对自己做平方运算,就是变成p^2,p^4这样,然后给你一个置换q,它是开头p做了m次这样的运算得到的,就是q=p^(2^m),求p

题解:

首先题目有个条件,置换的长度n为奇数,也就是$gcd(n,2)=1$。(英文题目就是烦)那么就相当于题目保证$gcd(n,2^m)=1$。

根据置换的性质,置换p无论平方多少次,这个环都不会分裂。

因为数据比较小,我们考虑先找到环,然后倒退即可。

 1 //Never forget why you start
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 using namespace std;
 9 int n,a[1005],tmp[1005],m,cnt,ans[1005],pos;
10 int suan(){
11   int ans=1;
12   for(int i=1;i<=n-1;i++){
13     ans=(ans*2)%n;
14     if(ans==1)return i;
15   }
16 }
17 int q_pow(int a,int b){
18   int ans=1;a%=n;
19   while(b){
20     if(b&1)ans=ans*a%n;
21     a=a*a%n;
22     b>>=1;
23   }
24   return ans;
25 }
26 int main(){
27   int i,j,k;
28   while(scanf("%d%d",&n,&m)!=EOF){
29     for(i=1;i<=n;i++)scanf("%d",&a[i]);
30     int round=suan();
31     tmp[0]=1;
32     cnt=0;k=1;
33     while(a[k]!=1){
34       tmp[++cnt]=a[k];
35       k=a[k];
36     }
37     m=m%round;
38     m=round-m;
39     m=q_pow(2,m);
40     ans[0]=tmp[0];
41     pos=0;
42     for(i=1;i<n;i++)
43       ans[i]=tmp[pos=(pos+m)%n];
44     for(i=1;i<=n;i++)
45       a[ans[i-1]]=ans[i%n];
46     for(i=1;i<=n;i++)
47       printf("%d\n",a[i]);
48   }
49   return 0;
50 }

原文地址:https://www.cnblogs.com/huangdalaofighting/p/8343202.html

时间: 2024-11-08 08:20:46

[POJ 1721]CARDS的相关文章

poj 1721 CARDS(置换群)

题目链接:poj 1721 CARDS 题意: 看了半天才看懂,就是一次置换为b[i]=a[a[i]],a[i]=b[i]. 现在已经知道了置换了多少次和当前的序列,问你最原来的序列为 题解: 将这个置换的循环次数ans找出来,再做ans-s次就行了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define F(i,a,b) for(int i=a;i<=b;++i) 5 usi

poj 1721 CARDS(置换)

http://poj.org/problem?id=1721 大致题意:原始序列通过洗牌机洗牌s次后变为当前序列,已知当前序列,求原始序列. 在置换群快速幂运算 研究与探讨中最后有详解,有两种解法,一种是求出置换的长度a(即一副牌洗a次后变回原来的位置),现已知原始序列置换s次变为当前序列,那么当前序列再置换a-s次就是原始序列了.求a就是直接模拟每个置换的过程,直到某序列与当前序列相等.另一种是置换的开方,相当于原始序列的2^s幂是当前序列,将当前序列开2^s次方便是原始序列. 第二种方法暂时

POJ 1721

好像不需要用到开方什么的... 可以知道,一副牌即是一个循环,那么,由于GCD(L,K)=1,所以一次洗牌后,亦是一个循环.其实,K次洗牌等于是T^(2^K)了.既然是循环,必定有周期.那么,周期是多少呢?以例子为例:1->4->6->2->7->3->5.其实对于第一个数(从零始)4,总会有先后移了2^a次方而回到原点,此时就是一个周期了.即是求2^a=1(mod n).求出最小的a即可知道周期.s%a=t.那么,即是差a-t个状态就可以回到初始的了. #includ

觉得一篇讲SPFA还不错的文章

我觉得他整理的有一些乱,我都改成插入代码了,看的顺眼一些 转载自http://blog.csdn.net/juststeps/article/details/8772755 下面的都是原文: 最短路径 之 SPFA算法 http://hi.baidu.com/southhill/item/ab26a342590a5aae60d7b967 求最短路径的算法有许多种,除了排序外,恐怕是OI界中解决同一类问题算法最多的了.最熟悉的无疑是Dijkstra,接着是Bellman-Ford,它们都可以求出由

HDU 1535 &amp;&amp; POJ 1511 Invitation Cards (SPFA 模板 + 反向建图)

Invitation Cards HDU: Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) POJ: Time Limit: 8000 MS     Memory Limit: 262144 K       Problem Description In the age of television, not many people attend theater performa

POJ 1511 Invitation Cards

题目来源:http://poj.org/problem?id=1511 题目很长,花了不少时间才理解题意,目的就是为了求出来回两次最小路径(即为本题的差旅费)之和, 第一次从CCS(1)出发到各个点路径最小,SPFA算法没得说,回来时终点是确定的都是CCS(1),相当于把路 径反过来,即把有向图去反方向,又是从1出发到各个点路径最小,再用一个SPFA.注意ans要用long long 不然也WA,这个地方WA了好几次,虽然更改后AC了,但还是不明白,题目明明写了smaller than 1000

HDU 1535 Invitation Cards (POJ 1511)

两次SPFA.求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换,但是这个有1000000个点,矩阵开不了. d1[]为 1~N 的最短路. 将所有边的 邻点 交换. d2[] 为 1~N 的最短路. 所有相加为 所要答案. 忧伤的是用SPFA  "HDU 1535"  AC了,但是POJ 一样的题 "POJ 1511" 就WA了. 然后强迫症犯了,不停的去测试. 题意中找到一句关键话 :Prices are positive integ

POJ 1511 Invitation Cards 【最短路,spfa算法,Dijkstra算法堆优化】

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 25219   Accepted: 8346 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

[2016-04-05][POJ][1511][Invitation Cards]

时间:2016-04-05 12:57:22 星期二 题目编号:[2016-04-05][POJ][1511][Invitation Cards] 题目大意:给定一个有向图,从点1出发,分别到各个站点后,又回到点1,问最少需要多少车费, 分析: 从1跑一次最短路,然后矩阵转置,再跑一次最短路,两次求和 这里不能用邻接矩阵保存,所以改成邻接表,然后矩阵转置的操作变成重新加一次边 遇到的问题:用vector存图超时,改用数组实现 #include <queue> #include <algo