POJ -- 3233 求“等比矩阵”前n(n <=10^9)项和
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 思路:1.最基本的,需要用到矩阵快速幂 2.快速幂求完之后怎样快速求和?若逐项累加求和必然会超时,这时需要求递推公式:(1)若n为偶数,则:S(n) = A^(n/2)*S(n/2)+s(n/2);(2)若n为奇数 S(n) = A^(n/2+1) + S(n/2)*A^(n/2+1) + S(n/2),公式不难推,写几个就发现规律了。这样就把时间复杂度降下来了。
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m;
typedef struct Matrix{
int m[][];
Matrix(){
memset(m, , sizeof(m));
}
}Matrix;
Matrix mtAdd(Matrix A, Matrix B){
for(int i = ;i < n;i ++)
for(int j = ;j < n;j ++){
A.m[i][j] += B.m[i][j];
A.m[i][j] %= m;
}
return A;
}
Matrix mtMul(Matrix A, Matrix B){
Matrix tmp;
for(int i = ;i < n;i ++)
for(int j = ;j < n;j ++)
for(int k = ;k < n;k ++){
tmp.m[i][j] += A.m[i][k]*B.m[k][j];
tmp.m[i][j] %= m;
}
return tmp;
}
Matrix mtPow(Matrix A, int k){
if(k == ) return A;
Matrix tmp = mtPow(A, k >> );
Matrix res = mtMul(tmp, tmp);
if(k&) res = mtMul(res, A);
return res;
}
Matrix mtSum(Matrix A, int k){
if(k == ) return A;
Matrix tmp = mtSum(A, k/);
if(k&){
Matrix t = mtPow(A, k/+);
Matrix tmp1 = mtMul(tmp, t);
Matrix tmp2 = mtAdd(t, tmp);
return mtAdd(tmp1, tmp2);
}else return mtAdd(tmp, mtMul(mtPow(A, k/), tmp));
}
int main(){
int k, tmp;
/* freopen("in.c", "r", stdin); */
while(~scanf("%d%d%d", &n, &k, &m)){
Matrix M;
for(int i = ;i < n;i ++)
for(int j = ;j < n;j ++){
scanf("%d", &tmp);
M.m[i][j] = tmp;
}
M = mtSum(M, k);
for(int i = ;i < n;i ++){
for(int j = ;j < n;j ++)
printf("%d ", M.m[i][j]);
puts("");
}
}
return ;
}
POJ -- 3233 求“等比矩阵”前n(n <=10^9)项和的更多相关文章
- 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://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- 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 矩阵快速幂
设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(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- 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(矩阵等比求和)
题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...
- 矩阵儿快速幂 - POJ 3233 矩阵力量系列
不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...
- 矩阵十点【两】 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的迹(就是主对角线上各项的 ...
随机推荐
- keepalived+haproxy-部署高可用负载均衡
环境: 准备两台机子,安装haproxy及keepalive都一样,只是配置不一样而已. 这里只说明一台机子上安装haproxy及keepalive. ======================== ...
- css3 回到顶部书写
回到顶部 JS 代码 backTop = function(){ if(!document.querySelector("#backTop")){return;} ...
- PHP中使用cURL
1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性 ...
- putty实现自动登录的方法(ssh和ssh2)
介绍putty实现自动登录的方法. 1.登录主机并输入ssh-keygen -t rsa 提示Enter file in which to save the key (/root/.ssh/id ...
- oracle11g 表或视图连接时有可能发生的问题
---------背景--------- oracle11g 有2个视图,都有一个id字段,且id字段的值相同 例如:id都有 A01 ,A02 ,A03 --------问题--------- 把2 ...
- C#多线程(二)
一.线程池 每次创建一个线程,都会花费几百微秒级别的时间来创建一个私有的局部栈,每个线程默认使用1M的内存.这个可以在使用Thread类的构造函数时设置: new Thread(new ThreadS ...
- Sublime Text3注册码
这是一个注册码-– BEGIN LICENSE -– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 ...
- windows 下 scrapy的安装
安装参考博客:http://davenzhang.com/scrapy_install.htm 我是先安装了scrapy,发现import scrapy 的时候报错.之后一次安装了下面关联软件的.ex ...
- 使用Python编程语言连接MySQL数据库代码
使用Python编程语言连接MySQL数据库代码,跟大家分享一下: 前几天我用python操作了mysql的数据库,发现非常的有趣,而且python操作mysql的方法非常的简单和快速,所以我把代码分 ...
- Nginx+uWSGI+bottle 在Linux上部署
在/data/lujianxing/bottle 文件夹中创建三个文件: bottle.py bottle的源文件 a.py from bottle import Bottle, run mybott ...