zoukankan      html  css  js  c++  java
  • Training little cats_矩阵快速幂

    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

    【题意】有n只猫咪,每只猫咪有0花生,g x表示给第x只猫咪一颗花生,e x表示第x只猫咪把花生全吃了,s x y表示交换x和y 猫咪的花生数;

    将上述k次操作进行m次,求最后每只猫咪的花生数。

    【思路】由于m的数非常大,所以一般的方法肯定会Tel,所以用矩阵快速幂

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<string>
    #include<vector>
    #include<cstdlib>
    #include<map>
    using namespace std;
    const int N=110;
    long long int n,m,k;
    struct Mat
    {
        long long int mat[N][N];
        void clear()
        {
            memset(mat,0,sizeof(mat));
        }
        void init()
        {
            clear();
            for(int i=0;i<=n;i++)
            {
                mat[i][i]=1;
            }
        }
    };
    Mat a;
    Mat mul(Mat a,Mat b)//矩阵乘法
    {
        Mat c;
        c.clear();//刚开始把c.init()改了好久
        for(int i=0;i<=n;i++)
        {
            for(int k=0;k<=n;k++)
            {
                if(a.mat[i][k])
                {
                    for(int j=0;j<=n;j++)
                    {
                        c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                    }
                }
            }
        }
        return c;
    }
    Mat pow(Mat a,long long int m)//快速幂
    {
        if(m==1) return a;
        Mat c;
        c.init();
        while(m)
        {
            if(m&1) c=mul(c,a);
            m>>=1;
            a=mul(a,a);
        }
        return c;
    
    }
    
    int main()
    {
        while(~scanf("%lld%lld%lld",&n,&m,&k))
        {
            if(!n&& !m&& !k) break;
            a.init();
            while(k--)
            {
                long long int x,y;
                char op[10];
                scanf("%s",op);
                if(op[0]=='g')
                {
                    scanf("%lld",&x);
                    a.mat[0][x]++;
                }
                else if(op[0]=='e')
                {
                    scanf("%lld",&x);
                    for(int i=0;i<=n;i++)
                    {
                        a.mat[i][x]=0;
                    }
                }
                else
                {
                    scanf("%lld%lld",&x,&y);
                    for(int i=0;i<=n;i++)
                    {
                        swap(a.mat[i][x],a.mat[i][y]);
                    }
                }
            }
                if(m==0)
                {
                    printf("0");
                    for(int i=2;i<=n;i++)
                    {
                        printf(" 0");
                    }
                    printf("
    ");
                    continue;
                }
    
               a=pow(a,m);
               printf("%lld",a.mat[0][1]);
               for(int i=2;i<=n;i++)
               {
                   printf(" %lld",a.mat[0][i]);
               }
               printf("
    ");
    
    
        }
        return 0;
    }
  • 相关阅读:
    目录创建用什么命令?创建文件用什么命令?复制文件用什 么命令?
    哪些浏览器支持HTML 5?
    终端是哪个文件夹下的哪个文件?黑洞文件是哪个文件夹下 的哪个命令?
    什么是端到端微服务测试?
    HTML 5中的本地存储概念?
    HTML 5的页面结构和HTML 4或早先的HTML有什么不同?
    Spring 切面可以应用五种类型的通知?
    Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?
    微服务架构如何运作?
    双因素身份验证的凭据类型有哪些?
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5974849.html
Copyright © 2011-2022 走看看