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 / ...
随机推荐
- 网易云音乐APP分析
网易云音乐-感受音乐的力量 你选择的产品是? 网易云音乐 为什么选择该产品作为分析? 之前用的一直是QQ音乐,但是有一天一个朋友分享了一首网易云上的音乐(顺便分享一下歌名:Drop By Drop) ...
- C语言总结报告
1.当初你是如何做出选择计算机专业的决定的? 经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 当初报考计算机专业,是看到计算机专业在当今社会有良 ...
- 团队作业7——第二次项目冲刺(Beta版本12.06)
项目每个成员的进展.存在问题.接下来两天的安排. 已完成的内容:队员每个人提出对接下来需要做的事情的看法和意见,将需要做的任务更新到了leangoo中进行管理,产品完成了界面优化的设计,测试复现了之前 ...
- java实现同步的两种方式
同步是多线程中的重要概念.同步的使用可以保证在多线程运行的环境中,程序不会产生设计之外的错误结果.同步的实现方式有两种,同步方法和同步块,这两种方式都要用到synchronized关键字. 给一个方法 ...
- 为label或者textView添加placeHolder
Tip:使用textView的代理需要在头文件中加入: <UITextViewDelegate> h文件 @interface FeedbackViewController : UIVie ...
- JAVA_SE基础——54.异常
异常的体系: ----------| Throwable 所以异常或者错误类的超类 --------------|Error 错误 错误一般是用于jvm或者是硬件引发的问题,所以我们一般不会通 ...
- ll的命令后面的字段详解
linux学习 命令ll后字段的解释 分类:linux | 标签: 命令ll后字段的解释 2010-10-25 15:47阅读(4513)评论(0) ls -l 列表信息详解 我们平时用ls -l ...
- linux查看文件内容的常见命令
1.cat命令,显示文件的所有内容,内容过多则显示最后一屏一般用于内容较少文件 2.more命令,分页显示文件的内容一般用于文件内容过多的文件,回车显示下一行,空格显示下一页,q/Q退出 3.head ...
- 记一次将公司网站http换成https
看了博客园将近一年了,一直都只是在被动的看,总觉得应该写点什么,但是又不知道该写点什么.今天正好公司要把网站由http换成https,那我就顺便记录一下吧. 由于之前没有弄过,所以就面向百度编程. 首 ...
- python基础——列表推导式
python基础--列表推导式 1 列表推导式定义 列表推导式能非常简洁的构造一个新列表:只用一条简洁的表达式即可对得到的元素进行转换变形 2 列表推导式语法 基本格式如下: [expr for va ...