POJ 3745 Training Little cats (Matrix operation + matrix fast power) __algorithm

Source: Internet
Author: User
Tags mul

POJ 3745 Training Little cats (Matrix operation + matrix fast power)
Time limit:2000ms Memory limit:65536kb

Description
Facer ' s pet cat just gave birth to a brood little of cats. has considered the health of those lovely cats, Facer decides to make the cats to do some. Facer has designed a set of moves for his cats. He's now asking you to supervise the cats to does 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.
The Cats perform a sequence of these moves and must repeat it m times! Poor cats! Only facer can come up and such embarrassing idea.
You are have to determine the final number of peanuts each cat have and directly give them the exact 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 are given firstly, where N was 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 single line, representing the numbers of peanuts the cats have.

Sample Input

3 1 6
G 1
G 2
G 2
S 1 2
G 3
E 2
0 0 0

Sample Output

2 0 1

Source
PKU Campus 2009 (POJ monthly contest–2009.05.17), Facer

First notice that the number of repetitions is very large, so there must be a mathematical algorithm.
Look at the operation, all linear operation, so you can use the linear transformation of vectors to handle these operations, the only problem is that there is a plus one of the operation, so the peanut quantity vector augmentation, add a non second dimension can be.
Then for X=[1,X1,X2,..., Xn]t,xi as the first cat peanut number x=[1,x_1,x_2,..., x_n]^t,x_i as the first cat peanut number
X=am[1,0,0...,0]t x=a^m[1,0,0...,0]^t
A can be obtained by converting a simple linear transformation into a matrix operation.
Use a fast power again.

Accepted    3840kB  812ms   1824 B  g++
#include <stdio.h> const int n=101;

const int POWER=50;

typedef long Long data_t;
data_t A[n][n];
data_t Ap[power][n][n];
/* x ' =ax x[0] is always 1, x[i] is the Peanuts cat I owns.
*/void CopyTo (data_t a[n][n],data_t b[n][n],int N);
void Mul (data_t a[n][n],data_t b[n][n],int N);
void Power (data_t a[n][n],int n,int m);

void Init (data_t a[n][n],int N);
    int main () {//freopen ("In.txt", "R", stdin);
    char cmd;
    int n,m,k,x,y,temp;
        while (scanf ("%d%d%d\n", &n,&m,&k) &&n) {init (a,n);
            while (k--) {scanf ("%c", &cmd);
                if (cmd== ' E ') {scanf ("%d\n", &x); 
            for (int i=0;i<=n;i++) a[x][i]=0;
                else if (cmd== ' s ') {scanf ("%d%d\n", &x,&y);
                    for (int i=0;i<=n;i++) {temp=a[x][i]; A[x][i]=a[y][i]; 
                A[y][i]=temp;
                } else if (cmd== ' G ') {scanf ("%d\n", &x); 
            a[x][0]++;
        } power (A,N,M); for (int i=1;i<=n;i++) printf ("%lld%c", a[i][0],i==n?)
    \ n ': ');
return 0;
    } void Mul (data_t a[n][n],data_t b[n][n],int N) {static data_t a0[n][n];
    for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) A0[I][J]=A[I][J];
            for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) {a[i][j]=0;
        for (int k=0;k<=n;k++) A[I][J]+=A0[I][K]*B[K][J];
} return;
    } void Power (data_t a[n][n],int n,int m) {int = =;
    data_t power=1;
    CopyTo (A,ap[0],n);
        while (power< (data_t) m) {power*=2;
        p++;
        CopyTo (Ap[p-1],ap[p],n);
    Mul (Ap[p],ap[p-1],n);
    Init (a,n); for (int i=0;i<=p;i++) if (m& (1<<i)) Mul (a,ap[i],n);
Return
            } void CopyTo (data_t a[n][n],data_t b[n][n],int N) {for (int i=0;i<=n;i++) for (int j=0;j<=n;j++)
    B[I][J]=A[I][J];
Return } void Init (data_t a[n][n],int N) {for (int i=0;i<=n;i++) for (int j=i+1;j<=n;j++) A[I][J]
    =a[j][i]= (data_t) 0;
    for (int i=0;i<=n;i++) a[i][i]= (data_t) 1;
Return }

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.