POJ 1721
好像不需要用到开方什么的。。。
可以知道,一副牌即是一个循环,那么,由于GCD(L,K)=1,所以一次洗牌后,亦是一个循环。其实,K次洗牌等于是T^(2^K)了。既然是循环,必定有周期。那么,周期是多少呢?以例子为例:1->4->6->2->7->3->5。其实对于第一个数(从零始)4,总会有先后移了2^a次方而回到原点,此时就是一个周期了。即是求2^a=1(mod n)。求出最小的a即可知道周期。s%a=t.那么,即是差a-t个状态就可以回到初始的了。
#include <iostream>
#include <algorithm>
#include <cstdio> using namespace std; int num[1005];
int tmp[1005];
int ans[1005]; int control(int n){
int ans=1;
for(int i=1;i<=n-1;i++){
ans=(ans*2)%(n);
if(ans==1)
return i;
}
} int quick(int s,int n){
int res=1; int p=2;
while(s){
if(s&1) res=(res*p)%n;
s>>=1;
p=(p*p)%n;
}
return res;
} int main(){
int n,s,cnt,k,pos;
while(scanf("%d%d",&n,&s)!=EOF){
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
int cy=control(n);
// cout<<"cy="<<cy<<endl;
tmp[0]=1;
cnt=0; k=1;
while(num[k]!=1){
tmp[++cnt]=num[k];
k=num[k];
}
/* for(int i=0;i<n;i++)
cout<<tmp[i]<<' ';
cout<<endl;*/
s=s%cy;
s=cy-s;
// cout<<"s="<<s<<endl;
s=quick(s,n);
// cout<<"s="<<s<<endl;
ans[0]=tmp[0];
pos=0;
for(int i=1;i<n;i++){
ans[i]=tmp[pos=(pos+s)%n];
}
for(int i=1;i<=n;i++){
num[ans[i-1]]=ans[i%n];
}
for(int i=1;i<=n;i++){
printf("%d\n",num[i]);
}
}
return 0;
}
POJ 1721的更多相关文章
- 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 CARDS
Alice and Bob have a set of N cards labelled with numbers 1 ... N (so that no two cards have the sam ...
- poj 题目分类(1)
poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...
- POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 转载:poj题目分类(侵删)
转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
随机推荐
- 基本socket api
socket函数,为了执行网络I/O,一个进程必须做的第一件事就是调用socket函数,并且指定通信协议类型. #include<sys/socket.h> int socket (int ...
- UVa11183 - Teen Girl Squad(最小树形图-裸)
Problem I Teen Girl Squad Input: Standard Input Output: Standard Output -- 3 spring rolls please. - ...
- UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)
先从中找出性能最好的那个数, 在用钱比較少的去组合,能组出来就表明答案在mid的右边,反之在左边, #include<string.h> #include<map> #incl ...
- poj 3356
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- 在linux環境下安裝jprofiler_linux_8_0_2.sh
1.安装jprofiler_linux_8_0_2.sh chmod+x jprofiler_linux_8_0_2.sh ./jprofiler_linux_8_0_2.sh –c 安装过程略..差 ...
- Qt Installer Framework的学习(三)
Qt Installer Framework的学习(三) Qt Installer Framework的样例中.通常是这种:config目录一般放了一个config.xml文件,包括的是安装配置xml ...
- Xcode6 引入第三方静态库project的方法
首先.介绍一下把在当前project中引入其它依赖project的方法: 第一:把其它项目project加入到现有project做法: 定义: FPro 现有project == 父project C ...
- Adding Kentico controls to the Visual Studio toolbox
https://docs.kentico.com/k10/references/kentico-controls https://docs.kentico.com/k10/references/ken ...
- iOS-UIApplication详解
UIApplication简介 UIApplication对象是应用程序的象征. 每一个应用程序都有自己的UIApplication对象,而且是单例. 一个iOS程序启动后创建的第一个对象就是UIAp ...
- 在ubuntu下安装zookeeper
安装java环境,并配置好java相关的环境变量$JAVA_HOME. 1.下载并解压最新稳定的zookeeper文件 wget http://mirrors.cnnic.cn/apache/zook ...