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 ...
随机推荐
- hdu6330 多校3 L 画一个cube
http://acm.hdu.edu.cn/showproblem.php?pid=6330 技巧:循环变量要选1~A,然后把公式写下标里.会快很多 #define _CRT_SECURE_NO_WA ...
- B. Salty Fish Go! -期望题(瞎搞题)
链接:https://www.nowcoder.com/acm/contest/104/B来源:牛客网 题意:A few days ago, WRD was playing a small game ...
- Exception 06 : org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session :
异常名称: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was ...
- EF Code First模型约束
总之,EF比较复杂.如果不想深究,建议简单用用.基本对应就行,大项目标准开发还是ModelFirst(先建立DB各种约束),然后再c#类约束.定义. 当然写原型时用ef很快.
- Androidstudio_LinearLayout
- 20165336 2017-2018-2《Java程序设计》课程总结
每周作业链接汇总 我期望的师生关系:对师生关系的看法 学习基础和C语言基础调查:关于学JAVA与C的调查 Linux安装及学习:Linux的安装 第一周学习总结:认识学习JAVA 第二周学习总结:JA ...
- eclipse怎么解决Failed to load the JNIshared library
Q: 打开eclipse打开报Failed to load the JNIshared library的错误. A:jdk的位数跟eclipse位数一致即可解决. 把eclipse下载64位即可 cm ...
- 陌生的 metaclass(转)
add by zhj:这是我见过的对metaclass解释最清楚的文章了,例子很好,真是一例胜千言 原文:http://wiki.jikexueyuan.com/project/explore-pyt ...
- swift一些常用系统方法的简化使用
//获取Image func FImage(_ imageName:String) -> UIImage { return UIImage(named:imageName)! } //获取Url ...
- module_init 内核调用过程
内核版本:linux_2.6.22.6 入口源文件: init.h