POJ - 3735 Training little cats

Training little cats time limit per test 2 seconds memory limit per test 256 megabytes


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 integers n, m and k are 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 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

The last time I did the topic of matrix fast power was about half a year ago.... Yesterday, I decided to reopen this topic (fake) review (true preview) under the reminder of senior Ziyang.

I don't know if I don't do it... I really don't know how to do it.... The first reaction in my brain when I saw this question was - line segment tree! However... this is not an interval update and there is no need to use a segment tree (≡ω≡.)

I have been wa for seven rounds to pass this question, and finally I found that the EOF requirement in the question is ending with three zeroes "0 0 0". Then I wrote n&&m&&k, so m can actually be 0, but I break here. After ╮(╯_╰)╭, just change it to && (m+n+k), or just declare break directly... In the future, you can't take some "smarts" for convenience when writing questions.

Below is the code, bare bare matrix fast power.
Here, the n+1 row unit matrix is ​​used to represent the val (peanut) change of n units. There is not much explanation. The knowledge of linear algebra, the m-round operation is directly multiplied by the matrix.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll n,m,k;
struct Matrix
{
    ll mi[150][150];
};

Matrix a;

Matrix Mul(Matrix a,Matrix b)
{
    Matrix c;
    memset(c.mi,0,sizeof(c.mi));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(a.mi[i][j]==0)
                continue;
            else
            {
                for(int k=1;k<=n;k++)
                {
                    if(b.mi[j][k]==0)
                        continue;
                    c.mi[i][k]+=a.mi[i][j]*b.mi[j][k];
                }
            }
        }
    }
    return c;
}

Matrix qic_pow(ll m)
{
    Matrix unit;
    memset(unit.mi,0,sizeof(unit.mi));
    for(int i=1;i<=n;i++)
        unit.mi[i][i]=1;
    while(m)
    {
        if(m&1)
            unit=Mul(unit,a);
        a=Mul(a,a);
        m/=2;
    }
    return unit;
}

int main()
{
    while(scanf("%lld%lld%lld",&n,&m,&k)!=EOF&&(m+n+k))
    {
        n++;
        memset(a.mi,0,sizeof(a.mi));
        for(int i=1;i<=n;i++)
            a.mi[i][i]=1;
        char s[5];
        while(k--)
        {
            scanf("%s",s);
            if(s[0]=='g')
            {
                ll x;
                scanf("%lld",&x);
                a.mi[x][n]++;
            }
            if(s[0]=='e')
            {
                ll x;
                scanf("%lld",&x);
                for(int i=1;i<=n;i++)
                    a.mi[x][i]=0;
            }
            if(s[0]=='s')
            {
                ll x,y;
                scanf("%lld%lld",&x,&y);
                ll temp;
                for(int i=1;i<=n;i++)
                {
                    swap(a.mi[x][i],a.mi[y][i]);
                }
            }
        }
        Matrix ans=qic_pow(m);
        for(int i=1;i<n;i++)
        {
            if(i!=n-1)
                printf("%lld ",ans.mi[i][n]);
            else
                printf("%lld\n",ans.mi[i][n]);
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325714772&siteId=291194637