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也可以,还不用拓展)
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m,c[],a[],b[];
int get_round()
{int cnt=,i,flag;
while ()
{
for (i=;i<=n;i++)
c[i]=b[b[i]];
flag=;
cnt++;
for (i=;i<=n;i++)
if (c[i]!=a[i])
{
flag=;
break;
}
if (flag) return cnt;
for (i=;i<=n;i++)
b[i]=c[i];
}
}
int main()
{int i,cnt;
cin>>n>>m;
for (i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
c[i]=a[i];
}
cnt=get_round();
m%=cnt;
m=cnt-m;
while (m--)
{
for (i=;i<=n;i++)
b[i]=a[a[i]];
for (i=;i<=n;i++)
a[i]=b[i];
}
for (i=;i<=n;i++)
printf("%d\n",a[i]);
}

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. linux分析、诊断及调优必备的“杀器”之二

    先说明下,之所以同类内容分成多篇文章,不是为了凑篇数,而是为了便于自己和大家阅读,下面继续: 7.sar The sar command is used to collect, report, and ...

  2. Java作业-数据库

    本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 在Java中使用数据库要经过以下几个步骤: 1. 注册 JDBC 驱动 Class.forName("com ...

  3. 使用 memoryview 和 struct 查看一个 GIF 图像的首部

    >>> import struct >>> fmt = '<3s3sHH' # ➊ >>> with open('filter.gif', ...

  4. Codeforces 193 D. Two Segments

    http://codeforces.com/contest/193/problem/D 题意: 给一个1~n的排列,在这个排列中选出两段区间,求使选出的元素排序后构成公差为1的等差数列的方案数. 换个 ...

  5. C#系统服务安装

    转载 http://blog.csdn.net/vvhesj/article/details/8349615 1.1创建WindowsService项目 导入需要的引用比如System.configu ...

  6. 《javascript设计模式与开发实践》阅读笔记(14)—— 中介者模式

    中介者模式 数个对象之间的通信全部委托一个中介者完成.适用于对象之间互相引用,关系错综复杂的情况. 什么情况下需要使用中介者模式 对象较多,且对象间会相互引用,当一个对象的某个状态改变时,得通知其他对 ...

  7. react中的DOM操作

    前面的话 某些情况下需要在典型数据流外强制修改子代.要修改的子代可以是 React 组件实例,也可以是 DOM 元素.这时就要用到refs来操作DOM 使用场景 下面是几个适合使用 refs 的情况 ...

  8. awk sed tr替换换行符为逗号,并合并为一行

    在群里看到的.记录以备用.  sed 帮助命令:http://man.linuxde.net/sed 文件里有如下行,我想将每行的回车符替换为逗号,并将所有行合并到一行,用awk或sed怎么写啊TOP ...

  9. C 函数指针与回调函数

    函数指针是指向函数的指针变量. 通常我们说的指针变量是指向一个整型.字符型或数组等变量,而函数指针是指向函数. 函数指针可以像一般函数一样,用于调用函数.传递参数. 函数指针变量的声明: #inclu ...

  10. ( 转 ) CORS 有一次 OPTIONS 请求的原理

    刚接触前端的时候,以为HTTP的Request Method只有GET与POST两种,后来才了解到,原来还有HEAD.PUT.DELETE.OPTIONS-- 目前的工作中,HEAD.PUT.DELE ...