zoukankan      html  css  js  c++  java
  • poj 3233 Matrix Power Series(矩阵快速幂+二分)

    Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.

    Input

    The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n lines each containing n nonnegative integers below 32,768, giving A’s elements in row-major order.

    Output

    Output the elements of S modulo m in the same way as A is given.

    Sample Input

    2 2 4
    0 1
    1 1

    Sample Output

    1 2
    2 3

    二分法:

    需要注意当n为偶数和奇数的时候。
    Talk is cheap.just show the code!
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define LL long long
    #define m(s) memset(s,0,sizeof(s))
    struct mat
    {
        int a[40][40];
    }d;
    int n,m,k,mod;
    
    mat mul(mat a,mat b)//矩阵相乘
    {
        mat ret;
        m(ret.a);
        for(int i=0;i<n;i++)
            for(int k=0;k<n;k++)
                 {
                     if(a.a[i][k])
                     {
                         for(int j=0;j<n;j++)
                         {
                             ret.a[i][j]+=a.a[i][k]*b.a[k][j];
                             if(ret.a[i][j]>=mod)
                                 ret.a[i][j]%=mod;
                         }
                     }
                 }
        return ret;
    }
    
    mat expo(mat a,int k)//矩阵快速幂
    {
        if(k==1)
            return a;
        mat e;
        m(e.a);
        for(int i=0;i<n;i++)
            e.a[i][i]=1;
        if(k==0)
            return  e;
        while(k)
        {
            if(k&1)
                e=mul(a,e);
            a=mul(a,a);
            k>>=1;
        }
        return e;
    }
    
    mat add(mat a,mat b)//矩阵相加
    {
        mat t;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                t.a[i][j]=(a.a[i][j]+b.a[i][j])%mod;
            return t;
    }
    mat sum(int k)//矩阵求和
    {
        if(k==1)
            return d;
        if(k&1)
            return add(sum(k-1),expo(d,k));
        else
        {
            mat tmp=sum(k>>1);
            return add(tmp,mul(tmp,expo(d,k>>1)));
        }
    }
    int main()
    {
        while(cin>>n>>k>>mod)
        {
            mat ans;
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                {
                    cin>>d.a[i][j];
                    if(d.a[i][j]>=m)
                        d.a[i][j]%=mod;
                }
            ans=sum(k);
            for(int i=0;i<n;i++)
            {    
                for(int j=0;j<n;j++)
                {
                    if(j)
                        cout<<" ";
                    cout<<ans.a[i][j];
                }
                cout<<endl;
            }
        }
        return 0;
    }


  • 相关阅读:
    关于yyyy-MM-dd格式日期字符串,解析成LocalDateTime遇到的问题
    idea设置代码提示不区分大小写
    不错的Django技术网址
    Django-Rest-Framework 教程: 快速入门
    Djanto static静态文件配置
    Html5 touch event
    Zepto.js touch模块深入分析
    Python单元测试框架之pytest -- fixtures
    Python:高级主题之(属性取值和赋值过程、属性描述符、装饰器)
    Nginx基本配置、性能优化指南
  • 原文地址:https://www.cnblogs.com/orion7/p/7238234.html
Copyright © 2011-2022 走看看