POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series
【题目链接】Matrix Power Series
【题目类型】二分等比求和
&题解:
这题我原来用vector写的,总是超时,不知道为什么,之后就改用数组了,照着别人的代码敲了一遍
【时间复杂度】O(logn)
&代码:
#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int N= 30 +9;
struct Matrix
{
int m[N][N];
};
Matrix I;
int n,k,M;
Matrix add(Matrix a,Matrix b)
{
Matrix c;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
c.m[i][j]=(a.m[i][j]+b.m[i][j])%M;
return c;
}
Matrix multi(Matrix a,Matrix b)
{
Matrix c;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
c.m[i][j]=0;
for(int k=0;k<n;k++)
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%M;
}
}
return c;
}
Matrix power(Matrix A,ll n)
{
Matrix ans=I;
while(n){
if(n&1)
ans=multi(ans,A);
A=multi(A,A);
n>>=1;
}
return ans;
}
Matrix sum(Matrix A,ll k)
{
if(k==1) return A;
Matrix t=sum(A,k/2);
Matrix cur=power(A,k/2+(k&1));
t=add(t,multi(t,cur));
if(k&1) t=add(t,cur);
return t;
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// freopen("E:1.txt","r",stdin);
while(cin>>n>>k>>M){
Matrix A;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>A.m[i][j];
A.m[i][j]%=M;
}
I.m[i][i]=1;
}
Matrix ans=sum(A,k);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
cout<<ans.m[i][j]<<" ";
cout<<endl;
}
}
return 0;
}
POJ 3233 Matrix Power Series(二分等比求和)的更多相关文章
- POJ 3233 Matrix Power Series 二分+矩阵乘法
链接:http://poj.org/problem?id=3233 题意:给一个N*N的矩阵(N<=30),求S = A + A^2 + A^3 + - + A^k(k<=10^9). 思 ...
- POJ 3233 Matrix Power Series --二分求矩阵等比数列和
题意:求S(k) = A+A^2+...+A^k. 解法:二分即可. if(k为奇) S(k) = S(k-1)+A^k else S(k) = S(k/2)*(I+A^(k/2)) ...
- 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 ...
- [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15417 Accepted: ...
- poj 3233 Matrix Power Series(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
随机推荐
- python与pycharm安装
1.python install 1.1 下载安装包 - https://www.python.org/downloads 1.2 installl 1.2.1 双击exe安装文件 1.3 如果安装 ...
- 创建本地SVN版本库以及将SVN导入GIT
创建本地SVN 通常SVN作为一种服务,是在服务器上架设,供用户通过网络访问使用.但如果只是自己日常使用,完全可以架设在本机上,不需要启动后台程序,通过文件的方式访问即可. 建立本地SVN非常简单,一 ...
- OPTIMIZE TABLE linked list 表优化原理 链表数据结构 空间再利用
小结: 1.加快读写: 2.对于InnoDB表,在一定条件下,通过复制旧表重建: 3.实践中, 3.1.show processlist;查看线程,发现,认为堵塞读请求: 3.2.数据长度空间不变,索 ...
- [gdb][python][libpython] 使用gdb调试python脚本
https://devguide.python.org/gdb/ https://sourceware.org/gdb/current/onlinedocs/gdb/Python.html#Pytho ...
- [redhat][centos] 让不同小版本的CentOS7使用相同的内核版本
背景: CentOS7有定期的小版本发布,即官网释出的ISO,是带着小版本号的.CentOS7可以使用平滑升级,从这些小版本号中升上去. 但是并不是每一次的更新,都在释出的ISO中,这样的话,一台既有 ...
- [troubleshoot][archliunx][chromium][flash] chrome提示flash不是最新
最近chrome总是在提示flash不是最新要求更新. 原来以前用的flash包 chromium-pepper-flash 不见了,改名变成了pepper-flash. /home/tong [to ...
- nfs的时间问题,影响编译
[root@okk dpdk]# rm -rf x86_64-native-linuxapp-gcc/ [root@okk dpdk]# A=`date +%s` ; B=`expr $A + `; ...
- Excel使用
筛选 1.数据->取消\使用筛选; 边框 函数 1.使用函数的话需要设置单元格格式为常规;
- Python开发【笔记】:PEP 8 编码规范
PEP 8 编码规范 https://bk.tencent.com/document/bkrule/040101.pdf ...
- MongoDB update修改器: 针对Fields的$修改器 $inc $set $unset
MongoDB update修改器: $inc $set $unset $push $pull $pop 针对Fields的$修改器 $set: { $set: { key: value } } $s ...