题目链接  请猛戳~

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

结题思路:

当k为偶数时,s(k)=(1+A^(k/2))*(A+A^2+…+A^(k/2))

 当k为奇数时,s(k)=A+(A+A^(k/2+1))*(A+A^2+…+A^(k/2))加以递归求解
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
const int MAX = 32;
struct Matrix{
int v[MAX][MAX];
};
Matrix E;//定义单位矩阵 E
int n,k,M;
Matrix add(Matrix a,Matrix b){//矩阵加法运算
Matrix c;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
c.v[i][j] = (a.v[i][j] + b.v[i][j]) % M;
}
return c;
}
Matrix mul(Matrix a,Matrix b){//矩阵乘法运算
Matrix c;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
c.v[i][j] = 0;
for(int k = 1;k <= n;k++)
c.v[i][j] = (a.v[i][k] * b.v[k][j] + c.v[i][j]) % M;
}
return c;
}
Matrix pow(Matrix a,int k){//矩阵幂运算
if(k == 0){
memset(a.v,0,sizeof(a.v));
for(int i = 1;i <= n;i++)
a.v[i][i] = 1;
return a;
}else if(k == 1) return a;
Matrix c = pow(a,k / 2);
if(k & 1){
return mul(a,mul(c,c));
}else
return mul(c,c);
}
Matrix sum(Matrix a,int k){ //连续升幂求和
if(k == 1)return a;
Matrix b = pow(a,(k + 1) / 2);
Matrix c = sum(a,k / 2);
if(k % 2){
return add(a,mul(add(a,b),c));
}else
return mul(add(E,b),c);
}
void initE(){ //初始化单位矩阵E
memset(E.v,0,sizeof(E.v));
for(int i = 1;i <= n;i++) E.v[i][i] = 1;
}
void print(Matrix a){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++)
printf("%d ",a.v[i][j]);
puts("");
}
}
Matrix get(){
Matrix a;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)scanf(" %d",&a.v[i][j]);
return a;
}
int main()
{ while(~scanf("%d%d%d",&n,&k,&M)){
initE();
Matrix a = get();
Matrix b = sum(a,k);
print(b);
}
return 0;
}

Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板的更多相关文章

  1. 矩阵儿快速幂 - POJ 3233 矩阵力量系列

    不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...

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

    题目链接 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A^2 + A^3 + - ...

  3. 题解报告:poj 3233 Matrix Power Series(矩阵快速幂)

    题目链接:http://poj.org/problem?id=3233 Description Given a n × n matrix A and a positive integer k, fin ...

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

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

  5. POJ 3233 Matrix Power Series——快速幂&&等比&&分治

    题目 给定一个 $n \times n$  的矩阵 $A$ 和正整数 $k$ 和 $m$.求矩阵 $A$ 的幂的和. $$S = A + A^2 + ... + A^k$$ 输出 $S$ 的各个元素对 ...

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

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

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

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

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

  9. POJ - 3233 矩阵套矩阵

    题意:给你矩阵\(A\),求\(S=\sum_{i=1}^{k}A^i\) 构造矩阵 \[ \begin{bmatrix} A & E \\ 0 & E\\ \end{bmatrix} ...

随机推荐

  1. eclipse之版本代号

  2. 配置python3 项目环境

    安装python3 安装仓库软件 sudo apt-get install software-properties-common python-software-properties 添加仓库 sud ...

  3. Python 3安装体验篇(win10)

    一.下载 1.打开官网https://www.python.org/downloads/windows/,点击Python 3版本链接 2.点击win10 64位安装链接,即可下载Python安装 二 ...

  4. python while、continue、break

    while循环实现用户登录 _user = "tom" _passwd = "abc123" counter = 0 while counter < 3: ...

  5. LeetCode(48)Rotate Image

    题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...

  6. 洛谷 3871 [TJOI2010]中位数

    [题解] 平衡树模板题,不过因为可以离线,所以有别的做法.把询问倒着做,变成删掉数字.求中位数,于是可以二分+树状数组. #include<cstdio> #include<cstr ...

  7. RS232

    RS232的最大的传输速率大约10KBytes/s. 全双工工作方式,异步.数据是8位作为一块来发送的,先发送最低位,最后发送最高位. 在232通信中: Both side of the cable ...

  8. 【Tomcat】tomcat logs 目录下各日志文件的含义

      tomcat每次启动时,自动在logs目录下生产以下日志文件,按照日期自动备份.可以帮助我们更好的找出错误.   一. 认识各种目录的作用及记录的信息 目录

  9. apple网址

    https://developer.apple.com/downloads/index.action#   开发工具下载

  10. Nginx教程收集

    学习要系统,最推荐的方式是看书. 下面是收集的一些Nginx教程: https://www.gitbook.com/book/yinsigan/nginx/details http://www.ngi ...