poj1026 Cipher
题目意思可概括为给定集合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的更多相关文章
- POJ1026 Cipher(置换的幂运算)
链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- poj1026 Cipher ——置换群
link:http://poj.org/problem?id=1026 其实这道题目和poj2369这道题目一样. 都是基础的置换群题目.把那道题目理解了,这道题就没问题了. 不过我的方法貌似比较挫, ...
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6014 Solved: 2503[Submit ...
- freeCodeCamp:Caesars Cipher
让上帝的归上帝,凯撒的归凯撒. 下面我们来介绍风靡全球的凯撒密码Caesar cipher,又叫移位密码. 移位密码也就是密码中的字母会按照指定的数量来做移位. 一个常见的案例就是ROT13密码,字母 ...
- ural Cipher Message
Cipher Message Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- 紫书例题-Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret ser ...
- 【BZOJ-1031】字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5504 Solved: 2277[Submit ...
- uva-1339Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret ser ...
随机推荐
- 试用windows Azure
试用windows Azure, 需要国外手机注册,信用卡注册. windows操作系统,只有2008R2,2012,2012R2可以选择,我选择XS最低档,然后选2012R2,欧洲数据中心,那个慢啊 ...
- Leetcode: Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [转]Java多线程干货系列—(一)Java多线程基础
Java多线程干货系列—(一)Java多线程基础 字数7618 阅读1875 评论21 喜欢86 前言 多线程并发编程是Java编程中重要的一块内容,也是面试重点覆盖区域,所以学好多线程并发编程对我们 ...
- C#写好的类库dll怎么在别人调用的时候也能看到注释?
菜单 Project -> 'xxxx' Properties -> Build -> Output -> 勾上 XML Documentation file
- Android异步任务AsyncTask
package com.example.asynctask; import java.net.MalformedURLException; import java.net.URL; import an ...
- Oracle中的MD5加密
一.技术点 1. DBMS_OBFUSCATION_TOOLKIT.MD5 DBMS_OBFUSCATION_TOOLKIT.MD5是MD5编码的数据包函数,但偶在使用select DBMS_OBFU ...
- Perl中的替换(七)
在Perl中使用s///进行替换操作,与m//进行查找操作类似. s/with (\w+)/against $1's team/; ##第一个双斜线,表示被替代的文本.第二个双斜线,表示将替 ...
- OpenStack fuel-web不可用解决办法
Contents [hide] 1 为增加一台计算节点 2 磁盘清空 3 启动占用8001端口的进程 4 启动占用5432端口的进程 为增加一台计算节点 打开fuel-web,发现无法打开,弹出ngi ...
- 利用foreach对页面控件的遍历 及三目运算符的使用
1.利用foreach对页面控件的遍历 及三目运算符的使用 利用div将一组CheckBox放在一起用于遍历 <body> <form id="form1" ru ...
- python核心编程学习记录之基础知识
虽然对python的基础知识有所了解,但是为了更深入的学习,要对python的各种经典书籍进行学习 第一章介绍python的优缺点,略过 第二章介绍python起步,第三章介绍python基础,仅记录 ...