POJ 1721 CARDS
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.
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
#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的更多相关文章
- POJ 1721 CARDS(置换群)
[题目链接] http://poj.org/problem?id=1721 [题目大意] 给出a[i]=a[a[i]]变换s次后的序列,求原序列 [题解] 置换存在循环节,因此我们先求出循环节长度,置 ...
- poj 1721 CARDS (置换群)
题意:给你一个数列,第i号位置的数位a[i],现在将数列进行交换,交换规则为a[i]=a[a[i]]:已知交换s次之后的序列,求原先序列 思路:置换的问题必然存在一个循环节,使一个数列交换n次回到原来 ...
- POJ 1721
好像不需要用到开方什么的... 可以知道,一副牌即是一个循环,那么,由于GCD(L,K)=1,所以一次洗牌后,亦是一个循环.其实,K次洗牌等于是T^(2^K)了.既然是循环,必定有周期.那么,周期是多 ...
- poj 1511-- Invitation Cards (dijkstra+优先队列)
刚开始想复杂了,一直做不出来,,,其实就是两遍dijkstra+优先队列(其实就是板子题,只要能有个好的板子,剩下的都不是事),做出来感觉好简单...... 题意:有n个车站和n个志愿者,早上每个志愿 ...
- 觉得一篇讲SPFA还不错的文章
我觉得他整理的有一些乱,我都改成插入代码了,看的顺眼一些 转载自http://blog.csdn.net/juststeps/article/details/8772755 下面的都是原文: 最短路径 ...
- 1.1.1最短路(Floyd、Dijstra、BellmanFord)
转载自hr_whisper大佬的博客 [ 一.Dijkstra 比较详细的迪杰斯特拉算法讲解传送门 Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算 ...
- 最短路算法详解(Dijkstra/SPFA/Floyd)
新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- 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 / ...
随机推荐
- Go语言的数组
在 Go 语言里,数组是一个长度固定的数据类型,用于存储一段具有相同的类型的元素的连续块.数组存储的类型可以是内置类型,如整型或者字符串,也可以是某种结构类型. 1 数组特性 (1)内存是连续分配,C ...
- 2017年秋软工-领跑衫获奖感言&我最感谢的人
啥都不说,先上幅图.获得领跑衫,开心. 一.回忆 这是我第二次来上恩师杨的软件工程,第一次是2016年春,那时候我还是本科三年级的学生.忘了第一次为啥去蹭课,印象中是我的榜样亮哥把我给忽悠过去的?我也 ...
- pjax实例demo(c#,iis)
pjax 百度都是api 也没找到demo 自己写了一个 C#写的 需要iis架设 测试ie10 和 火狐 成功 ie10不要用兼容模式 不然不好使 iis 可以直接架设webDemo1文件夹(源码) ...
- Bate敏捷冲刺每日报告--day5
1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285) Git链接:https://github.com/WHUSE2017/C-team 2 ...
- 201621123031 《Java程序设计》第12周学习总结
作业12-流与文件 1.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 在Java中的java.io包中定义了许多类专门负责处理各种方式的输入与输出.其中,所有输入 ...
- 搭建java环境——使用Sublime Text 3(windows环境)
实现sublime Text 3对Java编译执行 参考网址:http://tieba.baidu.com/p/2609515186 1.1直接在安装路径下找到*\Packages\Java.subl ...
- Linux 下的权限改变与目录配置
Linux 下的权限改变与目录配置 ./代表本目录的意思. (1):用户与用户组, 1:文件所有者,文件被某一用户所有 2:用户组: 对文件给与一个或者多个用户权限配置 3:其它人: (2):l ...
- Node入门教程(3)第二章: Node 安装
Node 安装 官网下载地址: https://nodejs.org/en/download/ 安装方式 windows 下安装 建议直接选择:Windows Installer (.msi)下载进行 ...
- java.lang.String 类源码解读
String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public fin ...
- Python内置函数(14)——bytes
英文文档: class bytes([source[, encoding[, errors]]]) Return a new "bytes" object, which is an ...