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;
    }
  • 相关阅读:
    Enterprise Library Step By Step系列(一):配置应用程序块——入门篇
    Enterprise Library Step By Step系列(八):日志和监测应用程序块——进阶篇
    在ASP.NET页面中冻结DataGrid的列或头部
    数据库设计技巧系列(五)——各种小技巧
    用任务计划实现数据库的异地备份
    如何更好的与人沟通?[图]
    在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息
    SQL Server 2012 Express LocalDB
    Clay: 创建和使用深层次对象图
    VS 2012 的 单元测试 和 测试资源管理器
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5974849.html
Copyright © 2011-2022 走看看