poj 3233 Matrix Power Series(矩阵二分,高速幂)
| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 15739 | Accepted: 6724 |
Description
Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.
Input
The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n lines each containing n nonnegative
integers below 32,768, giving A’s elements in row-major order.
Output
Output the elements of S modulo m in the same way as A is given.
Sample Input
2 2 4
0 1
1 1
Sample Output
1 2
2 3
当k为奇数时
Sk = A + A2 + A3 + … + Ak
=(1+Ak/2)*(A + A2 + A3 + … + Ak/2 )+{Ak}
=(1+Ak/2)*(Sk/2 )+{Ak}
当k为偶数时
Sk = A + A2 + A3 + … + Ak
=(1+Ak/2)*(A + A2 + A3 + … + Ak/2 )+{Ak}
=(1+Ak/2)*(Sk/2 )
就能够二分递归求Sk
代码:
//829ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
struct matrix
{
int ma[50][50];
} a;
matrix multi(matrix x,matrix y)//矩阵相乘
{
matrix ans;
memset(ans.ma,0,sizeof(ans.ma));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(x.ma[i][j])//稀疏矩阵优化
for(int k=1; k<=n; k++)
{
ans.ma[i][k]=(ans.ma[i][k]+x.ma[i][j]*y.ma[j][k])%m;
}
}
}
return ans;
}
matrix add(matrix x,matrix y)//矩阵相加
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
x.ma[i][j]=(x.ma[i][j]+y.ma[i][j])%m;
return x;
}
matrix pow(matrix a,int m)
{
matrix ans;
for(int i=1; i<=n; i++) //单位矩阵
{
for(int j=1; j<=n; j++)
{
if(i==j)
ans.ma[i][j]=1;
else
ans.ma[i][j]=0;
}
}
while(m)//矩阵高速幂
{
if(m&1)
{
ans=multi(ans,a);
}
a=multi(a,a);
m=(m>>1);
}
return ans;
}
matrix solve(matrix x,int k)//递归求Sk
{
if(k==1)
return x;
matrix ans;
for(int i=1; i<=n; i++) //单位矩阵
{
for(int j=1; j<=n; j++)
{
if(i==j)
ans.ma[i][j]=1;
else
ans.ma[i][j]=0;
}
}
ans=add(ans,pow(x,k/2));
ans=multi(ans,solve(x,k/2));
if(k&1)
ans=add(ans,pow(x,k));
return ans;
}
int main()
{
int k;
while(~scanf("%d%d%d",&n,&k,&m))
{
matrix ans,a;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
scanf("%d",&a.ma[i][j]);
}
ans=solve(a,k);
for(int i=1; i<=n; i++)
{
for(int j=1; j<n; j++)
printf("%d ",ans.ma[i][j]);
printf("%d\n",ans.ma[i][n]);
}
}
return 0;
}
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(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- 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 矩阵求和
http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- 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(矩阵等比求和)
题目链接 模板题. #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——3264Balanced Lineup(RMQ模版水题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 44112 Accepted: 20713 ...
- FWT 学习笔记
FWT学习笔记 好久以前写的,先粘上来 定义数组 \(n=2^k\) \(A=[a_0,a_1,a_2,a_3,...,a_{n-1}]\) 令\(A_0=[a_0,a_1,a_2,...,a_{\f ...
- 我要好offer之 搜索算法大总结
1. 二分搜索 详见笔者博文:二分搜索的那些事儿,非常全面 2. 矩阵二分搜索 (1) 矩阵每行递增,且下一行第一个元素大于上一个最后一个元素 (2) 矩阵每行递增,且每列也递增 3. DFS 深度优 ...
- batch.bat explaination
1.Echo 命令 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置. 语法 echo [{on|off}] [message] Sample篅echo of ...
- js 函数arguments一种用法
无意改同事的代码发现的 function toggle(){ var _arguments=arguments; var count=0; $("#more").click(fun ...
- mfc对话框嵌入Flash的交互(转)
原文转自 http://blog.csdn.net/yacper/article/details/5021081 研究Flash嵌入游戏中的可行性....... 渲染问题已解决 事件响应已解决 下面是 ...
- HDU 5972 Regular Number(字符串shift - and算法)
题目链接 HDU5972 2016 ACM/ICPC 大连区域赛 B题 我们预处理出$b[i][j]$,$b[i][j] = 1$的意义是数字$i$可以放在第$j$位. 然后就开始这个匹配的过程. ...
- 洛谷——P2658 汽车拉力比赛
P2658 汽车拉力比赛 题目描述 博艾市将要举行一场汽车拉力比赛. 赛场凹凸不平,所以被描述为M*N的网格来表示海拔高度(1≤ M,N ≤500),每个单元格的海拔范围在0到10^9之间. 其中一些 ...
- Xamarin.Forms中为WebView指定数据来源Source
Xamarin.Forms中为WebView指定数据来源Source 网页视图WebView用来显示HTML和网页形式内容.使用这种方式,可以借助网页形式进行界面设计,并利于更新和维护.WebVi ...
- Light oj 1085 - All Possible Increasing Subsequences (简单dp + 离散化 + BIT)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1085 题意: 问你有多少个上升子序列. 思路: dp[i]表示以第i个数结尾的 ...