Poj 3233 Matrix Power Series(矩阵二分快速幂)
题目链接:http://poj.org/problem?id=3233
解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k,然后结果的每个元素A[i][j] % m。(n <= 30,k < 10^9,m < 10^4)
要用到矩阵快速幂,但我认为最重要的其实还是相加的那个过程,因为k的范围是10^9,一个一个加肯定是不行的,我想了一个办法就是我以k = 8为例说明:
ans = A + A^2 + A^3 + A^4 + A^5 + A^6 + A^7 + A^8
= A + A^2 + A^3 + A^4 + A^4 * (A + A^2 + A^3 + A^4) // 这样分成两块之后就只要算一次A + A^2 + A^3 + A^4,就可以得出最后结果,
而A + A^2 + A^3 + A^4这个又可以通过相同的方法划分成如下:
= A + A^2 + A^2 * (A + A^2)同理。。。。就可以在logn时间求出他们的和了,然后快速的求A^k次方是用二分矩阵快速幂这里就不说了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<deque>
#include<cmath>
using namespace std;
typedef __int64 INT;
const int N = ;
int m; struct node
{
INT t[N][N];
int n;
friend node operator * (node a,node b)
{
node B = a;
for(int i = ;i < a.n;++i)
for(int j = ;j < a.n;++j)
{
int tot = ;
for(int k = ;k < a.n;++k)
tot += ((a.t[i][k] * b.t[k][j]) % m);
B.t[i][j] = tot % m;
}
return B;
}
friend node operator + (node a,node b)
{
node B;
B.n = a.n;
for(int i = ;i < a.n;++i)
for(int j = ;j < a.n;++j)
B.t[i][j] = (a.t[i][j] + b.t[i][j]) % m;
return B;
}
};
node A;
void print(node t)
{
for(int i = ;i < t.n;++i)
for(int j = ;j < t.n;++j)
printf(j == t.n-? "%d\n":"%d ",t.t[i][j]);
}
node Pw(node tt,int n) //二分矩阵快速幂求A ^ n
{
if(n == ) return A;
node res;
res.n = tt.n;
memset(res.t,,sizeof(res.t));
for(int i = ;i < res.n;++i)
res.t[i][i] = ;
while(n)
{
if(n & )
res = res * tt;
n >>= ;
tt = tt * tt;
}
return res;
} node get_ans(int n) //求A^1 到 A ^ n的和
{
if(n == )
return A;
if(n & )
{
node temp1;
temp1.n = A.n;
temp1 = Pw(A,n);
node temp2;
temp2.n = ;
temp2 = get_ans(n-);
temp1 = temp1 + temp2;
return temp1;
}
else
{
node temp1;
temp1.n = A.n;
temp1 = Pw(A,n / );
node temp2 = get_ans(n / );
temp2.n = A.n;
temp1 = temp1 * temp2;
temp1 = temp1 + temp2;
return temp1;
}
} int main()
{ int n,k;
while(scanf("%d%d%d",&n,&k,&m)!=EOF)
{
A.n = n;
for(int i = ;i < n;++i)
for(int j = ;j < n;++j)
scanf("%d",&A.t[i][j]);
node ans = get_ans(k);
print(ans);
}
return ;
}
Poj 3233 Matrix Power Series(矩阵二分快速幂)的更多相关文章
- 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 Description Given a n × n matrix A and ...
- 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 矩阵快速幂
设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(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- 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: ...
随机推荐
- Android开发者资源大汇总
本文总结了最新的Android开发资源.下面列出的资源都是常用的,每个Android程序员都应该知道,能大大方便App开发.Enjoy~ 来源:Android开发周刊 中文的Android开发信息,资 ...
- Windows下apache php wordpress配置
2. Use notepad to open httpd.conf config file. Make use the line "LoadModule rewrite_module mod ...
- Unity 3D本地发布WebPlayer版时Failed to download data file解决方案
遇到这个问题就是指Web服务器并没有支持这种*.unity3d文件类型.需要做的是在Web服务器中添加MIME类型: IIS 7 及以上版本: 在功能视图的IIS选项卡中: 双击打开MIME,选择添加 ...
- Flex ObjectHandles 构建绘图程序!
模型 主画布组件:com/components/graph/GraphContainer.mxml <?xml version="1.0" encoding="ut ...
- (练习)rational rose进行用例图设计
用例图:
- java.lang.NoSuchFieldError: deferredExpression
处理:遇到这个异常的时候是用jstl标签,是版本问题,由于MyEclipse添加Java EE5,其中自动包括了jstl1.2的版本,lib中又存在一个jstl1.1.2的jar包,把旧版本的删掉就可 ...
- 常用的Java 架包(jar)的用途
前言:如果需要在项目中引入jar包,可以采用maven,配置方式在 http://mvnrepository.com 查询 slf4j-api 简介:slf4j并不是一种具体的日志系统,而是一个用户 ...
- sql-exists和not exists
EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或FalseEXISTS 指定一个子查询,检测行的存在. 实例: (一). 在子查询中使用 NULL ...
- uploadfile上传文件时ie浏览器无法弹出窗口
设置--->安全---->activeX筛选取消选择 更多.net.sqlserver.jquery资料欢迎访问 htttp://www.itservicecn.com
- 【 CodeForces 604A】B - 特别水的题2-Uncowed Forces
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/B Description Kevin Sun has jus ...