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的迹(就是主对角线上各项的 ...
随机推荐
- Swift 学习笔记1
最近在看Swift,努力在看相关的文档以及书籍,因为Swift3.0的更新,以及它开源了,所以打算写一些关于Swift的相关文章.让Swift能够更好的被我理解
- Python3 网络编程
虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Microsoft都有各自的 ...
- Ubuntu14.04 LTS安装不成功
北京时间2014年04月18日早8:00时,Ubuntu14.04 LTS在ubuntu官网放出,果断下之体验. 镜像为ubuntu-14.04-desktop-amd64.iso.大小为964M.M ...
- CSS display:inline和float:left两者区别探讨
本文和大家重点讨论一下CSS display:inline和float:left两者的区别,CSS display是指显示状态,inline表示内联,特点是紧贴着前一个内联元素,通常默认的内联元素有 ...
- php生成随机产生六位数密码的代码
php生成随机产生六位数密码的代码,供大家学习参考.本文转自:http://www.jbxue.com/article/6199.html php生成随机产生六位数密码的代码,供大家学习参考. 复制代 ...
- [Python][flask][flask-login]关于flask-login中各种API使用实例
本篇博文跟上一篇[Python][flask][flask-wtf]关于flask-wtf中API使用实例教程有莫大的关系. 简介:Flask-Login 为 Flask 提供了用户会话管理.它处理了 ...
- [Linux/Ubuntu] vi/vim 使用方法讲解(转载)
转自:http://www.cnblogs.com/emanlee/archive/2011/11/10/2243930.html vi/vim 基本使用方法 vi编辑器是所有Unix及Linux系统 ...
- 一步步学习ASP.NET MVC3 (12)——FileResult
请注明转载地址:http://www.cnblogs.com/arhat 忙了两天,本来老魏昨天就应该写出新的文章,但是由于昨天雨夹雪而且加上昨天晚上加了班,到家都没饭吃了,一看时间都9点了,什么饭店 ...
- Winodws live writer
发布一篇试试.
- Automotive Security的一些资料和心得(7):AUTOSAR和Security
1. 密码模块[1] 密码模块在Services Layer Configurable and common access to 密码子程序 硬件支持密码模块 2. 应用 应用和密码子程序分离 Cry ...