题目意思可概括为给定集合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. Android项目目录结构分析

    Android项目目录结构分析 1.HelloWorld项目的目录结构1.1.src文件夹1.2.gen文件夹1.3.Android 2.1文件夹1.4.assets 1.5.res文件夹1.6.An ...

  2. ads 错误

    这个问题已经不是第一次碰到了,每次弄周立功的EasyARM2210的时候都会遇见,每次都没有记住.就是要用ADS运行板子配套光盘里面的配套程序的时候会出现: (Fatal)L6002U:Could n ...

  3. poj: 2159

    简单题,看起来很凶 #include <iostream> #include <stdio.h> #include <string> #include <st ...

  4. C++之路进阶——codevs2366(朋友圈)

    2366 朋友圈 2012年省队选拔赛河北  时间限制: 10 s  空间限制: 256000 KB  题目等级 : 大师 Master     题目描述 Description 在很久很久以前,曾经 ...

  5. sql字符转换函数大全

    删除空格 有两个函数,TTRIM()和LTRIM(),可以用来从字符串中剪掉空格.函数LTRIM()去除应该字符串前面的所有空格:函数RTRIM()去除一个字符串尾部的所有空格.这些和vbscript ...

  6. JAVA测试装饰者模式

    package shb.java.demo; /** * 测试装饰者模式 * @package :shb.java.demoJava02 * @author shaobn * @Describe : ...

  7. 对于Mybatis在C#.Net中个人使用的总结(一) Mybatis 的结果映射

    (图片中的文字上传之后就都看不清,我再图片的下边会用斜体字标清) 首先我在项目中使用Mybatis 是用XML完成映射的.至于XML这门语言,其实很简单的(对于入门来说,因为我是刚入门哈~),如果你还 ...

  8. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

    Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You ...

  9. Sqlserver 平面文件导入/ SSIS FlatFileSource导入文件时 出现LocaleID is not installed报错问题

    最近在使用SqlServer和SSIS导入一个CSV文件到数据库时(SSIS选用的FlatFileSource作为数据流源),老是遇到  The LocaleID 4 is not installed ...

  10. kdump failed

    kdump  是一种先进的基于 kexec 的内核崩溃转储机制.当系统崩溃时,kdump 使用 kexec 启动到第二个内核. 第二个内核通常叫做捕获内核,以很小内存启动以捕获转储镜像.第一个内核保留 ...