Poj3735, Fast Matrix power

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

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


The question is as follows: There are n cats, three commands about peanuts (get peanuts, eat peanuts, exchange peanuts), give a set of commands, repeat n times, ask how many peanuts each cat gets at the end.

M is so big, there is no doubt that the Rapid power of the matrix.

First, construct a matrix of units, because you only need to operate on the matrix of units, and then multiply the matrix obtained after the operation by the initial state to get the final state.

See:

The cat having peanuts in I is to add 1 to the last column of row I in the matrix;

Note: For N cats, use a n + 1 dimensional matrix, that is, a n + 1 dimensional matrix. Only one of them can be used for operation!


The following focuses on the Rapid power of the matrix. The idea is the same as the Rapid power of an integer. However, sparse matrix is used in this process, so we can use this optimization,

That is, the zero element is skipped;

I think it should be noted that when a matrix is multiplied, another zero matrix needs to be used. After multiplication, the result is placed on the matrix to be placed, rather than directly putting the result on the final matrix!

The Code is as follows:


# Include <stdio. h> # include <string. h> # include <math. h> typedef _ int64 int64; int64 C [120] [120], ANS [120] [120], d [120] [120]; struct cat {int64 P [120] [120]; CAT () {memset (p, 0, sizeof (p) ;}; int main () {int64 I, j, m, n, k, X, Y, T; char W [2]; while (scanf ("% i64d % i64d % i64d", & N, & M, & K )! = EOF) // while (scanf (----) cannot be used (----)! = EOF & n! = 0 & M! = 0 & K! = 0) then, if one of them is 0, the {If (n = 0 & M = 0 & K = 0) break; memset (ANS, 0, sizeof (ANS); cat ca; for (I = 0; I <= N; I ++) {ca. P [I] [I] = 1; ans [I] [I] = 1 ;}for (I = 0; I <K; I ++) {scanf ("% s", W); If (W [0] = 'G') {scanf ("% i64d", & X); CA. P [N] [x-1] ++;} else if (W [0] = 'E') {scanf ("% i64d", & X ); for (j = 0; j <= N; j ++) Ca. P [J] [x-1] = 0;} else {scanf ("% i64d % i64d", & X, & Y); For (j = 0; j <= N; j ++) {T = Ca. P [J] [x-1]; CA. P [J] [x-1] = Ca. P [J] [Y-1]; CA. P [J] [Y-1] = T ;}} while (M! = 0) {If (M % 2 = 1) {memset (D, 0, sizeof (d); // The zero matrix D is used here; for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) if (ca. P [I] [J]) for (k = 0; k <= N; k ++) d [I] [k] + = ans [I] [J] * ca. P [J] [k]; // note the subscripts of I, j, and K here. Do not make a mistake. Try for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) ans [I] [J] = d [I] [J]; // then, the result is that the matrix D is placed on the matrix ans instead of directly putting the result on the matrix ans} memset (C, 0, sizeof (c); for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) {If (ca. P [I] [J] = 0) continue; For (k = 0; k <= N; k ++) C [I] [k] + = Ca. P [I] [J] * ca. P [J] [k] ;}for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) CA. [I] [J] = C [I] [J]; M = m/2;} for (I = 0; I <n-1; I ++) printf ("% i64d", ANS [N] [I]); printf ("% i64d \ n", ANS [N] [n-1]);} return 0 ;}




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.