Poj 3735 training little cats (matrix construction, fast power)

Source: Internet
Author: User
Training little cats
Time limit:2000 ms   Memory limit:65536 K
Total submissions:10350   Accepted:2471

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


Execute K commands m times to find the number of peanuts for each cat.

Construction matrix: http://blog.csdn.net/magicnumber/article/details/6217602


We construct the (n + 1) * (n + 1) matrix, the number of peanuts stored in the last column, and then the rapid power of the Matrix to add a sparse matrix for optimization: Note, ans. ma [I]

[K] = (ans. ma [I] [k] + X. ma [I] [J] * Y. ma [J] [k]); can be optimized, ans. ma [I] [J] = (ans. ma [I] [J] + X. ma [I] [k] * Y. ma [k] [J]) is not optimal.

This point has been pitfall for a long time.

Code:

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; struct matrix {long ma [130] [130];} A; int N, T; long long m; matrix multi (matrix X, matrix Y) // matrix multiplication {matrix ans; memset (ans. ma, 0, sizeof (ans. ma); For (INT I = 1; I <= n + 1; I ++) {for (Int J = 1; j <= n + 1; j ++) {If (X. ma [I] [J]) // sparse matrix Optimization for (int K = 1; k <= n + 1; k ++) {ans. ma [I] [k] = (ans. ma [I] [k] + X. ma [I] [J] * Y. ma [J] [k]) ;}} return ans ;}int main (){ While (~ Scanf ("% d % i64d % d", & N, & M, & T) & (n + M + T) {for (INT I = 1; I <= n + 1; I ++) {for (Int J = 1; j <= n + 1; j ++) {if (I = J). ma [I] [J] = 1; else. ma [I] [J] = 0 ;}} char s [10]; int X, Y; For (INT I = 0; I <t; I ++) {scanf ("% s", S); If (s [0] = 'G') {scanf ("% d", & X);. ma [x] [n + 1] ++;} If (s [0] = 'E') {scanf ("% d", & X ); for (Int J = 1; j <= n + 1; j ++) {. ma [x] [J] = 0 ;}} if (s [0] = 's') {scanf ("% d", & X, & Y); For (Int J = 1; j <= n + 1; j ++) {long temp =. ma [x] [J];. ma [x] [J] =. ma [y] [J];. ma [y] [J] = temp ;}} matrix ans; For (INT I = 1; I <= n + 1; I ++) // unit matrix {for (Int J = 1; j <= n + 1; j ++) {if (I = J) ans. ma [I] [J] = 1; else ans. ma [I] [J] = 0 ;}while (m) // rapid matrix power {If (M & 1) {ans = multi (ANS, );} A = multi (a, a); M = (M> 1) ;}for (INT I = 1; I <= N; I ++) printf ("% i64d", ans. ma [I] [n + 1]); printf ("\ n");} return 0 ;}




Poj 3735 training little cats (matrix construction, fast power)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.