POJ 3233 Matrix Power Series (矩阵分块,递推)
矩阵乘法是可以分块的,而且幂的和也是具有线性的。
不难得到 Si = Si-1+A*Ai-1,Ai = A*Ai-1。然后矩阵快速幂就可以了。
/*********************************************************
* ------------------ *
* author AbyssalFish *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std; typedef long long ll;
typedef vector<int> row;
typedef vector<row> mat; int n, k, M; mat Mul;
mat &operator *(mat &A, mat& B)
{
mat &R = Mul;
R.assign(n,row(n));
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
for(int k = ; k < n; k++){
R[i][j] = (R[i][j] +A[i][k]*B[k][j])%M; }
}
} return R;
} //#define LOCAL
#ifdef LOCAL
void censor(mat &B)
{
for(auto r: B){
for(int c: r)
cout<<c<<' ';
cout<<endl;
}
}
#endif mat operator ^(mat A,int q)
{
mat Re(n,row(n));
for(int i = ; i < n; i++) Re[i][i] = ;
while(q){
if(q&) Re = Re*A;
A = A*A;
q >>= ;
}
return Re;
} int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int nn; scanf("%d%d%d",&nn,&k,&M);
n = *nn;
mat A(nn,row(nn));
for(int i = ; i < nn; i++){
for(int j = ; j < nn; j++){
scanf("%d",&A[i][j]);
}
}
mat B(n,row(n));
for(int i = ; i < nn; i++) {
B[i][i] = ;
copy(A[i].begin(),A[i].end(),B[i].begin()+nn);
copy(A[i].begin(),A[i].end(),B[i+nn].begin()+nn);
}
B = B^k;
for(int i = ; i < nn; i++){
for(int j = ; j < nn; j++){
printf("%d%c",B[i][j+nn],j==nn-?'\n':' ');
}
}
#ifdef LOCAL
cout<<"rum time:"<<clock()<<"ms"<<endl;
#endif // LOCAL
return ;
}
POJ 3233 Matrix Power Series (矩阵分块,递推)的更多相关文章
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- poj 3233 Matrix Power Series(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- 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] ...
- poj 3233 Matrix Power Series 矩阵求和
http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- POJ 3233 Matrix Power Series(矩阵等比求和)
题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...
- 矩阵十点【两】 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 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
随机推荐
- 输入类型<input type="number"> / input标签的输入限制
输入限制 属性 描述 disabled 规定输入字段应该被禁用. max 规定输入字段的最大值. maxlength 规定输入字段的最大字符数. min 规定输入字段的最小值. pattern 规定通 ...
- 括号序列(区间dp)
括号序列(区间dp) 输入一个长度不超过100的,由"(",")","[",")"组成的序列,请添加尽量少的括号,得到一 ...
- Zookeeper下载方法
Zookeeper官网地址:http://zookeeper.apache.org/ Zookeeper下载链接:http://mirrors.tuna.tsinghua.ed ...
- 分别使用ES5和ES6进行数组去重以及注意事项
ES6,ES5数组去重 使用Es6进行数组去重 var arr = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]; ...
- redis使用方法
redis缓存服务器笔记 redis是一个高性能的key-value存储系统,能够作为缓存框架和队列 但是由于他是一个内存内存系统,这些数据还是要存储到数据库中的 作为缓存框架: create/upd ...
- Decorator模式(装饰器模式)
Decorator模式? 假如现在有一块蛋糕,如果只涂上奶油,其他什么都不加,就是奶油蛋糕.如果加上草莓,就是草莓奶油蛋糕.如果再加上一块黑色巧克力板,上面用白色巧克力写上姓名,然后插上代表年龄的蜡烛 ...
- FTP服务器FileZilla Server配置及使用方法
FileZilla Server下载安装完成后,安装过程不写说明了,网上一抓一大把,直接从配置开始记录. 1.创建服务器 ² Password:栏位中输入本服务器Filezilla服务的密码, ² ...
- android 开发-spinner下拉框控件的实现
Android提供实现下拉框功能的非常实用的控件Spinner. spinner控件需要向xml资源文件中添加spinner标签,如下: <Spinner android:id="@+ ...
- <Win7硬件故障分析>
查看当前系统正在运行的硬件信息. 右击计算机——属性——设备管理器看到下面: 一些常见的硬件故障: 主板故障 >开机不正常,偶尔出现突然的断电现象 >明明自己选择了关机,但是还是无法关机或 ...
- over partition by与group by
over partition by与group by 的区别 http://www.cnblogs.com/scottpei/archive/2012/02/16/2353718.html 今天看到一 ...