Training Little Cats -- 矩阵乘法

来源:互联网 发布:java数组int增删改查 编辑:程序博客网 时间:2024/05/19 23:54

poj 3745
Time Limit: 2000ms Memory Limit: 65536kB

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

解题思路:

猫咪的花生的向量A:

1 0 0 0

初始方阵T,通过变化要求要把T变成变化的方阵

1 0 0 00 1 0 00 0 1 00 0 0 1

定义 R = A * T,那么R[i]表示第i只猫的最终花生数量。上面向量A的第一个1就是为了取矩阵T第1行各个值的。

那么

g i := ++T[0][i]

e i := set T[x][i] = 0 即将矩阵的第i列全部置为0

s i j := swap(T[x][i], T[x][j]) 即将矩阵的i列和j列互换。

T执行m次,结果R = A * Tm 。
这里写图片描述

参考了别人的代码:

#include <iostream>#include <cstring>#include <cstdio>using namespace std;#define LL long long#define MAX_N 100 + 16struct Matrix{    LL val[MAX_N][MAX_N];    int height, width;    Matrix(){}    Matrix(int height, int width) : height(height), width(width)    {        zero();    }    void zero()    {        memset(val, 0, sizeof(val));    }    void unit()    {        zero();        height = width = max(height, width);        for (int i = 0; i < height; ++i)        {            val[i][i] = 1;        }    }    Matrix operator*(const Matrix &m)    {        Matrix ret(this->height, m.width);        for (int i = 0; i < this->height; ++i) //i: A从上到下,从第一行,到最后一行        {            for (int k = 0; k < this->width; ++k) //k: 对于A的第i行的每一个,从左到右,从第一个,到最后一个            {                if (this->val[i][k]) // 判断val[i][k]如果为0就跳过:应付稀疏矩阵                {                    for (int j = 0; j < m.width; ++j) //A[i][k] 与 T[k][j]相乘                    {                        ret.val[i][j] += this->val[i][k] * m.val[k][j];                    }                }            }        }        return ret;    }    Matrix operator^(const LL k)// 重复k次    {        Matrix ret = create_identity((*this).width), base = (*this);        for (LL e = k; e; e >>= 1)        {            if (e & 1)            {                ret = ret * base;            }            base = base * base;        }        return ret;    }    void resize(int width, int height)    {        this->width = width;        this->height = height;    }    static Matrix create_identity(int size)    {        Matrix ret(size, size);        for (int i = 0; i < size; ++i)        {            ret.val[i][i] = 1;        }        return ret;    }}A, T;int main(){    int a, b;    int n, m, k;    while (scanf("%d%d%d", &n, &m, &k) && n)    {        if(n==0 && m==0 && k ==0) break;        A.resize(n + 1, 1);        A.zero();        A.val[0][0] = 1;        T.resize(n + 1, n + 1);        T.unit();        for (int i = 0; i < k; ++i)        {            getchar();            char cmd = getchar();            if (cmd == 'g')            {                scanf("%d", &a);                ++T.val[0][a];            }            else if (cmd == 'e') //第a列的每一个变成0            {                scanf("%d", &a);                for (int i = 0; i <= n; ++i)                {                    T.val[i][a] = 0;                }            }            else            {                scanf("%d%d", &a, &b); // 把第a列与第b列进行交换                for (int i = 0; i <= n; ++i)                {                    swap(T.val[i][a], T.val[i][b]);                }            }        }        Matrix R = A * (T ^ m);        for (int i = 1; i <= n; i++)        {            printf("%lld ", R.val[0][i]);        }        puts("");    }    return 0;}