Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板
题目链接 请猛戳~
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 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板的更多相关文章
- 矩阵儿快速幂 - POJ 3233 矩阵力量系列
不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...
- 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 + - ...
- 题解报告:poj 3233 Matrix Power Series(矩阵快速幂)
题目链接:http://poj.org/problem?id=3233 Description Given a n × n matrix A and a positive integer k, fin ...
- Matrix Power Series POJ - 3233 矩阵幂次之和。
矩阵幂次之和. 自己想着想着就想到了一个解法,但是还没提交,因为POJ崩了,做了一个FIB的前n项和,也是用了这个方法,AC了,相信是可以得. 提交了,是AC的 http://poj.org/prob ...
- POJ 3233 Matrix Power Series——快速幂&&等比&&分治
题目 给定一个 $n \times n$ 的矩阵 $A$ 和正整数 $k$ 和 $m$.求矩阵 $A$ 的幂的和. $$S = A + A^2 + ... + A^k$$ 输出 $S$ 的各个元素对 ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- 矩阵十点【两】 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的迹(就是主对角线上各项的 ...
- POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- POJ - 3233 矩阵套矩阵
题意:给你矩阵\(A\),求\(S=\sum_{i=1}^{k}A^i\) 构造矩阵 \[ \begin{bmatrix} A & E \\ 0 & E\\ \end{bmatrix} ...
随机推荐
- 微信小程序 设置计时器(setInterval)、清除计时器(clearInterval)
1.wxml代码 <!--index.wxml--> <view class="container"> <button type='primary' ...
- win10下安装psql9,后无法访问数据库引擎
1.修改安装文件兼容性,并启动安装 2.安装后 修改psql control center快捷方式的启动文件兼容性 3.修改 start workgroup engine 快捷方式的启动文件兼容性 一 ...
- LLVM 概览
下面是 LLVM 首页对 LLVM 介绍的中文翻译. LLVM 项目是一系列模块化.可重用和工具链技术的集合.不必在意它的名称,LLVM 和之前的虚拟机基本没什么关系了,然而也确实提供了对构建这些虚拟 ...
- HDU1241&POJ2386 dfs简单题
2道题目都差不多,就是问和相邻所有点都有相同数据相连的作为一个联通快,问有多少个连通块 因为最近对搜索题目很是畏惧,总是需要看别人代码才能上手,就先拿这两道简单的dfs题目来练练手,顺便理一理dfs的 ...
- [SCOI2008]奖励关 - 状压动规 - 概率与期望
Description 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝 ...
- Java 学习(5):修饰符 运算符
目录 --- 修饰符 --- 运算符 --- 循环结构 --- 分支结构 修饰符: 修饰符用来定义类.方法或者变量,通常放在语句的最前端.如下: public class className { // ...
- 安装eclipse插件,很慢终于找到了解决的方法
1 .除非你需要,否则不要选择"联接到所有更新站点" 在安装对话框里有一个小复选框,其标示为"在安装过程中联接到所有更新站点从而找到所需的软件."从表面上看,这 ...
- Codeforces704B. Ant Man
n<=5000个数轴上的点,有属性x,a,b,c,d,从i跳到j的代价如下: 问从s跳到t的最小代价. 方法?:先构造s->t链,然后依次插入其他点,每次选个最佳的位置.过了这题,正确性不 ...
- 斯特林(Stirling)公式 求大数阶乘的位数
我们知道整数n的位数的计算方法为:log10(n)+1n!=10^m故n!的位数为 m = log10(n!)+1 lgN!=lg1+lg2+lg3+lg4+lg5+................. ...
- UVA 11400_ Lighting System Design
题意: 给定一系列灯泡的额定功率,电源价钱,一个灯泡的价格以及系统所需该种灯泡的数量.已知流过灯泡的电流相等,所以为省钱可以将电压小的灯泡换成电压大的灯泡,但是不能换成电压更小的灯泡,问最少要花多少钱 ...