poj 1721 CARDS 洗牌机
2015-10-17 10:14
405 查看
CARDS
Description
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 ai+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 ith 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
Sample Output
Source
CEOI 1998
翻译在
中有
要注意这个题目的每一次操作都不是一次置换,要按照它规定的方式操作。
反复操作找出周期circle,题目告诉了你终点状态ed,和操作了s次,要你求起始状态st。
首先s=s%circle。
相当于变换了这么多次。
然后从终点 再变换 circle-s 次,即可得到起点。
很像链表中的一个操作。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1684 | Accepted: 877 |
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 ai+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 ith 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
Source
CEOI 1998
翻译在
算法合集之《置换群快速幂运算 研究与探讨》
中有要注意这个题目的每一次操作都不是一次置换,要按照它规定的方式操作。
反复操作找出周期circle,题目告诉了你终点状态ed,和操作了s次,要你求起始状态st。
首先s=s%circle。
相当于变换了这么多次。
然后从终点 再变换 circle-s 次,即可得到起点。
很像链表中的一个操作。
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<climits> #include<queue> #include<vector> #include<map> #include<sstream> #include<set> #include<stack> #include<cctype> #include<utility> #pragma comment(linker, "/STACK:102400000,102400000") #define PI 3.1415926535897932384626 #define eps 1e-10 #define sqr(x) ((x)*(x)) #define FOR0(i,n) for(int i=0 ;i<(n) ;i++) #define FOR1(i,n) for(int i=1 ;i<=(n) ;i++) #define FORD(i,n) for(int i=(n) ;i>=0 ;i--) #define lson num<<1,le,mid #define rson num<<1|1,mid+1,ri #define MID int mid=(le+ri)>>1 #define zero(x)((x>0? x:-x)<1e-15) #define mk make_pair #define _f first #define _s second using namespace std; //const int INF= ; typedef long long ll; //const ll inf =1000000000000000;//1e15; //ifstream fin("input.txt"); //ofstream fout("output.txt"); //fin.close(); //fout.close(); //freopen("a.in","r",stdin); //freopen("a.out","w",stdout); const int INF =0x3f3f3f3f; const int maxn= 1000+20 ; //const int maxm= ; //by yskysker123 int n,s,circle; int A[maxn],B[maxn],C[maxn]; void GetCircle() { circle=0; while(1) { circle++; for(int i=1;i<=n;i++) B[i]= C[C[i]]; int i; for(i=1;i<=n;i++) if(B[i]!=A[i]) break; if(i>n) break; for(int i=1;i<=n;i++) C[i]=B[i]; } } void Work(int k) { for(int i=1;i<=k;i++) { for(int j=1;j<=n;j++) B[j]=A[A[j]]; for(int j=1;j<=n;j++) A[j]=B[j]; } for(int i=1;i<=n;i++) printf("%d\n",B[i]); } int main() { while(~scanf("%d%d",&n,&s)) { for(int i=1;i<=n;i++) scanf("%d",&A[i]),B[i]=C[i]=A[i]; GetCircle(); s=s%circle; int k=circle-s; Work(k); } return 0; }
相关文章推荐
- 你不知道的JavaScript--Item7 函数和(命名)函数表达式
- 从现在开始正式进入智能家居行业
- 你不知道的JavaScript--Item7 函数和(命名)函数表达式
- 简单实现利用EasyTouch实现物体的跳跃
- 中间件的日志类
- 简析安卓开发之activity(sumzom)
- 插入排序
- Android Api Demos登顶之路(101)View-->AutoComplete:Multiple items
- C语言——类型限定词
- IOS 中常用站位符
- 进程间通信各方式间比较
- JAVA基础学习day23--GUI基础
- caffe问题及解决方法
- poj3253
- javaWeb中图片验证码的生成
- 小小君的C语言第七课
- ASP.NET 5 Beta8 发布
- 为什么Redis比Memcached易
- org.apache.catalina.LifecycleException
- 简析poco框架库-数据库的用法