poj1721
来源:互联网 发布:临沂有淘宝交流 编辑:程序博客网 时间:2024/04/30 07:11
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.
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).
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).
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 46312475
Sample Output
4756123
Source
CEOI 1998
置换群。
代码如下:
#include<iostream>using namespace std;int main(void) { int n, m;//牌的数量,置换次数 int a[1010][1010];//存储从末状态做i次置换后j位置放的牌 int b[1010]; while (cin >> n >> m) { for (int i = 1; i <= n; i++) cin >> a[0][i];//读取数据 int t = 0;//置换次数 while (1) {//找出置换周期 for (int i = 1; i <= n; i++) b[i] = a[t][a[t][i]]; t++; for (int i = 1; i <= n; i++) a[t][i] = b[i]; if (a[t][1] == a[0][1]) break; } m = t - m % t; for (int i = 1; i <= n; i++)//第m次置换就是初始状态 cout << a[m][i] << endl; } return 0;}
- poj1721
- poj1721
- poj1721 CARDS----置换
- [poj1721]置换群循环节
- SQL优化原则
- linux开发板支持NFS文件系统配置
- STM32之GPIO
- SWFUpload介绍 使用SWFUpload上传文件
- 学习Joomla1--密码加密方式
- poj1721
- DreamWeaver文件保存时,提示"发生共享违例"问题的解决方法
- Setforegroundwindow 将窗口置最前
- 黑马程序员—.NET基础—04
- core dump
- 上传图片
- windows7下安装ubuntu双系统
- SQL Server 系统数据库
- 【最长非升子序列】北大 POJ 1887 Testing the CATCHER