• POJ 3735 Training little cats 矩阵快速幂


    Training little cats
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 13446   Accepted: 3328

    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

      题目大意就是有三个操作, 给i 号小猫花生, 让 i 号小猫吃掉花生, 交换 i, j 号小猫的食物。 一系列的命令执行m 个循环, 问最后每个小猫的花生有多少。

      拿到这道题就知道是矩阵快速幂, 因为要循环m 次 且m 非常的大, 难点就是怎么构造这个矩阵。

       

      构造n+1 行列 的增广矩阵最后一列记录花生的数量, 行号代表每个小猫的编号, 最后输出的也是 dp[i][n+1] i 代表第i号小猫。

      输入,如果是g 的话, 对应的i号小猫增加一个花生, 如果e 的话, 则第i行全部清零, s i j 则交换 i , j 两行。 

      接下来就是矩阵快速幂, 说实话这里还有一个坑:

      如果你这么写的话:

      

      for (int i=0;i<N;i++)

        for (int j=0;j<N;j++)

          for (int k=0;k<N;k++)

            a[i][j]+=b[i][k]*c[k][j];

     

      会TLE, 因为这么写的话对稀疏矩阵是十分慢的, 所以对于稀疏矩阵有这么一个优化:

     

      

      for (int i=0;i<N;i++)

        for (int j=0;j<N;j++)

          if (b[i][j])

            for (int k=0;k<N;k++)

              a[i][k]+=b[i][j]*c[j][k];

      这么写就不再是一次确定结果矩阵的一个元素, 而是一遍一遍的加, 当发现b 矩阵有一个零则跳过

     

      

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    typedef long long LL;
    struct Matrix{
        LL a[130][130];
        int n, m;
        void clear() {
            n = m = 0;
            memset( a, 0, sizeof(a) );
        }
        Matrix operator * ( Matrix b ) {
            Matrix res;
            res.clear();
            res.n = n;
            res.m = b.m;
            for( int i = 1; i <= n+1; i++ ) {
                for( int j = 1; j <= n+1; j++ ) {
                    if( a[i][j] ) {
                        for( int k = 1; k <= b.m; k++ ) {
                            res.a[i][k] += a[i][j] * b.a[j][k];
                        }
                    }
                }
            }
            return res;
        }
    };
    
    Matrix quick_power( Matrix a, LL m ) {
        Matrix res;
        res.clear();
        res.n = res.m = a.n;
        for( int i = 1; i <= res.n+1; i++ ) {
            for( int j = 1; j <= res.n+1; j++ ) {
                if( i == j ) res.a[i][j] = 1;
            }
        }
        while( m ) {
            if( m & 1 ) res = res * a;
            m >>= 1;
            a = a * a;
        }
        return res;
    }
    
    int main() {
        int n, k;
        LL m;
        while( ~scanf( "%d%lld%d", &n, &m, &k ) && (n + m + k)) {
            Matrix A;
            A.clear();
            A.n = n+1;
            A.m = n+1;
            for( int i = 1; i <= n+1; i++ ) {
                for( int j = 1; j <= n+1; j++ ) {
                    if( i == j ) A.a[i][j] = 1;
                }
            }
            char s[10];
            
            while( k-- ) {
                
                scanf( "%s", s );
                if( s[0] == 'g' ) {
                    int x;
                    scanf( "%d", &x );
                    A.a[x][n+1]++;
                }
                if( s[0] == 'e' ) {
                    int x;
                    scanf( "%d", &x );
                    for( int j = 1; j <= n+1; j++ ) {
                        A.a[x][j] = 0;
                    }
                }
                if( s[0] == 's' ) {
                    int x, y;
                    scanf( "%d%d", &x, &y );
                    for( int j = 1; j <= n+1; j++ ) {
                        LL temp = A.a[x][j];
                        A.a[x][j] = A.a[y][j];
                        A.a[y][j] = temp;
                    }
                }
            }
            
            Matrix res = quick_power( A, m );
            for( int i = 1; i <= n; i++ ) {
                if( i == 1 ) printf( "%lld", res.a[i][n+1] );
                else printf( " %lld", res.a[i][n+1] );
            }
            printf( "\n" );
        }
        return 0;
    }
    View Code

      果然学计算机必须学好数学!

  • 相关阅读:
    GRIDVIEW导出到EXCEL
    数据表示:字节 高位低位
    js学习笔记0
    12奇招,循序删除顽固的文件
    加快开关机速度
    PHP正则表达式的快速学习
    firefox下height不能自动撑开的解决办法
    给MySQL加密(也适用于Wamp5中)
    我的电脑创建资源管理器
    css 圆角7种CSS圆角框解决方案
  • 原文地址:https://www.cnblogs.com/FriskyPuppy/p/5938112.html
Copyright © 2020-2023  润新知