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: ...
随机推荐
- 学习 JSP:第一步Eclipse+Tomcat+jre(配置环境)
[下载软件](例子version:版本号) Eclipse从官网下载(version:4.7)http://www.eclipse.org/downloads/ jre从官网下载(version:1. ...
- mod性质 学习笔记
mod性质小结 \(a\equiv b(\mod m)\) $ \rightarrow \( \)a-b=k*m,k\in Z$ \(a\equiv b且c\equiv d(\mod m)\) \(\ ...
- Nginx报504 gateway timeout错误的解决方法
转载文章来源:http://www.111cn.net/sys/nginx/90669.htm(若侵删) Nginx报504 gateway timeout错误引起,一个是文件配置问题,另一个是相关处 ...
- UVa1476 Error Curves
画出函数图像后,发现是一个类似V字型的图. 可以用三分法找图像最低点 WA了一串,之后发现是读入优化迷之被卡. /*by SilverN*/ #include<iostream> #inc ...
- 转 Django+Bootstrap练习--我的类博客系统开发
转自: http://blog.sina.com.cn/s/blog_7e050dc80102w312.html 本文记录了一个类博客网站从无到有的搭建过程,同时也是我入门django以及再次入门前端 ...
- strace工具的实现原理【转】
转自:http://blog.csdn.net/jasonchen_gbd/article/details/44044539 版权声明:本文为博主原创文章,转载请附上原博链接. 目录(?)[-] ...
- AC日记——线段树练习4 codevs 4919
4919 线段树练习4 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给你N个数,有两种操作 ...
- Redis实用监控工具一览
Redis已经成为web应用开发不可或缺的一个组成部分,在项目中的应用越来越广泛,这篇文章就来讲讲那些关于Redis监控的那点事. vredis-benchmark 1.1 简介 第一个就介绍一下,R ...
- luogu P2056 采花
题目描述 萧芸斓是 Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了 n 朵花,花有 c 种颜色(用整数 1-c 表示) ,且花是排成 ...
- BZOJ1001[BeiJing2006]狼抓兔子最小割網絡流
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...