组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)

时间:2022-06-03 01:02:38

CARDS

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1448   Accepted: 773

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

7 4
6
3
1
2
4
7
5

Sample Output

4
7
5
6
1
2
3

Source

 

Mean:

剀剀和凡凡有N张牌(依次标号为1,2,……,N)和一台洗牌机。假设N是奇数。洗牌机的功能是进行如下的操作:对所有位置I(1≤I≤N),如果位置I上的牌是J,而且位置J上的牌是K,那么通过洗牌机后位置I上的牌将是K。

剀剀首先写下一个1~N的排列ai,在位置ai处放上数值ai+1的牌,得到的顺序x1, x2, ..., xN作为初始顺序。他把这种顺序排列的牌放入洗牌机洗牌S次,得到牌的顺序为p1, p2, ..., pN。

现在,剀剀把牌的最后顺序和洗牌次数告诉凡凡,要凡凡猜出牌的最初顺序x1, x2, ..., xN。

analyse:

刚开始搞置换群,看得云里雾里的,还好看到了潘震皓的《置换群快速幂运算 + 研究与探讨》,讲的很清楚,而且很符合ACM的出题习惯。

很显然,这是一题典型的置换群问题,一副扑克就是一个置换,而对于每次的操作,我们可以看作置换的平方运算,题目说n为奇数,这就保证了在进行置换平方运算的过程中不会出现分裂,那么我们就可以使用置换群的快速幂来做了。

进行2*x次运算就可,当然其中有一个剪枝,将O(n^2+logs)的时间复杂度变为了O(n+logs)了,十分经典。

Time complexity:O(n+logs)

Source code:

// Memory   Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-09-11-20.34
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1010
#define LL long long
using namespace std;
int a[N],b[N],c[N],n,m;
int work()
{
int j;
int cnt=0;
while(1)
{
for(int i=1;i<=n;i++)
b[i]=c[c[i]];
cnt++;
for(j=1;j<=n;j++)
if(b[j]!=a[j])
break;
if(j>n)break;
for(int i=1;i<=n;i++)
c[i]=b[i];
}
return cnt;
}
int main()
{
// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
c[i]=a[i];
b[i]=a[i];
}
int cnt=work();
m%=cnt;
m=cnt-m;
while(m--)
{
for(int i=1;i<=n;i++)
b[i]=a[a[i]];
for(int i=1;i<=n;i++)
a[i]=b[i];
}
for(int i=1;i<=n;i++)
printf("%d\n",b[i]);
}
return 0;
}

  

组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)的更多相关文章

  1. &lbrack;CareerCup&rsqb; 18&period;2 Shuffle Cards 洗牌

    18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of ...

  2. poj 3128 Leonardo&&num;39&semi;s Notebook &lpar;置换群的整幂运算&rpar;

    题意:给你一个置换P,问是否存在一个置换M,使M^2=P 思路:资料参考 <置换群快速幂运算研究与探讨> https://wenku.baidu.com/view/0bff6b1c6bd9 ...

  3. POJ 1995&lpar;有关快速幂运算的一道水题&rpar;

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9745   Accepted: ...

  4. POJ1026 Cipher(置换的幂运算)

    链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  5. 《挑战程序设计竞赛》2&period;6 数学问题-快速幂运算 POJ1995

    POJ3641 此题应归类为素数. POJ1995 http://poj.org/problem?id=1995 题意 求(A1^B1+A2^B2+ - +AH^BH)mod M. 思路 标准快速幂运 ...

  6. &lbrack;ACM&rsqb; hdu 3923 Invoker &lpar;Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

    Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...

  7. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  8. 【BZOJ-1965】SHUFFLE 洗牌 快速幂 &plus; 拓展欧几里德

    1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 541  Solved: 326[Submit][St ...

  9. 算数运算符&colon; &plus; - &ast; &sol; &sol;&sol;&lpar;地板除&rpar; &percnt;&lpar;取余&rpar; &ast;&ast;&lpar;幂运算&rpar; &sol; 比较运算符 &gt&semi; &lt&semi; &gt&semi;&equals; &lt&semi;&equals; &equals;&equals; &excl;&equals;

    # ### python运算符 #(1) 算数运算符: + - * / //(地板除) %(取余) **(幂运算) var1 = 5 var2 = 8 # +res = var1 + var2 pri ...

随机推荐

  1. MVC IIS环境部署注意事项

    环境:MVC3.0,IIS7 Mvc3.0的部署除像正常部署aspx程序一样外,另外还需要注意的几点: 1. 安装MVC3.0 确保服务器上安装了MVC3.0,默认版本是“3.0.20105.0” 2 ...

  2. 微信支付开发&lpar;1&rpar; JS API支付

    关键字:微信支付 微信支付v3 jsapi支付 统一支付 Native支付 prepay_id 作者:方倍工作室原文: http://www.cnblogs.com/txw1958/p/wxpayv3 ...

  3. jQuery实践——属性和css篇

    属性: attr html:<div>demo1</div> jQuery:$("div").attr("id","demo1 ...

  4. 20145225《Java程序设计》 2015—2016年学期课程总结

    20145225<Java程序设计> 2015—2016年学期课程总结 读书笔记链接汇总 1.2016年2月25日 <Java程序设计>课程准备之问卷调查 摘要: 一.你对自己 ...

  5. Cocos2d-x 3&period;0标签类Label

    Cocos2d-x 3.0后推出了新的标签类Label,这种标签通过使用FreeType[1]来使它在不同的平台上有相同的视觉效果.由于使用更快的缓存代理,它的渲染也将更加快速.Label提供了描边和 ...

  6. &lbrack;Hive - LanguageManual&rsqb; Create&sol;Drop&sol;Alter Database Create&sol;Drop&sol;Truncate Table

    Hive Data Definition Language Hive Data Definition Language Overview Create/Drop/Alter Database Crea ...

  7. jquery、js全选反选checkbox

    操作checkbox,全选反选 //全选 function checkAll() { $('input[name="TheID"]').attr("checked&quo ...

  8. C&num; 通过豆瓣网络编程API获取图书信息

    这篇文章主要是关于如何通过豆瓣API获取信息的书籍,起初,我看到了原来的想法的内容是"C# 网络编程之网页简单下载实现"中通过HttpWebResponse类下载源代码,再通过正則 ...

  9. 主机windwo7&plus;虚拟机centos如何配置虚拟机可以上网,且与主机互ping通

    /etc/sysconfig/network-scripts/ifcfg-eth0的配置如下: E=eth0PROTO=static#IPADDR=10.1.1.138IPADDR=192.168.0 ...

  10. Python之Django环境搭建&lpar;MAC&plus;pycharm&plus;Django&plus;&plus;postgreSQL&rpar;

    Python之Django环境搭建(MAC+pycharm+Django++postgreSQL) 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/7828614. ...