Training Little Cats (poj3735, Matrix fast Power)

Source: Internet
Author: User

Training Little Cats
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10737 Accepted: 2563
Description

Facer ' s pet cat just gave birth to a brood of little cats. Have considered the health of those lovely cats, Facer decides to make the cats to do some exercises. Facer have well designed a set of moves for his cats. The He is now asking your to supervise the cats to doing 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 has.
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.
Determine the final number of peanuts each cat has, and directly give them the exact quantity in order to Sav e them.

Input

The input file consists of multiple test cases, ending with three zeroes "0 0 0". For each test case, three integers n, m and K were given firstly, where n is the number of cats and K is the length of the move sequence. The following K lines describe the sequence.
(m≤1,000,000,000, n≤100, k≤100)

Output

For each test case, output n numbers in a, representing the numbers of peanuts the cats has.

Sample Input
3 1 6g 1g 2g 2s 1 2g 3e 20 0 0
Sample Output
2 0 1

"Test Instructions": There are n cats, at the beginning of each cat has 0 peanuts, the existing set of operations, by the following three of the K operation consists of:
1. G I give I a cat a peanut
2. E I let the cat eat all the peanuts it has
3. S I J Exchange cat I and Cat J's Own peanuts

now the above set of operations to do m times, ask each cat how many peanuts?


"Solving the puzzle": M reaches 10^9, obviously cannot calculate directly.
Because K operation is fixed after giving, so think of matrix, matrix fast power can reduce time complexity to O (LOGM). Question to how to construct transpose matrix?
Next I think, observe the above three kinds of operation, found that the second, three kinds of operation is easier to handle, the focus falls on the first operation.
A good way to do this is to add a helper that changes the initial matrix to a n+1 tuple numbered 0 through N, with 3 cats as an example:
Defining the initial matrixA = [1 0 0 0], the number of No. 0 elements is fixed to 1,1~n respectively for the corresponding cat-owned peanuts.
For the first operation G I, we make mat[0][i] 1 on the basis of the unit matrix, for example, G 1:
110 0
0 1 0 0
0 0 1 0
0 0 0 1, obviously [1 0 0 0]*mat = [1 1 0 0]
For the second operation E I, we make mat[i][i] = 0 on the basis of the unit matrix, for example, E 2:
1 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1, obviously [1 2 3 4]*mat = [1 2 0 4]
For the third operation S I J, we exchange the column I with section J on the basis of the unit matrix,For example S 1 3:
1 0 0 0
0 0 0 1
0 0 1 0
0 1 0 0, obviously [1 2 0 4]*mat = [1 4 0 2]
Now, for each operation we can get a transpose matrix that multiplies the K-operation Matrix we can get a new transpose matrix T.
A * t means that we go through a set of operations, like we can get the matrix of the M-group operation as a * T ^ m, the final matrix of [0][1~n] is the answer.

The above approach is straightforward, but it is too cumbersome to construct a K-different matrix.

Is there any other way to directly construct the transpose matrix T? The answer is yes.
We are still based on the unit matrix:
For the first operation of G I, we make mat[0][i] = Mat[0][i] + 1;
For the second operation E I, we make the matrix I Lieqing 0;
For the third operation S I j, we make the column I and section J interchangeable.
With this implementation, we are always dealing with a matrix, eliminating the hassle of constructing K-matrices.

At this point, the construction of the transpose matrix T is complete, then simply use the matrix to find a fast power of a * T ^ M, there is a note, the problem needs to use a long long.
The implementation can look at the following code.





Reprint Please specify the Source:Looking for Children & stars

Title Link: http://poj.org/problem?id=3735
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#defineLL __int64#defineMmax 105structmatrix{LL Mat[mmax][mmax]; voidZero () {memset (Mat,0,sizeof(MAT)); }    voidUnit () { for(intI=0; i<mmax; i++)             for(intj=0; j<mmax; J + +) Mat[i][j]= (i==j); }} a,t; //A = initial matrix, T = Transpose matrixintN;matrix Multiply (Matrix A,matrix b) {matrix C; memset (C.mat,0,sizeof(C.mat));  for(intI=0; i<n+1; i++)    {         for(intj=0; j<n+1; J + +)        {            if(a.mat[i][j]==0)Continue;  for(intk=0; k<n+1; k++)            {                if(b.mat[j][k]==0)Continue; C.MAT[I][K]+=a.mat[i][j]*B.mat[j][k]; }        }    }    returnC;}    Matrix Quicklymod (Matrix A,ll N) {matrix res;  for(intI=0; i< the; i++)//Unit Array         for(intj=0; j< the; J + +) Res.mat[i][j]= (i==j);  while(n) {if(n&1) Res=multiply (a,res); A=multiply (a,a); N>>=1; }    returnRes;}intMain () {LL m,k; intx, y; Charch[5];  while(SCANF ("%d%i64d%i64d", &n,&m,&k)! =EOF) {        if(! N&AMP;&AMP;!M&AMP;&AMP;!K) Break; /*A.zero (); A.mat[0][0]=1;*/T.unit ();  for(intI=0; i<k; i++) {scanf ("%s", CH); if(ch[0]=='g') {scanf ("%d",&x); t.mat[0][x]++; }            Else if(ch[0]=='e') {scanf ("%d",&x);  for(intI=0; i<=n; i++) t.mat[i][x]=0; }            Else if(ch[0]=='s') {scanf ("%d%d",&x,&y);  for(intI=0; i<=n; i++) {swap (t.mat[i][x],t.mat[i][y]);//Interchange Columns} }} T=Quicklymod (t,m);/*Matrix ans = A * (T ^ m);        for (int i = 1; I <= n; i++) printf ("%lld", Ans.val[0][i]); printf ("\ n");*/printf ("%i64d", t.mat[0][1]);  for(intI=2; i<=n;i++) printf ("%i64d", t.mat[0][i]); printf ("\ n"); }    return 0;}/*3 1 6g 1g 2g 2s 1 2g 3e 0 0*/

Come on! Juvenile!!!

Training Little Cats (poj3735, Matrix 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.