• 原题如下:

    Matrix Power Series
    Time Limit: 3000MS   Memory Limit: 131072K
    Total Submissions: 28044   Accepted: 11440

    Description

    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
  • 题解:构造矩阵:

    此时,令Sk=I+A+…+Ak-1,则有:

    通过计算这个矩阵的k次幂,就可求出A的累乘和,时间复杂度为O(n3logk)

  • 代码:
     #include <cstdio>
    #include <cctype>
    #define number s-'0'
    #include <cstring>
    #include <vector> using namespace std; typedef vector<int> vec;
    typedef vector<vec> mat; int n,k,m;
    mat A; void read(int &x)
    {
    char s;
    x=;
    bool flag=;
    while (!isdigit(s=getchar()))
    (s=='-')&&(flag=true);
    for (x=number; isdigit(s=getchar());x=x*+number);
    (flag)&&(x=-x);
    } void write(int x)
    {
    if (x<)
    {
    putchar('-');
    x=-x;
    }
    if (x>) write(x/);
    putchar(x%+'');
    } mat mul(mat &A, mat &B)
    {
    mat C(A.size(), vec(B[].size()));
    for (int i=; i<A.size(); i++)
    {
    for (int j=; j<B[].size(); j++)
    {
    for (int k=; k<B.size(); k++)
    {
    C[i][j]=(C[i][j]+A[i][k]*B[k][j])%m;
    }
    }
    }
    return C;
    } mat pow(mat A, int n)
    {
    mat B(A.size(), vec(A.size()));
    for (int i=; i<A.size(); i++) B[i][i]=;
    while (n>)
    {
    if (n&) B=mul(B, A);
    A=mul(A, A);
    n>>=;
    }
    return B;
    } int main(int argc, char * argv[])
    {
    read(n);read(k);read(m);
    A=mat (n,vec(n));
    mat B(n*, vec(n*));
    for (int i=; i<n; i++)
    {
    for (int j=; j<n; j++)
    {
    read(A[i][j]);
    B[i][j]=A[i][j];
    }
    B[n+i][i]=B[n+i][n+i]=;
    }
    B=pow(B,k+);
    for (int i=; i<n; i++)
    {
    for (int j=; j<n; j++)
    {
    int a=B[n+i][j]%m;
    if (i==j) a=(a+m-)%m;
    printf("%d%c", a, j+==n?'\n':' ');
    }
    }
    }

Matrix Power Series(POJ 3233)的更多相关文章

  1. Matrix Power Series POJ - 3233 矩阵幂次之和。

    矩阵幂次之和. 自己想着想着就想到了一个解法,但是还没提交,因为POJ崩了,做了一个FIB的前n项和,也是用了这个方法,AC了,相信是可以得. 提交了,是AC的 http://poj.org/prob ...

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

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

  3. 矩阵十点【两】 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的迹(就是主对角线上各项的 ...

  4. POJ 3233 Matrix Power Series (矩阵乘法)

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

  5. [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)

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

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

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

  7. 线性代数(矩阵乘法):POJ 3233 Matrix Power Series

    Matrix Power Series   Description Given a n × n matrix A and a positive integer k, find the sum S = ...

  8. POJ 3233 Matrix Power Series(二分等比求和)

    Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...

  9. POJ 3233 Matrix Power Series(矩阵快速幂)

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

随机推荐

  1. 设计模式:装饰者模式介绍及代码示例 && JDK里关于装饰者模式的应用

    0.背景 来看一个项目需求:咖啡订购项目. 咖啡种类有很多:美式.摩卡.意大利浓咖啡: 咖啡加料:牛奶.豆浆.可可. 要求是,扩展新的咖啡种类的时候,能够方便维护,不同种类的咖啡需要快速计算多少钱,客 ...

  2. PYTHON-anaconda-安装

    1.下载: 官网地址:https://www.anaconda.com/products/individual/get-started 镜像地址(推荐):https://mirrors.tuna.ts ...

  3. 【算法•日更•第三十期】区间动态规划:洛谷P4170 [CQOI2007]涂色题解

    废话不多说,直接上题:  P4170 [CQOI2007]涂色 题目描述 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符 ...

  4. Nginx同时支持Http和Https

    现在的网站支持Https几乎是标配功能,Nginx能很好的支持Https功能.下面列举一个配置同时支持Http和Https的功能. 需要注意的是:既然选择使用Https,就是为了保证通信安全,那么就没 ...

  5. Centos7修改用户名

    系统原来的用户lou,改为scrapy,要改以下个地方,注:没有修改对应密码 1.   # vi /etc/passwd 修改其中的用户名部分.用户组部分.主目录部分2.  修改用户组的配置文件 # ...

  6. Jmeter 常用函数(28)- 详解 __FileToString

    如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 读取整个文件 语法格式 ${__Fil ...

  7. Python 抓包程序(pypcap)

    #/usr/bin/env python #-*-coding:utf8-*- #抓包脚本 """ This script is used to Capture and ...

  8. Java 8新特性(四):新的时间和日期API

    Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...

  9. 《Java从入门到失业》第三章:基础语法及基本程序结构(四):基本数据类型(字符编码和char型)

    3.6.4字符编码 咦?怎么好像有东西乱入了?不是讲基本数据类型么?哈哈,因为还剩下最后一个char型了,因为char型会牵涉到Unicode编码相关,因此我决定先科普一下字符集编码. 我儿子现在上小 ...

  10. Java二进制和位运算,这一万字准能喂饱你

    基础不牢,地动山摇.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习.关注公众号[BAT的乌托 ...