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. python自动发邮件

    from email.header import Header from email.mime.text import MIMEText from email.utils import parsead ...

  2. 小草手把手教你 LabVIEW 串口仪器控制——初识VISA串口

    有些人,学习一样东西时候,喜欢现成的例子.很多人学习一门技术,都喜欢现成的例子开始,比如学单片机的啊,最开始都是修改的例子吧,学语言的也是.最开始都是模仿.这个年头看书上的理论知识太浪费时间了.所以啊 ...

  3. Fluent Interface(流式接口)

    我最初接触这个概念是读自<<模式-工程化实现及扩展>>,另外有Martin fowler大师 所写http://martinfowler.com/bliki/FluentInt ...

  4. 第四篇:用IntelliJ IDEA 搭建基于jersey的RESTful api

    编译器:Intellij IDEA 系统环境: MAC OS 相关技术:Maven.tomcat 7.jdk8 1.创建项目 首先创建一个web Application项目(这里我们打算用maven引 ...

  5. jsMath对象

    Math对象: abs.用来求绝对值. ceil:用来向上取整. floor:用来向下取整. round:用来四舍五入取近似值. sqrt:用来开方. pow:括号内有2位参数.如pow(2,5)表示 ...

  6. 第四章 JavaScript操作DOM对象

    第四章   JavaScript操作DOM对象 一.DOM操作 DOM是Document Object Model的缩写,即文档对象模型,是基于文档编程的一套API接口,1988年,W3C发布了第一级 ...

  7. Linq SelectMany 交叉连接

    class Student1 { public int Score { get; set; } public Student1(int score) { this.Score = score; } } ...

  8. api-gateway实践(05)新网关工作 - 缓存定义

    一.缓存分类 1.服务注册信息 1.1.[GroupCode_VersionCode]对应[Version定义]的缓存                       缓存类型:hash         ...

  9. Docker学习笔记 - Docker的容器

    docker logs  [-f]  [-t]  [--tail]  容器名 -f -t --tail="all" 无参数:返回所有日志 -f 一直跟踪变化并返回 -t 带时间戳返 ...

  10. spring-boot 使用Aop通知打印控制器请求报文和返回报文

    一.简介 开发过程中我们往往需要写许多例如: @GetMapping("/id/get") public Result getById( String id) throws Exc ...