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 ...
随机推荐
- zabbix错误记录
zabbix部署好,在使用一段时间后,出现了不少报错,在此简单做一记录.1)Zabbix监控界面报错“Lack of free swap space”解决公司线上部署的zabbix3.0的监控界面首页 ...
- html的解析
Web页面运行在各种各样的浏览器当中,浏览器载入.渲染页面的速度直接影响着用户体验 简单地说,页面渲染就是浏览器将html代码根据CSS定义的规则显示在浏览器窗口中的这个过程.先来大致了解一下浏览器都 ...
- C# 将透明图片的非透明区域转换成Region
以下代码实现将一张带透明度的png图片的非透明部分转换成Region输出 /// <summary> /// 根据图片得到一个图片非透明部分的区域 /// </summary> ...
- 命令行参数(argc, argv)
每个C语言程序都必须有一个称为main()的函数,作为程序启动的起点.当执行程序时,命令行参数(command-line argument)(由shell逐一解析)通过两个入参提供给main()函数. ...
- matlab 中的textscan
textread 与textscan的区别 textscan更适合读入大文件: textscan可以从文件的任何位置开始读入,而textread 只能从文件开头开始读入: textscan也可以从上 ...
- jdbc基础 (二) 通过properties配置文件连接数据库
csdn博文地址:jdbc基础 (二) 通过properties配置文件连接数据库 上一篇描述了对mysql数据库的简单操作,下面来看一下开发中应该如何灵活应用. 因为jdbc对数据库的驱动加载.连接 ...
- [整理] ES5 词法约定文档树状图
将ES5 词法说明整理为了树状图,方便查阅,请自行点开小图看大图:
- Javascript基础系列之(七)函数(对象和属性)
包含在对象里的数据可以通过两种方式来访问 既属性的对象(property)和方法(method) 属性是隶属于某个特定对象的变量,方法则是某个特定对象才能调运的函数. 对象是由一些彼此相关的属性和方法 ...
- groovyConsole — the Groovy Swing console
1. Groovy : Groovy Console The Groovy Swing Console allows a user to enter and run Groovy scripts. T ...
- HDU 5976 Detachment 打表找规律
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5976 Detachment Time Limit: 4000/2000 MS (Java/Other ...