题目链接  请猛戳~

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. db2事务日志已满解决办法

    查看事务日志配置(MICRO_11为数据库名称): db2 get db cfg for MICRO_11 运行结果: 日志文件大小(4KB)                         (LOG ...

  2. winform消息提示框

    摘自:http://www.cnblogs.com/risen/archive/2008/01/15/1039751.html public partial class AlertForm : For ...

  3. Python之机器学习-sklearn生成随机数据

    sklearn-生成随机数据 import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotli ...

  4. Django之Ajax提交

    Ajax 提交数据,页面不刷新 Ajax要引入jQuery Django之Ajax提交 Js实现页面的跳转: location.href = "/url/" $ajax({ url ...

  5. [bzoj1208][HNOI2004][宠物收养所] (平衡树)

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  6. npm 发包

    前几天封装了公用的locaStorage组件,当然封装后需要发布npm官网,于是摸索了一番终于搞定了,总结下来希望对大家有所帮助 npm安装的package一般支持下面几大类: 本地包 url远程包 ...

  7. JQuery常用的案例

    1.给导航栏添加鼠标移上去的时候变换背景颜色的方法. $(function () { $(".nav li").mouseover(function () { $(this).cs ...

  8. 关于oracle 压缩表

    这周客户的问题非常多,总是说我的数据不对.于是我对数据梳理了以后发现以前认为是重复数据的,其实并不是,而是我忽略了一个维度.那么这样一来,我们的周详单表就会有500多万的数据.一个月按照4周计算,就要 ...

  9. KSQL日期字段访问

    日期常量用法 KSQL中用日期常量必须用{ts'" + dateTime.ToString("yyyy-M-d HH:mm:ss") + "'} 正确写法: I ...

  10. NOIP2015提高组D1T3 斗地主

    问一副排n张,n<=23最少打几次打完,数据组数T<=100. 面向数据编程.. 前30分:乱暴力?没有顺子,把单.对子.炸弹.三张.王炸.三带一判一次即可. 前70分:状压,先预处理哪些 ...