【POJ】1721 - CARDS(置换群)
2016-07-19 16:14
387 查看
点击打开题目
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
这道题要算逆置换,而且每次置换都是两次。
还是先算出循环阶数T。关键的地方在于把逆置换群变为正的:p(2^m) = q 那么由 q 求 p 时,可以从q出发,进行 T - m % T 次正置换(两次),即:
q(2^( T - m % T ))= p
这样直接正着求p就行啦。
代码如下:
#include <cstdio>
#define MAX 1000
int pr[MAX+11];
int ne[MAX+11];
int tt[MAX+11];
int n,m;
int T; //循环周期
void find() //寻找循环周期
{
T = 0;
while (1)
{
for (int i = 1 ; i <= n ; i++)
ne[i] = tt[tt[i]]; //每次进行两重置换
T++;
bool flag = true; //是否循环
for (int i = 1 ; i <= n ; i++)
if (ne[i] != pr[i])
{
flag = false; //未构成循环
break;
}
if (flag)
break;
for (int i = 1 ; i <= n ; i++)
tt[i] = ne[i];
}
}
int main()
{
while (~scanf ("%d %d",&n,&m))
{
for (int i = 1 ; i <= n ; i++)
{
scanf ("%d",&pr[i]);
tt[i] = pr[i];
}
find();
int ant = T - m % T; //正循环次数
for (int i = 1 ; i <= ant ; i++)
{
for (int j = 1 ; j <= n ; j++)
pr[j] = ne[ne[j]];
for (int j = 1 ; j <= n ; j++)
ne[j] = pr[j];
}
for (int i = 1 ; i <= n ; i++)
printf ("%d\n",pr[i]);
}
return 0;
}
CARDS
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1831 | Accepted: 940 |
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
这道题要算逆置换,而且每次置换都是两次。
还是先算出循环阶数T。关键的地方在于把逆置换群变为正的:p(2^m) = q 那么由 q 求 p 时,可以从q出发,进行 T - m % T 次正置换(两次),即:
q(2^( T - m % T ))= p
这样直接正着求p就行啦。
代码如下:
#include <cstdio>
#define MAX 1000
int pr[MAX+11];
int ne[MAX+11];
int tt[MAX+11];
int n,m;
int T; //循环周期
void find() //寻找循环周期
{
T = 0;
while (1)
{
for (int i = 1 ; i <= n ; i++)
ne[i] = tt[tt[i]]; //每次进行两重置换
T++;
bool flag = true; //是否循环
for (int i = 1 ; i <= n ; i++)
if (ne[i] != pr[i])
{
flag = false; //未构成循环
break;
}
if (flag)
break;
for (int i = 1 ; i <= n ; i++)
tt[i] = ne[i];
}
}
int main()
{
while (~scanf ("%d %d",&n,&m))
{
for (int i = 1 ; i <= n ; i++)
{
scanf ("%d",&pr[i]);
tt[i] = pr[i];
}
find();
int ant = T - m % T; //正循环次数
for (int i = 1 ; i <= ant ; i++)
{
for (int j = 1 ; j <= n ; j++)
pr[j] = ne[ne[j]];
for (int j = 1 ; j <= n ; j++)
ne[j] = pr[j];
}
for (int i = 1 ; i <= n ; i++)
printf ("%d\n",pr[i]);
}
return 0;
}
相关文章推荐
- 抽屉原理简单应用 POJ 2356 POJ 3370
- 在线lca算法模板
- CAS sso配置
- 浅谈前端与SEO
- Activity与Fragment的生命周期
- HDU 1070 Milk
- 获取当前段和行
- Docker: 如何修改 Docker 的镜像存储位置
- [Redis] redis入门
- 考虑这个简单的函数
- zk create() 方法
- DSP程序中 ,如何完成延时且不影响整体程序的工作效率
- 教你初步了解红黑树
- 命令行操作 sqlite 数据库
- Freescale android 4.4.2 平台工具链安装--技巧
- 搜集的一些mysql数据库操作,建表之类的就不写啦
- MFC一些理解
- CGLib动态代理(转载)
- Spark应用(app jar)发布到Hadoop集群的过程
- Spark应用(app jar)发布到Hadoop集群的过程