矩阵乘法的第四类问题。与vijos1049做法一样。
只是这次,他把结果给了你让你求一个原始数列。具体做法就是把变换规则取反,然后还和原来一样矩阵求幂,然后与原始序列相乘,在最后输出答案就可以了。
比方说我的变换规则是 2 3 1 5 4
那么原本的变换操作是
[ [ 0 1 0 0 0 ]
[ 0 0 1 0 0 ]
[ 1 0 0 0 0 ]
[ 0 0 0 0 1 ]
[ 0 0 0 1 0 ] ]
所以逆操作是
[ [ 0 0 1 0 0 ]
[ 1 0 0 0 0 ]
[ 0 1 0 0 0 ]
[ 0 0 0 0 1 ]
[ 0 0 0 1 0 ] ]
题目中给的描述有误,但不影响做题
Decode the Strings
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 521 Accepted Submission(s): 159
Problem Description
Bruce Force has had an interesting idea how to encode strings. The following is the description of how the encoding is done:
Let x 1,x 2,...,x n be the sequence of characters of the string to be encoded.
1. Choose an integer m and n pairwise distinct numbers p 1,p 2,...,p n 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 y i to x pi, and then for 1 ≤ i ≤ n replace x i by y i.
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 p 1, ..., p n 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?
Let x 1,x 2,...,x n be the sequence of characters of the string to be encoded.
1. Choose an integer m and n pairwise distinct numbers p 1,p 2,...,p n 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 y i to x pi, and then for 1 ≤ i ≤ n replace x i by y i.
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 p 1, ..., p n 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
The input contains several test cases. Each test case starts with a line containing two numbers n and m (1 ≤ n ≤ 80, 1 ≤ m ≤ 10
9). The following line consists of n pairwise different numbers p
1,...,p
n (1 ≤ p
i ≤ 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
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?
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define MAXN 100
using namespace std;
struct Matrix
{
int size;
int element[MAXN][MAXN];
void setSize(int);
Matrix operator* (Matrix);
Matrix power(int);
};
void Matrix::setSize(int a)
{
for (int i=0; i<a; i++)
for (int j=0; j<a; j++)
element[i][j]=0;
size = a;
}
Matrix Matrix::operator* (Matrix param)
{
Matrix product;
product.setSize(size);
for (int i=0; i<size; i++)
for (int j=0; j<size; j++)
for (int k=0; k<size; k++)
product.element[i][j]+=element[i][k]*param.element[k][j];
return product;
}
Matrix Matrix::power(int exp)
{
Matrix res,A;
A=*this;
res.setSize(size);
for(int i=0;i<size;i++)
res.element[i][i]=1;
while(exp)
{
if(exp&1)
res=res*A;
exp>>=1;
A=A*A;
}
return res;
}
char str[100];
int n,m;
int squ[100],tsqu[100];
Matrix a;
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0 && m==0)break;
a.setSize(n);
for(int i=0;i<n;i++)
{
int tmp;
squ[i]=i;
scanf("%d",&tmp);
a.element[tmp-1][i]=1;
}
a=a.power(m);
gets(str);
gets(str);
for(int i=0;i<n;i++)
{
int tmp=0;
for(int j=0;j<n;j++)
tmp+=a.element[i][j]*squ[j];
tsqu[i]=tmp;
}
for(int i=0;i<n;i++)
printf("%c",str[tsqu[i]]);
printf("\n");
}
return 0;
}