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. Improved GAN

    https://www.bilibili.com/video/av9770302/?p=16 从之前讲的basic gan延伸到unified framework,到WGAN 再到通过WGAN进行Ge ...

  2. jmeter将JDBC Request查询出的数据作为下一个接口的参数

    现在有一个需求,从数据库tieba_info表查出rank小于某个值的username和count(*),然后把所有查出来的username和count(*)作为参数值,用于下一个接口. tieba_ ...

  3. spss缺失值填充步骤

    缺失值填充是数据预处理最基本的步骤,一般能想到的是固定值填充(均值等统计学方法).根据与本列有相关关系的列函数表示来填充.这次我用的是em算法进行填充,具体原理后续补充. 主要记录一下步骤: 工具栏: ...

  4. Error: Cannot find module 'babel-helpers'

    cnpm install babel-core babel-loader babel-plugin-transform-runtime -D cnpm install babel-preset-env ...

  5. MongoDB 目录

    MongoDB 介绍 centos7.6 安装与配置 MongoDB yum方式 MongoDB 数据库操作 MongoDB 用户管理 MongoDB 新建数据库和集合 查询集合 MongoDB 增删 ...

  6. java框架之SpringBoot(14)-任务

    使用 maven 创建 SpringBoot 项目,引入 Web 场景启动器. 异步任务 1.编写异步服务类,注册到 IoC 容器: package zze.springboot.task.servi ...

  7. java框架之SpringBoot(16)-分布式及整合Dubbo

    前言 分布式应用 在分布式系统中,国内常用 Zookeeper + Dubbo 组合,而 SpringBoot 推荐使用 Spring 提供的分布式一站式解决方案 Spring + SpringBoo ...

  8. python数据结构-如何根据字典中值的大小对字典项排序

    如何根据字典中值的大小对字典项排序 问题举例 某班英语成绩以字典形式存储,如何根据成绩高低,计算学生成绩排名 { “tom”:80, "lily":88, "marton ...

  9. [PHP] swoole在daemonize模式下,chdir失效问题

    swoole version: 1.9.6 其实跟swoole的版本无关,因为原代码体系,fpm模式下,在启动的时候,是使用 chdir 函数改变了当前目录的,而其它代码在做类的自动加载的时候,都是写 ...

  10. 再解炸弹人,dfs&bfs

    输入样例: 13 13 3 3##############GG.GGG#GGG.####.#G#G#G#G##.......#..G##G#.###.#G#G##GG.GGG.#.GG##G#.#G# ...