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 ...
随机推荐
- FusionCharts V3图表导出图片和PDF属性说明(转)
百闻不如一见,狠狠点击,快快下载:(演示文档有错误,不提供下载了.待新的演示文档出来.) 许多朋友说上面的DEMO用不了.fusioncharts官方的演示非常不错,就是来不及整理,各位大侠们可以研究 ...
- C# LUA 闭包
许多语言中有闭包的概念,C#的闭包以lambda表达式表现,可以实现与LUA完全一样的效果. //LUA------------------------------------------------ ...
- 去他的效应(what-the-hell effect)与自我放纵
去他的 效应(what-the-hell effect)与自我放纵 为什么写这篇文章: 对于我来说,但我感到疲惫——"无意拿起"手机,对自己说"随便看看"——但 ...
- chrome拓展开发实战
chrome拓展开发实战:页面脚本的拦截注入 时间 2015-07-24 11:15:00 博客园精华区 原文 http://www.cnblogs.com/horve/p/4672890.htm ...
- matlab批量合并txt文件
1: %% merge.m 2: %%%%Main程序%%%%%% 3: %%%%%% 4: %%%%%%本程序合并完各个子文件夹中的txt到主文件目录下,并且合并的文件以子文件夹名字命名 5: %% ...
- 数据摘要算法的测试效率(SHA、MD5和CRC32)
1.算法概述 数据摘要算法是密码学算法中非常重要的一个分支,它通过对所有数据提取指纹信息以实现数据签名.数据完整性校验等功能,由于其不可逆性,有时候会被用做敏感信息的加密.数据摘要算法也被称为哈希(H ...
- [C++] 如何查看DLL有哪些函数
Visual Studio里面自带了一个工具 dumpbin. 打开VS的command line,输入dumpbin可以查看帮助. 我们查看导出函数的话,使用选项/EXPORTS. 如果函数太多,可 ...
- File类和RandomAccessFile类
目录 File类 File类常用操作 (1)创建文件 (2)删除文件 (3)创建文件夹 (4)列出指定目录全部文件 (5)删除目录 RandomAcce ...
- IOS开发之——OpenUDID的使用获取用户唯一设备
下载网址:https://github.com/ylechelle/OpenUDID OpenUDID测试结果分析 1)优点: a.没有用到MAC地址.MAC地址跟UDID一样,存在隐私问题.不能保证 ...
- Jquery操作select,radio,input,p之类
select的操作 变化后触发操作 $("#txtaddprojecturl").change(function(){ $("#addprojectname") ...