Source: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 3735
Solution:
B Indicates the final result. Here, we construct n + 1-dimensional vectors for N numbers to facilitate subsequent 'G' operations.
Each operation is represented in a matrix. Here, matrix A is (n + 1, n + 1) and initialized as a matrix of units.
The 'G' num operation increases the number of num by 1, and Adds 1 to the last element of row num of matrix A, that is, a [num] [last] + = 1.
The 'E' num operation changes the number of num to 0, and changes all row num of matrix A to 0.
That is, for (I = 1: I <= last; I ++) A [num] [I] = 0;
The 'S' num1 and num2 operations enable the num1 and num2 elements to swap positions, so that the num1 and num2 rows of matrix A can be swapped for (I = 1: I <= last; I ++) Swap (A [num1] [I], a [num2] [I]);
Then calculate the M power of
Then perform a multiplication with the initialized B vector. The result 1 to last-1 of the obtained vector is the required result.
Note:
1: Determine whether the two multiplier values are 0 before multiplication in matrix multiplication.
2: The process of constructing matrix A. At the beginning, I used a matrix multiplication for every operation, resulting in a timeout. In fact, we only need to modify the matrix each time.
3: The final result may exceed the int range. Use _ int64.
Check that there are several other items in status that have been 0 ms. I don't know how they are used. Please also read the tips!
# Include <string. h> <br/> # include <stdio. h> </P> <p> const int S = 102; </P> <p> int size; <br/> // n m K <br/>__ int64 temp1 [s] [s]; <br/>__ int64 temp2 [s] [s]; </P> <p> // a * B = c a, B are all matrices <br/> void multply1 (_ int64 A [] [s], __int64 B [] [s] ,__ int64 C [] [s]) <br/>{< br/> for (INT I = 1; I <= size; I ++) <br/> for (Int J = 1; j <= size; j ++) <br/> {<br/> C [I] [J] = 0; <br/> for (int K = 1; k <= size; k ++) <br/> if (a [I] [k] & B [k] [J]) <br/> C [I] [J] + = A [I] [K] * B [k] [J]; <br/>}</P> <p> // a * B = C A is a matrix, B is the vector <br/> void multply2 (_ int64 A [] [s] ,__ int64 B [] ,__ int64 C []) <br/> {<br/> for (INT I = 1; I <= size; I ++) <br/> {<br/> C [I] = 0; <br/> for (int K = 1; k <= size; k ++) <br/> if (a [I] [k] & B [k]) <br/> C [I] + = A [I] [k] * B [k]; <br/>}</P> <p>/B-> A <br/> void copy (_ int64 A [] [s], __int64 B [] [s]) <br/>{< br/> for (INT I = 1; I <= size; I ++) <br/> for (Int J = 1; j <= size; j ++) <br/> {<br/> [I] [J] = B [I] [J]; <br/>}</P> <p> // a ^ m A is a matrix <br/> void Chang (_ int64 A [] [s ], __int64 M ,__ int64 B [] [s]) <br/>{< br/> If (M> 0) <br/> {<br/> // int temp [s] [s]; <br/> Chang (a, m/2, B ); <br/> // display (B); <br/> copy (temp1, B); <br/> copy (temp2, B ); <br/> multply1 (temp1, temp2, B); <br/> If (M % 2! = 0) <br/>{< br/> copy (temp1, B); <br/> multply1 (temp1, A, B ); <br/>}< br/> else <br/> {<br/> // memset (B, 0, sizeof (B )); why can't I use this here! B is not initialized. I don't know why <br/> for (INT II = 1; II <= size; II ++) <br/> for (INT IIi = 1; III <= size; III ++) <br/> B [II] [III] = 0; </P> <p> for (INT I = 1; I <= size; I ++) <br/> {<br/> B [I] [I] = 1; <br/>}</P> <p> void swap (_ int64 & X ,__ int64 & Y) <br/>{< br/>__ int64 temp = x; <br/> X = y; <br/> Y = temp; <br/>}</P> <p> int main () <br/>{< br/> int N, K; <br/>__ int64 m; <br/> char ch; <br/> while (scanf ("% d % i64d % d", & N, & M, & K) & N + M + k) <br/>{< br/> size = n + 1; </P> <p> // initialize ans <br/>__ int64 ans [s] [s]; <br/> memset (ANS, 0, sizeof (ANS); <br/> for (INT I = 1; I <= size; I ++) <br/> ans [I] [I] = 1; </P> <p> while (k --) <br/>{< br/> getchar (); <br/> scanf ("% C", & Ch ); <br/> int mult [s] [s]; <br/> // Init (CH, mult); </P> <p> int num; </P> <p> If (CH = 'G') <br/> {<br/> scanf ("% d", & num ); <br/> ans [num] [size] + = 1; <br/> // display (mult ); <br/>}< br/> else if (CH = 's') <br/>{< br/> int num1, num2; <br/> scanf ("% d", & num1, & num2); <br/> for (INT J3 = 1; J3 <= size; J3 ++) <br/> swap (ANS [num1] [J3], ANS [num2] [J3]); <br/> // display (mult ); <br/>}< br/> else if (CH = 'E') <br/>{< br/> scanf ("% d", & num ); <br/> for (INT jjj = 1; jjj <= size; jjj ++) <br/> ans [num] [jjj] = 0; <br/> // display (mult); <br/>}</P> <p >__ int64 an [s] [s]; <br/>__ int64 one [s]; <br/>__ int64 two [s]; </P> <p> Chang (ANS, M, ); <br/> // actually the following is redundant. The last column of an matrix is the answer, which can be improved. <br/> int JJ; <br/> for (JJ = 1; JJ <size; JJ ++) <br/> one [JJ] = 0; <br/> one [JJ] = 1; </P> <p> multply2 (an, One, two); </P> <p> printf ("% i64d", two [1]); <br/> for (Int J = 2; j <size; j ++) <br/> printf ("% i64d", two [J]); <br/> printf ("/N"); <br/>}</P> <p>}