题目意思可概括为给定集合S = {1,..,n}的一个双射关系f, 求经过k次复合之后元素i对应的元素fk(i) (i∈S)。

由于函数是双射,一个原像对应唯一一个像,同样一个像有唯一一个原像,考虑整个映射关系,存在整数m∈ Z,使得fm=f0=I。

即具有周期性。

每个元素映射回它自己有独立的周期T(i),整个映射的周期T=lcm(T(i)), i ∈ S。

独立处理更快,但对于本题也是刚刚卡过。

当然如果事先把所有询问读入加以预处理或者直接全部预处理会更快。

样例代码860ms卡过。

http://poj.org/problem?id=1026
 
 #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = + ;
int n;
char s[maxn];
int f[maxn];
int period[maxn];
int repeats; struct Node{
int from, to;
}node[maxn]; int gcd(int a, int b){
if(!b) return a;
return gcd(b, a % b);
} bool cmp(Node a, Node b){
return a.to < b.to;
} void init(){
for(int i = ; i < n; i++){
int t = f[i], cnt = ;
while(i != t) t = f[t], ++cnt;
period[i] = cnt;
}
} void solve(){
for(int i = ; i < n; i++) node[i].from = node[i].to = i;
for(int i = ; i < n; i++){
int p1 = repeats % period[i];
while(p1--) node[i].to = f[node[i].to];
}
sort(node, node + n, cmp);
for(int i = ; i < n; i++) printf("%c", s[node[i].from]);
printf("\n");
} int main(){
while(~scanf("%d", &n) && n){
for(int i = , j; i < n; i++){
scanf("%d", &j);
f[i] = j - ;
}
init();
while(~scanf("%d", &repeats) && repeats){
getchar();
gets(s);
int len = strlen(s);
for(int i = len; i < n; i++) s[i] = ' ';
s[n] = '\0';
solve();
}
printf("\n");
}
return ;
}

poj1026 Cipher的更多相关文章

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

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

  2. poj1026 Cipher ——置换群

    link:http://poj.org/problem?id=1026 其实这道题目和poj2369这道题目一样. 都是基础的置换群题目.把那道题目理解了,这道题就没问题了. 不过我的方法貌似比较挫, ...

  3. JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher

    加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...

  4. BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6014  Solved: 2503[Submit ...

  5. freeCodeCamp:Caesars Cipher

    让上帝的归上帝,凯撒的归凯撒. 下面我们来介绍风靡全球的凯撒密码Caesar cipher,又叫移位密码. 移位密码也就是密码中的字母会按照指定的数量来做移位. 一个常见的案例就是ROT13密码,字母 ...

  6. ural Cipher Message

    Cipher Message Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Desc ...

  7. 紫书例题-Ancient Cipher

    Ancient Roman empire had a strong government system with various departments, including a secret ser ...

  8. 【BZOJ-1031】字符加密Cipher 后缀数组

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5504  Solved: 2277[Submit ...

  9. uva-1339Ancient Cipher

    Ancient Roman empire had a strong government system with various departments, including a secret ser ...

随机推荐

  1. struts标签小记

    1.<s:iterator>标签的  奇偶数行使用不同样式 <s:iterator id="list" value="#request.listq&qu ...

  2. Compile Sources 和 Copy Bundle Resources的区别

    Compile Sources主要存放.m文件 Copy Bundle Resources 主要存放xib plist bundle js 文件

  3. 体验Java的封装性

    package com.cnblogs.java; //体验Java的封装性 /* * 如下的人类年龄赋值-300岁,显然很不合理,这种直接对类的属性赋值,有时候虽然不合理但却会编译通过. * 所以我 ...

  4. codeforces 520 Two Buttons

    http://codeforces.com/problemset/problem/520/B B. Two Buttons time limit per test 2 seconds memory l ...

  5. 部署ganglia3.7

    环境 centOS6.6 gmetad节点关闭iptable gmetad和httpd只需要在一台节点安装,gmond需要在每台节点上安装. 一.安装epel源 sudo wget http://do ...

  6. Kafka集群模式部署

    环境:kafka 0.8.1.1 基本概念 Kafka维护按类区分的消息,称为主题(topic) 生产者(producer)向kafka的主题发布消息 消费者(consumer)向主题注册,并且接收发 ...

  7. spark-submit常用参数

    yarn模式默认启动2个executor,无论你有多少的worker节点 standalone模式每个worker一个executor,无法修改executor的数量 partition是RDD中的一 ...

  8. BackgroundWorker的使用方法

    http://msdn.microsoft.com/zh-cn/library/system.componentmodel.backgroundworker(VS.80).aspx Backgroun ...

  9. paper 25 :SVM支持向量机是什么意思?

    转载来源:https://www.zhihu.com/question/21094489 作者:余洋链接:https://www.zhihu.com/question/21094489/answer/ ...

  10. 17---Net基础加强

    更新中,敬请期待............ 复习 将xml显示到treeview 修改增加 删除 foreach原理 深拷贝与浅拷贝 模拟数据库及登陆 复习总结