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: ...
随机推荐
- VC6.0读取Excel文件数据
啰嗦一下:本人所在公司从事碟式斯特林太阳能发电设备的研发与销售.单台设备图如下: 工作原理如下:整个设备大致可分为五个部分, 1.服务器,负责气象.发电等数据存取,电.网连接等处理: 2.气象站,通过 ...
- git点滴的积累
git的基本学习的网址: http://www.yiibai.com/git/git_update_operation.html 0.git首次上传代码 http://www.cnblogs.com/ ...
- node不懂的方法的使用
1. 学习的时候注意,过滤器,上传文件sftp,读取excel,还有cookie的操作,sql的操作.node的框架express koa hapi 还有引擎ejs,hbs,jade,日志管理等,并发 ...
- [poj1742]coin
题意:多重背包的w=v特殊情况 分析:此题如果用单调队列O(nv)会被无耻卡常数…… 然后鉴于此题W=V,所以只存在背包恰好为i的是否存在,即bool型的f[i]是否为1 即往背包染色上面考虑 如果是 ...
- jQuery使用之(二)设置元素的样式
css是页面不能分隔的部分,jQuery中也提供了一些css相关的实用的办法.前面章节中有使用过 addClass()为元素添加css样式风格.本节主要介绍jQuery如何设置页面的样式风格.包括添加 ...
- Grovvy Step byStep Examples
def LIMIT=10 def count=1 println 'start' while(count<=LIMIT){ println "count:${count}" ...
- UItableView的编辑--删除移动cell
// // RootViewController.m // UI__TableView的编辑 // // Created by dllo on 16/3/17. // Copyright © 2016 ...
- c++ 中 delete p与 delete []p的区别
#include <cstdio> class A{private: int i;public: ~A() { printf("hi"); }};void d(A *) ...
- JQuery学习(1)
JQuery学前准备 JQuery的各种包: 1.jquery-ui(包含小工具及组件) 2.jquery-1.7.1.intellisense.js(智能提示包) 3.jquery-1.7.1.js ...
- 【ZOJ 3844】Easy Task
题意 每次把序列中最大的数a的一个和最小的数b的一个变成a-b.求最后是否能使序列里的数全部相同,能则输出这个相同的数. 分析 一定是有解的,不断减少最大数的个数,最大数减少为0个时,就是减少了不同数 ...