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
先用最终序列模拟,求出该平方洗牌法的循环长度cnt
然后再变换最终序列(cnt-m%cnt)次,得到初始序列
这只是一种很好理解的方法
实际上,本题有几个条件:
1.只有1个循环,这保证了置换n次必定循环,求cnt转化为求解2cnt ≡ 1(mod n)
2.n为奇数,以上方程必定有解
所以不需要模拟,直接枚举求解(实在无聊打个BSGS也可以,还不用拓展)
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cmath>
  6. using namespace std;
  7. int n,m,c[],a[],b[];
  8. int get_round()
  9. {int cnt=,i,flag;
  10. while ()
  11. {
  12. for (i=;i<=n;i++)
  13. c[i]=b[b[i]];
  14. flag=;
  15. cnt++;
  16. for (i=;i<=n;i++)
  17. if (c[i]!=a[i])
  18. {
  19. flag=;
  20. break;
  21. }
  22. if (flag) return cnt;
  23. for (i=;i<=n;i++)
  24. b[i]=c[i];
  25. }
  26. }
  27. int main()
  28. {int i,cnt;
  29. cin>>n>>m;
  30. for (i=;i<=n;i++)
  31. {
  32. scanf("%d",&a[i]);
  33. b[i]=a[i];
  34. c[i]=a[i];
  35. }
  36. cnt=get_round();
  37. m%=cnt;
  38. m=cnt-m;
  39. while (m--)
  40. {
  41. for (i=;i<=n;i++)
  42. b[i]=a[a[i]];
  43. for (i=;i<=n;i++)
  44. a[i]=b[i];
  45. }
  46. for (i=;i<=n;i++)
  47. printf("%d\n",a[i]);
  48. }

POJ 1721 CARDS的更多相关文章

  1. POJ 1721 CARDS(置换群)

    [题目链接] http://poj.org/problem?id=1721 [题目大意] 给出a[i]=a[a[i]]变换s次后的序列,求原序列 [题解] 置换存在循环节,因此我们先求出循环节长度,置 ...

  2. poj 1721 CARDS (置换群)

    题意:给你一个数列,第i号位置的数位a[i],现在将数列进行交换,交换规则为a[i]=a[a[i]]:已知交换s次之后的序列,求原先序列 思路:置换的问题必然存在一个循环节,使一个数列交换n次回到原来 ...

  3. POJ 1721

    好像不需要用到开方什么的... 可以知道,一副牌即是一个循环,那么,由于GCD(L,K)=1,所以一次洗牌后,亦是一个循环.其实,K次洗牌等于是T^(2^K)了.既然是循环,必定有周期.那么,周期是多 ...

  4. poj 1511-- Invitation Cards (dijkstra+优先队列)

    刚开始想复杂了,一直做不出来,,,其实就是两遍dijkstra+优先队列(其实就是板子题,只要能有个好的板子,剩下的都不是事),做出来感觉好简单...... 题意:有n个车站和n个志愿者,早上每个志愿 ...

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

    我觉得他整理的有一些乱,我都改成插入代码了,看的顺眼一些 转载自http://blog.csdn.net/juststeps/article/details/8772755 下面的都是原文: 最短路径 ...

  6. 1.1.1最短路(Floyd、Dijstra、BellmanFord)

    转载自hr_whisper大佬的博客 [ 一.Dijkstra 比较详细的迪杰斯特拉算法讲解传送门 Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算 ...

  7. 最短路算法详解(Dijkstra/SPFA/Floyd)

    新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...

  8. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

  9. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

随机推荐

  1. 【备忘】Idea的那些事

    说到Java的IDE,似乎eclipse和Idea是目前的主流.然而,OO的课程组却一直在推荐使用eclipse,于是很多人就这样错过了Idea这样强大的IDE工具.本文将会对于Idea和Idea的一 ...

  2. Beta冲刺 第六天

    Beta冲刺 第六天 1. 昨天的困难 1.对于设计模式的应用不熟悉,所以在应用上出现了很大的困难. 2.SSH中数据库的管理是用HQL语句实现的,所以在多表查询时出现了很大的问题. 3.页面结构太凌 ...

  3. io多路复用(一)

    sever端 1 import socket sk1 = socket.socket() sk1.bind(('127.0.0.1',8001,)) sk1.listen() sk2 = socket ...

  4. 第十条:始终要覆盖toString()方法

    Object类提供的toString()方法如下: public String toString() {    return getClass().getName() + "@" ...

  5. mysql命令行大全

      1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  6. JAVA_SE基础——23.类的定义

    黑马程序员入学blog ... java 面向对象的语言 对象:真实存在的唯一的实物. 比如:我家的狗, 类: 实际就是对某种类型事物的共性属性与行为的抽取.  抽象的概念...   比如说:车   ...

  7. DES加密实现的思想及代码

    感谢: http://blog.csdn.net/yxstars/article/details/38424021 上面的日志非常清晰的写出了这个DES加密的过程,主要存在初始IP置换,然后中间存在8 ...

  8. mysql数据库基本操作

    下载地址 http://www.mysql.com/downloads/mysql/ 端口号:3306 用户名:root 密码:自定义 连接到MySQL服务器 >mysql -uroot -pr ...

  9. 新概念英语(1-131)Don't be so sure

    Lesson 131 Don't be so sure! 别那么肯定! Listen to the tape then answer this question. What's the problem ...

  10. OAuth2.0学习(1-2)OAuth2.0的一个企业级应用场景 - 新浪开放平台微博OAuth2.0认证

    http://open.weibo.com/wiki/%E9%A6%96%E9%A1%B5 开发者可以先浏览OAuth2.0的接口文档,熟悉OAuth2.0的接口及参数的含义,然后我们根据应用场景各自 ...