S(k)=A^1+A^2...+A^k.

保利求解就超时了,我们考虑一下当k为偶数的情况,A^1+A^2+A^3+A^4...+A^k,取其中前一半A^1+A^2...A^k/2,后一半提取公共矩阵A^k/2后可以发现也是前一半A^1+A^2...A^k/2。因此我们可以考虑只算其中一半,然后A^k/2用矩阵快速幂处理。对于k为奇数,只要转化为k-1+A^k即可。n为矩阵数量,m为矩阵大小,复杂度O[(logn*logn)*m^3]

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#define LL long long
using namespace std; struct mx
{
    LL n, m;
    LL c[][];//需要根据题目开大
    void initMath(LL _n)//初始化方阵
    {
        m = n = _n;
    }
    void initOne(LL _n)//初始化单位矩阵
    {
        m = n = _n;
        for (LL i = ; i<n; i++)
            for (LL j = ; j<m; j++)
                c[i][j] = (i == j);
    }
    void print()//测试打印
    {
        for (LL i = ; i<n; i++)
        {
            for (LL j = ; j < m; j++)
            {
                cout << c[i][j];
                if (j != m - )cout << ' ';
            }
                
            cout << endl;
        }
    }
};
int mod = ;
mx Mut(mx a, mx b)
{
    mx c;
    c.n = a.n, c.m = b.m;
    for (LL i = ; i<a.n; i++)
        for (LL j = ; j<b.m; j++)
        {
            LL sum = ;
            for (LL k = ; k<b.n; k++)
                sum += a.c[i][k] * b.c[k][j], sum %= mod;
            c.c[i][j] = sum;
        }
    return c;
}
mx fastMi(mx a, LL b)
{
    mx mut; mut.initOne(a.n);
    while (b)
    {
        if (b % != )
            mut = Mut(mut, a);
        a = Mut(a, a);
        b /= ;
    }
    return mut;
}
LL n, k;
mx a, ans, b;
mx s(LL kx)
{
    if (kx == )
    {
        return a;
    }
    if (kx % ==)
    {
        mx p = s(kx / );
        mx y = fastMi(a, kx/);
        y = Mut(y,p);
        for (int i = ; i < n; i++)for (int j = ; j < n; j++)
        {
            y.c[i][j] += p.c[i][j];
            y.c[i][j] %= mod;
        }
        return y;
    }
    else
    {
        mx p = s(kx-);
        mx y = fastMi(a, kx);
        for (int i = ; i < n; i++)for (int j = ; j < n; j++)
        {
            y.c[i][j] += p.c[i][j];
            y.c[i][j] %= mod;
        }
        return y;
    }
}
int main(int argc, const char * argv[]) {
    cin.sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        b.initMath(n);
        ans.initMath(n);
        a.initMath(n);
        for(int i=;i<n;i++)
            for (int j = ; j < n; j++)
            {
                cin >> a.c[i][j];
            }
        ans = s(k);
        ans.print();
    }
    return ;
}

POJ-3233 Matrix Power Series 矩阵A^1+A^2+A^3...求和转化的更多相关文章

  1. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  2. poj 3233 Matrix Power Series(矩阵二分,高速幂)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15739   Accepted:  ...

  3. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

  4. poj 3233 Matrix Power Series 矩阵求和

    http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...

  5. POJ 3233 Matrix Power Series 矩阵快速幂

    设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...

  6. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  7. POJ 3233 Matrix Power Series(矩阵等比求和)

    题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...

  8. 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series

    poj 1575  Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...

  9. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

随机推荐

  1. 架构4(lvs lb集群解决方案二 lvs+keepalived)

    keepalived 1.实现调度器的HA 2.对realserver做健康检测 3.动态维护ipvs路由表

  2. etree导入问题

    原因:主要是lxml没有这个包的问题,需要安装下: 1.需要在https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 下选择你和你对应的pycharm对应的版 ...

  3. 初始化vue项目,报错This is probably not a problem with npm,there is likely additional logging output above

    https://blog.csdn.net/ink_if/article/details/79015811 参考别人的博客 初始化项目,vue init webpack-simple demo 然后n ...

  4. dubbo注册到zookeeper

    zk注册中心安装,参见dubbo官网:http://dubbo.apache.org/books/dubbo-admin-book/install/zookeeper.html provider.xm ...

  5. int 的重载

    测试代码: 结果: 分析: 首先创建两个对象同时进行初始化所以两次调用带参的构造函数: 其次在创建一个 对象然后将其等于前两个对象相加,这里由于该类没有重载+运算符而是重载了int 所以当两个对象相加 ...

  6. CentOS 7 MariaDB-MHA

    关于MHA    MHA(Master High Availability)是一款开源的mysql高可用程序,目前在mysql高可用方面是一个相对成熟的解决方案.MHA 搭建的前提是MySQL集群中已 ...

  7. Spring Boot 的 application.properties

    更改默认端口:8080 server.port = 8081 更改context-path :/server.context-path = /springboot #server.address= # ...

  8. MongoDB集群单mongos的问题总结

    问题发现 在使用过程中,通过spark访问集群的效率不是很令人满意,80核心同时运行的速度比单核心也就快了20倍左右,预测瓶颈在mongodb读写上.当然,此时没遇到其他问题暂时没进行问题梳理. 在数 ...

  9. 移动端热更新方案(iOS+Android)

    PPT资源包含iOS+Android 各种方案分析:https://github.com/qiyer/Share/blob/master/%E7%83%AD%E6%9B%B4%E6%96%B0%E5% ...

  10. TLS握手、中断恢复与证书中心的原因

    在双方都拿到随机数A.B.C后,将会使用这三个随机数生成一个对话密钥,然后使用该对话密钥进行对称加密通信,这种方式我们可以看到,安全性取决于随机数C的加密,前面的几个都是明文传的,这里就取决于服务器的 ...