USC 20121007 组队赛 H题 矩阵乘法
来源:互联网 发布:免费报税软件 编辑:程序博客网 时间:2024/06/06 07:36
H. Decode
Bruce Force has had an interesting idea how to encode strings. The following is the description of how the encoding is done:
Let x1,x2,...,xn be the sequence of characters of the string to be encoded.
1. Choose an integer m and n pairwise distinct numbers p1,p2,...,pn from the set {1, 2, ..., n} (a permutation of the numbers 1 to n).
2. Repeat the following step m times.
3. For 1 ≤ i ≤ n set yi to xpi, and then for 1 ≤ i ≤ n replace xi by yi.
For example, when we want to encode the string "hello", and we choose the value m = 3 and the permutation 2, 3, 1, 5, 4, the data would be encoded in 3 steps: "hello" -> "elhol" -> "lhelo" -> "helol".
Bruce gives you the encoded strings, and the numbers m and p1, ..., pn used to encode these strings. He claims that because he used huge numbers m for encoding, you will need a lot of time to decode the strings. Can you disprove this claim by quickly decoding the strings?
Input Specification
The input contains several test cases. Each test case starts with a line containing two numbers n and m (1 ≤ n ≤ 80, 1 ≤ m ≤ 109). The following line consists of n pairwise different numbers p1,...,pn (1 ≤ pi ≤ n). The third line of each test case consists of exactly n characters, and represent the encoded string. The last test case is followed by a line containing two zeros.
Output Specification
For each test case, print one line with the decoded string.
Sample Input
5 3
2 3 1 5 4
helol
16 804289384
13 10 2 7 8 1 16 12 15 6 5 14 3 4 11 9
scssoet tcaede n
8 12
5 3 4 2 1 8 6 7
encoded?
0 0
Sample Output
hello
second test case
encoded?
原来做这道题是小弟用找循环节做的,超时!听闻学长说矩阵乘法,看看想想还是一次过了,嘿嘿。相当于构造n*n的矩阵,我们换成正推的方法,如果交换则将矩阵的列交换。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N = 80; int n,mod; struct Matrix { int mat[N][N]; Matrix(){memset(mat,0,sizeof(mat));}}; char str[90];void init_e(Matrix &m ) {//初始化矩阵 for (int i = 0; i < n; i++) for (int j=0; j<n; j++) m.mat[i][j]=(i==j);} void init(Matrix &m ,int a[]){ for (int i = 0; i < n; i++) m.mat[i][a[i]-1]=1;} //乘法 Matrix operator * (Matrix a, Matrix b) { Matrix ret; for (int i = 0; i < n; i++) { for (int k = 0; k < n; k++) if (a.mat[i][k]) { for (int j = 0; j < n; j++) if (b.mat[k][j]) ret.mat[i][j] = ret.mat[i][j] + a.mat[i][k] * b.mat[k][j]; } } return ret; } //求幂一般都是正方形矩阵,所以ret = a; Matrix operator ^ (Matrix a, int b) { Matrix ret = a, tmp = a; init_e(ret); for ( ; b; b >>= 1) { if (b & 1) { ret = ret * tmp; } tmp = tmp * tmp; } return ret; }void Print(Matrix&a){ char ret[90]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++) if( a.mat[j][i]) ret[i]=str[j]; } ret[n]='\0'; printf("%s\n",ret);}int main(){ int k,a[90]; while( scanf("%d%d",&n,&k)){ if(n==0&&k==0) break; for(int i=0;i<n;i++) scanf("%d",&a[i]); getchar(); gets(str); Matrix E; init(E,a); Matrix ret=E^k; Print(ret); } return 0;}
- USC 20121007 组队赛 H题 矩阵乘法
- 4.6组队赛H题分石头问题,推导,规律
- USC newweek2 H hdu 3335
- 组队赛(B/H Sudoku) HDU
- 矩阵快速幂 武汉组队赛
- 【线段树】【4-6组队赛】Problem H
- 20110805组队赛d题
- 20110805 组队赛 f题
- 20110805 组队赛b题
- 20110805组队赛c题
- 【蓝桥杯训练题】矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 判断一个单链表是否有环及环的链接点(转)
- 书香与女人
- opencv2.4.2安装与配置
- 颜色空间转换:RGB转换为Lab(续)
- ADO编程的网址---->vc++利用ADO操作数据库的网址
- USC 20121007 组队赛 H题 矩阵乘法
- µC/OS-II内存管理
- 内存管理函数HeapAlloc,LocalAlloc,VirtualAlloc,malloc,new的异同
- GBK编码表
- 打开ev_dbg的log信息
- Android自动化工具Monkeyrunner使用(六) —— touch
- MyEclipse10离线安装SVN插件
- SpringMVC+FreeMarker(FTL)Integration example
- 销售订单隐藏成本价格出口 user-exit