Construct the conversion matrix... for example,. 2 3 1 5 4.
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
Multiply (, 5) with this matrix to (, 4)
Program:
[Cpp]
# Include <iostream>
# Include <stdio. h>
# Include <algorithm>
# Include <string. h>
# Include <math. h>
# Include <map>
# Include <queue>
# Include <stack>
# Include <set>
# Define ll long
# Define OOS 2000000000
# Define pi acos (-1)
Using namespace std;
Struct node
{
Int s [85] [85];
} H, _ 2jie [32];
Int n, m;
Char s [85], ans [85];
Node mul (node a, node B)
{
Int k, I, j;
Node h;
Memset (h.s, 0, sizeof (h.s ));
For (k = 1; k <= n; k ++)
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
H. s [I] [j] + = a. s [I] [k] * B. s [k] [j];
Return h;
}
Node GetMatrix (int m)
{
Int I;
_ 2jie [0] = h;
For (I = 1; I <= 30; I ++) _ 2jie [I] = mul (_ 2jie [I-1], _ 2jie [I-1]);
Memset (h.s, 0, sizeof (h.s ));
For (I = 1; I <= n; I ++) h. s [I] [I] = 1;
For (I = 0; I <= 30; I ++)
If (m & (1 <I ))
H = mul (h, _ 2jie [I]);
Return h;
}
Int main ()
{
Int I, x, t [85];
While (~ Scanf ("% d", & n, & m ))
{
If (! N &&! M) break;
Memset (h.s, 0, sizeof (h.s ));
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & x );
H. s [I] [x] = 1;
}
H = GetMatrix (m );
For (I = 1; I <= n; I ++)
For (x = 1; x <= n; x ++)
If (h.s [I] [x] = 1)
T [x] = I;
Gets (s + 1 );
Gets (s + 1 );
For (I = 1; I <= n; I ++) ans [I] = s [t [I];
Ans [n + 1] = '\ 0 ';
Puts (ans + 1 );
}
Return 0;
}