Click Open Link
Training little cats
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:9807 |
|
Accepted:2344 |
Description
Facer's pet cat just gave birth to a brood of little cats. having considered the health of those lovely cats, Facer decides to make the cats to do some exercises. facer has well designed a set of moves for his cats. he is now asking you to supervise the cats to do his exercises. facer's great exercise for cats contains three different moves:
G I: Let the ith cat take a peanut.
E I: Let the ith cat eat all peanuts it have.
S I j: Let the ith cat and jth cat exchange their peanuts.
All the cats perform a sequence of these moves and must repeat it m times! Poor cats! Only Facer can come up with such embarrassing idea.
You have to determine the final number of peanuts each cat have, and directly give them the exact quantity in order to save them.
Input
The input file consists of multiple test cases, ending with three zeroes "0 0 0". For each test case, three IntegersN,MAndKAre given firstly, whereNIs the number of cats andKIs the length of the move sequence. The followingKLines describe the sequence.
(M≤ 1,000,000,000,N≤ 100,K≤ 100)
Output
For each test case, outputNNumbers in a single line, representing the numbers of peanuts the cats have.
Sample Input
3 1 6g 1g 2g 2s 1 2g 3e 20 0 0
Sample output
2 0 1
Source
PKU campus 2009 (poj monthly contest-2009.05.17), Facer
There are n cats. There are three operations:
G x gives Cat x a peanut
E x let Cat X eat all the peanuts
S x Y: replace the number of peanuts in Cat X and Cat Y.
These operations are a set of K times.
Repeat this entire set of operations m times and ask how many peanuts each cat has.
Cleverly convert to multiply the matrix.
Set one (n + 1) * (n + 1)
Each column represents a cat, and the last row represents the number of peanuts of the cat. If the G operation is performed, the last row is + 1. If the e operation is performed, this column is set to zero. If the s operation is performed, column X and column Y are exchanged. In this way, a matrix is obtained:
{}
{A B}
{}
{}
{C d}
In these four parts, A is a sparse matrix consisting of 0 and 1, B is the last column (except the last element) and all is composed of 0. c is the last row (except the last element) represents the number of peanuts for each cat. D has only one element, which is 1.
Because IM is too large, even the rapid power of the matrix times out, You can optimize the sparse matrix.
The sparse matrix can be calculated as follows:
For (INT I = 0; I <= N; I ++)
For (int K = 0; k <= N; k ++)
If (A. M [I] [k])
For (Int J = 0; j <= N; j ++)
C. M [I] [J] + = a. m [I] [k] * B. m [k] [J];
In this way, you can happily run more than 200 ms on OJ.
// 1768k235ms # include <stdio. h> # include <string. h >#include <algorithm> # define maxn 127 # define ll long longusing namespace STD; ll n, m, K; struct matrax {ll M [maxn] [maxn]; void clear () {memset (M, 0, sizeof (M) ;}} A, per; void Init () // create a matrix {. clear (); Per. clear (); char s [2]; ll X, B; For (INT I = 0; I <= N; I ++) {. M [I] [I] = 1; Per. M [I] [I] = 1 ;}for (LL J = 0; j <K; j ++) {scanf ("% s", S ); if (s [0] = 'G') {scanf ("% i64d", & X);. M [N] [x-1] ++;} If (s [0] = 'E') {scanf ("% i64d", & X ); for (INT I = 0; I <= N; I ++). M [I] [x-1] = 0;} If (s [0] = 's') {scanf ("% i64d % i64d", & X, & B ); for (ll I = 0; I <= N; I ++) Swap (. M [I] [x-1],. M [I] [b-1]) ;}} matrax multi (matrax A, matrax B) // matrix multiplication {matrax C; C. clear (); For (INT I = 0; I <= N; I ++) for (int K = 0; k <= N; k ++) if (. M [I] [k]) for (Int J = 0; j <= N; j ++) C. M [I] [J] + =. M [I] [k] * B. M [k] [J]; return C;} matrax power (ll k) // rapid matrix power {matrax P, ANS = per; P = A; while (k) {If (K & 1) {ans = multi (ANS, P); k --;} else {k/= 2; P = multi (p, p );}} return ans;} int main () {While (scanf ("% i64d % i64d % i64d", & N, & M, & K), N | M | K) {Init (); matrax ans = power (m); For (INT I = 0; I <n-1; I ++) printf ("% i64d", ans. M [N] [I]); printf ("% i64d \ n", ans. M [N] [n-1]);} return 0 ;}