[POJ 3735] Training little cats (构造矩阵、矩阵快速幂)

19 篇文章 0 订阅
5 篇文章 0 订阅
Training little cats
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9613 Accepted: 2296

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 nm 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

Source


题目大意:
有N只猫,K次操作(得花生、吃花生、交换花生),重复M次。问最后每只猫各有多少花生剩余。

解题思路:
这道题目的巧妙之处就是构造单位矩阵,从而实现三种操作。


之后便能用矩阵快速幂了。

另外还有两个trick。
1. 因为矩阵的乘法是O(N^3)。复杂度高。这道题直接搞会T。而我们可以发现,矩阵乘法都是稀疏矩阵,稀疏矩阵乘法有优化。
/*
    稀疏矩阵乘法优化
*/
for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
        if (!a[i][j]) continue;  //稀疏矩阵乘法优化
        for (int k = 0; k < b.m; k++)
            tmp.a[i][k] += a[i][j] * b.a[j][k];
    }

2. 虽然M,N,K都是 int 范围的,但是操作之后,每只猫的花生数量会超 int。

代码:
/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
const int maxn = 110;
const int maxm = 110;
const int mod = 10000;
struct Matrix {
    int n, m;
    ll a[maxn][maxm];
    void clear() {
        n = m = 0;
        memset(a, 0, sizeof(a));
    }
    Matrix operator * (const Matrix &b) const {
        Matrix tmp;
        tmp.clear();
        tmp.n = n; tmp.m = b.m;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                if (!a[i][j]) continue;  //稀疏矩阵乘法优化
                for (int k = 0; k < b.m; k++)
                    tmp.a[i][k] += a[i][j] * b.a[j][k];
            }
        return tmp;
    }
};
int n, k, m;
void init(int n, Matrix &I) {
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
            I.a[i][j] = 0LL;
        }
        I.a[i][i] = 1LL;
    }
    I.n = I.m = n + 1;

}
Matrix Matrix_pow(Matrix A, int k) {
    Matrix res;
    init(n, res);
    while(k) {
        if (k & 1) res = res * A;
        k >>= 1;
        A = A * A;
    }
    return res;
}
int main () {
    Matrix A, I, res;
    while(scanf("%d%d%d", &n, &m, &k)) {
        if (m == 0 && n == 0 && k == 0) {
            break;
        }
        A.clear();
        A.n = n + 1; A.m = 1;
        A.a[n][0] = 1LL;
        char ch;
        int u, v;
        init(n, res);
        while(k--) {
            init(n, I);
            getchar();
            scanf("%c", &ch);
            if (ch == 'g') {
                scanf("%d", &u);
                I.a[u - 1][n] = 1LL;
            }
            if (ch == 's') {
                scanf("%d%d", &u, &v);
                I.a[u - 1][u - 1] = 0LL; I.a[u - 1][v - 1] = 1LL;
                I.a[v - 1][v - 1] = 0LL; I.a[v - 1][u - 1] = 1LL;
            }
            if (ch == 'e') {
                scanf("%d", &u);
                I.a[u - 1][u - 1] = 0LL;
            }
            res = I * res;  //每次操作时,I应该乘在前面,因为矩阵乘法没有交换律
        }
        Matrix tmp = Matrix_pow(res, m);
        tmp = tmp * A;
        for (int i = 0; i < n; i++) {
            printf("%lld ", tmp.a[i][0]);
        }
        cout<<endl;
    }
    return 0;
}

上述图片来自: http://blog.csdn.net/magicnumber/article/details/6217602
以下是Java解决POJ3233—矩阵幂序列问题的代码和解释: ```java import java.util.Scanner; public class Main { static int n, k, m; static int[][] A, E; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); k = sc.nextInt(); m = sc.nextInt(); A = new int[n][n]; E = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A[i][j] = sc.nextInt() % m; E[i][j] = (i == j) ? 1 : 0; } } int[][] res = matrixPow(A, k); int[][] ans = matrixAdd(res, E); printMatrix(ans); } // 矩阵乘法 public static int[][] matrixMul(int[][] a, int[][] b) { int[][] c = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % m; } } } return c; } // 矩阵快速幂 public static int[][] matrixPow(int[][] a, int b) { int[][] res = E; while (b > 0) { if ((b & 1) == 1) { res = matrixMul(res, a); } a = matrixMul(a, a); b >>= 1; } return res; } // 矩阵加法 public static int[][] matrixAdd(int[][] a, int[][] b) { int[][] c = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { c[i][j] = (a[i][j] + b[i][j]) % m; } } return c; } // 输出矩阵 public static void printMatrix(int[][] a) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } } ``` 解释: 1. 首先读入输入的n、k、m和矩阵A,同时初始化单位矩阵E。 2. 然后调用matrixPow函数求出A的k次幂矩阵res。 3. 最后将res和E相加得到结果ans,并输出。 4. matrixMul函数实现矩阵乘法,matrixPow函数实现矩阵快速幂,matrixAdd函数实现矩阵加法,printMatrix函数实现输出矩阵
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值