POJ3233Matrix Power Series(十大矩阵问题之三 + 二分+矩阵快速幂)
http://poj.org/problem?id=3233
| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 18658 | Accepted: 7895 |
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
Source
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
int n,m,k;
struct Mat
{
int mat[][];
};
Mat operator* (Mat x, Mat y)
{
Mat c;
memset(c.mat, , sizeof(c.mat));
for(int t = ; t <= n; t++)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
c.mat[i][j] = (c.mat[i][j] + x.mat[i][t] % m * (y.mat[t][j] % m) ) % m;
}
}
return c;
}
Mat operator^ (Mat x, int y)
{
Mat c;
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
c.mat[i][j] = (i == j);
while(y)
{
if(y & )
c = c * x;
x = x * x;
y >>= ;
}
return c;
}
Mat operator + (Mat x, Mat y)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
x.mat[i][j] = ( x.mat[i][j] % m + y.mat[i][j] % m ) % m;
}
return x;
}
Mat dfs(int t,Mat temp)
{
if(t == )
return temp;
int mid = t / ;
Mat c = dfs(mid, temp);
if(t & )
{
c = c + (temp ^ mid ) * c;
return c + (temp ^ t); //第一次交没加括号,查了好长时间的错,惭愧惭愧,其实codeblock都waring了,弱
}
else
return c + (temp ^ mid) * c;
}
int main()
{ scanf("%d%d%d", &n,&k,&m);
Mat a,c;
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
scanf("%d", &a.mat[i][j]);
c = dfs(k,a);
for(int i = ; i <= n; i++)
{
for(int j = ; j < n; j++)
printf("%d ", c.mat[i][j]);
printf("%d\n", c.mat[i][n]);
}
return ;
}
POJ3233Matrix Power Series(十大矩阵问题之三 + 二分+矩阵快速幂)的更多相关文章
- poj3233Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23187 Accepted: ...
- poj3233Matrix Power Series
链接 也是矩阵经典题目 二分递归求解 a+a^2+a^3+..+a^(k/2)+a^(k/2+1)+...+a^k = a+a^2+..+a^k/2+a^k/2(a^1+a^2+..+a^k/2)( ...
- 十大免费教程资源帮助新手快速学习JavaScript
“JavaScript”的名头相信大家肯定是耳熟能详,但只有一小部分人群了解它的使用与应用程序构建方式.这“一小部分”人指的当然是技术过硬的有为青年.网络程序员以及IT专业人员.但对于一位新手或者说外 ...
- POJ3233Matrix Power Series(矩阵快速幂)
题意 题目链接 给出$n \times n$的矩阵$A$,求$\sum_{i = 1}^k A^i $,每个元素对$m$取模 Sol 考虑直接分治 当$k$为奇数时 $\sum_{i = 1}^k A ...
- C++-POJ3233-Matrix Power Series[矩阵乘法][快速幂]
构造矩阵 #include <cstdio> ; struct Matrix{int a[MAXN][MAXN];}O,I;int N; ;i<MAXN;i++);j<MAXN ...
- [矩阵乘法] PKU3233 Matrix Power Series
[ 矩 阵 乘 法 ] M a t r i x P o w e r S e r i e s [矩阵乘法]Matrix Power Series [矩阵乘法]MatrixPowerSeries Desc ...
- poj4474 Scout YYF I(概率dp+矩阵快速幂)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1051 Descr ...
- 2014 Super Training #10 G Nostop --矩阵快速幂
原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...
- poj_3070Fibonacci(矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12732 Accepted: 9060 Descri ...
随机推荐
- 基于 URL 的权限控制
先不用框架,自己实现一下 数据库 /* SQLyog v10.2 MySQL - 5.1.72-community : Database - shiro *********************** ...
- POJ 1125 Stockbroker Grapevine
Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33141 Accepted: ...
- 项目管理和缺陷跟踪工具Redmine
官网: http://www.redmine.org/ http://demo.redmine.org/ 下载: http://www.redmine.org/projects/redmine/wik ...
- Android view中的requestLayout和invalidate方法
Android view中的requestLayout和invalidate方法 requestLayout:当view确定自身已经不再适合现有的区域时,该view本身调用这个方法要求parent v ...
- .net mvc Bundle 问题解决方案
使用.net MVC4 开发Web项目时,可以利用"Bundle"对Css.JS文件进行压缩打包,一方面可以减少数据加载的次数,另一方面可以减少数据传输量,但在实际使用中却遇到了问 ...
- CAS 单点登录流程
经验:在网上学东西不要指望一篇文章就能让你明白——我在网上学CAS流程,看了五六篇博文,其中三篇是觉得作者表达能力不行,或者作者自己就没明白怎么回事就出来写东西,看到一半就跳过了,剩下两篇每篇看了两遍 ...
- 文件“D:\file.txt”正由另一进程使用,因此该进程无法访问该文件。
关于如题的解决方案! 都是有一定编程基础的人,我就不讲其它的了. 1.在实例化一个FileStream后,用完它一定要关闭.先试试这一条: 2.第一条不起作用的话,用本条.在实例化FileStream ...
- Zxing二维码重复扫描,不退出。
扫描条码,把手机实现类似超市扫描枪之类的连续扫描. private void continuePreview(){ SurfaceView surfaceView = (SurfaceView) fi ...
- UISwitch和UIActivity的使用
- (void)viewDidLoad{ [super viewDidLoad]; //创建一个开关控件,苹果给它固定的size(79*27),frame更改size无效 //继承于 ...
- iOS - 语音云通讯
iOS SDK 2.0 语音及图片消息详解本文档将详细介绍融云的语音及图片消息接口功能及使用说明.阅读本文前,我们假设您已经阅读了融云 iOS 开发指南,并掌握融云 SDK 的基本用法. 语音消息用来 ...