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: ...
随机推荐
- HTML5 为 <input> 增加的属性 ; 为 <form> 增加的如需属性
HTML5 为 <input> 增加了如下属性: autocomplete autofocus form formaction formenctype formmethod formnov ...
- 洛谷P3102 [USACO14FEB]秘密代码Secret Code
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 阿里、腾讯热门面试题:聊聊Unix与Java的IO模型?(含详细解析)
众所周知 如果去百度.腾讯等一线大厂面试,一定会深入考候选人的基础技术功底,其中尤为关键和重视的就是IO相关的技术和知识. 而要搞明白IO相关的概念,首先就得弄清楚同步与异步,阻塞与非阻塞到底是什么意 ...
- ubuntu命令错误集
1.在ubuntu命令行使用rz从windows传输文件时出现乱码 解决方法:使用 rz -e 选项进行传输,一般小文件传输不用加 -e 选项,大文件传输需要.
- Javascript专题(三)b.各种轮播和细节分析--上下滚动轮播
这一次,我们用原生JS实现上下滚动方式的轮播.顺带学习一下用JS来创建HTML元素. 上一次写的轮播是淡入淡出效果的,相对来说其实是比较简单的. github源码: 上下轮播源码-github A. ...
- php配置redis
redis扩展程序下载链接(版本3.0.504) 链接: https://pan.baidu.com/s/1QfrjtnifOl8SPf7XCEly2g 密码: u6x6 wampserver下载地址 ...
- 如何直接修改cf,of等标志位的值?
如何直接修改 cf,of 等 标志寄存器位的值? 我记得在哪个教程里见过,但是不太记得了… 貌似是在yjx驱动教程里面… 我想弄这个的原因是想验证 网上查到的 各种跳转语句(ja,jl,jg等) 需要 ...
- 004 Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two ...
- string的各种函数(系统学习)
1.按照面向对象的要求,可以把字符串看作一个对象,设计一个串类加以描述.但是,在一般的情况下不必建立自己的串类,c++标准 在库<string>中给出了类string,提供了丰富的串操作, ...
- new 和 delete 用法
1. 这两个其实是 C++ 语言标准库的库函数,原型分别如下: void *operator new(size_t); //allocate an object void *operator dele ...