Matrix Power Series

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述
Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.
 
输入
The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 10^9) and m (m < 10^4). Then follow n lines each containing n nonnegative integers below 32,768, giving A’s elements in row-major order.
输出
Output the elements of S modulo m in the same way as A is given.
样例输入
2 2 4
0 1
1 1
样例输出
1 2
2 3
来源
POJ Monthly
上传者
张云聪
#include <iostream>
#include <cstdio> using namespace std; int M=; struct Matrix{
long int line,column;
long int m[][];
};
struct Matr{
long int line,column;
long int m[][];
Matr(Matrix x){
line =x.line*;
column=x.column*;
for(int i=;i<x.line*;i++){
for(int j=;j<x.column;j++){
m[i][j]=x.m[i%x.line][j];
}
}
for(int i=;i<x.line;i++){
for(int j=x.column;j<x.column*;j++){
m[i][j]=;
}
}
for(int i=x.line;i<x.line*;i++){
for(int j=x.column;j<x.column*;j++){
if(i==j){
m[i][j]=;
}else{
m[i][j]=;
}
}
}
}
}; Matr mult(Matr a,Matr b){
Matr ans(a);
ans.line=a.line;
ans.column=b.column;
//ans=inist(ans,0);
for(int i=;i<ans.line;i++){
for(int j=;j<ans.column;j++){
ans.m[i][j]=;
for(int k=;k<ans.column;k++){
ans.m[i][j]+=(a.m[i][k]*b.m[k][j]);
ans.m[i][j]%=M;
}
}
}
return ans;
} Matr fast_matrix(Matr x,int n){
Matr an(x),tmp(x);
for(int i=;i<x.line;i++){
for(int j=;j<x.column;j++){
an.m[i][j]=x.m[i+x.line/][j];
}
}
an.line/=;
while(n){
if(n%!=){
an=mult(an,tmp);
}
tmp=mult(tmp,tmp);
n>>=;
}
return an;
} int main()
{
int n,m,k;
Matrix a;
scanf("%d %d %d",&n,&k,&m);
M=m;
a.line=n;
a.column=n;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
scanf("%d",&a.m[i][j]);
}
}
Matr ans(a);
Matr ans2=fast_matrix(ans,k-);
for(int i=;i<ans2.line;i++){
for(int j=;j<ans2.column/;j++){
printf("%d ",ans2.m[i][j]);
}
printf("\n");
}
return ;
}

nyoj_299_Matrix Power Series_矩阵快速幂的更多相关文章

  1. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  2. 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] ...

  3. POJ3233:Matrix Power Series(矩阵快速幂+二分)

    http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k ...

  4. POJ 3233:Matrix Power Series 矩阵快速幂 乘积

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 18450   Accepted:  ...

  5. POJ3233:Matrix Power Series(矩阵快速幂+递推式)

    传送门 题意 给出n,m,k,求 \[\sum_{i=1}^kA^i\] A是矩阵 分析 我们首先会想到等比公式,然后得到这样一个式子: \[\frac{A^{k+1}-E}{A-E}\] 发现要用矩 ...

  6. POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 27277   Accepted:  ...

  7. POJ3233 Matrix Power Series(矩阵快速幂+分治)

    Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. ...

  8. POJ3233Matrix Power Series(矩阵快速幂)

    题意 题目链接 给出$n \times n$的矩阵$A$,求$\sum_{i = 1}^k A^i $,每个元素对$m$取模 Sol 考虑直接分治 当$k$为奇数时 $\sum_{i = 1}^k A ...

  9. POJ 3233 Matrix Power Series(矩阵快速幂)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...

随机推荐

  1. flask 知识点总结

    ============================request对象的常用属性============================具体使用方法如下:request.headers, requ ...

  2. 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解

    序,线程是比进程小的进程,非常广泛的被使用. 一.继承Thread实现线程操作 1.注意setDaemon(boolean)方法,参数为true时为守护线程,参数为false时为用户线程. 守护线程的 ...

  3. 预处理prepareStatement是怎么防止sql注入漏洞的?

    序,目前在对数据库进行操作之前,使用prepareStatement预编译,然后再根据通配符进行数据填值,是比较常见的做法,好处是提高执行效率,而且保证排除SQL注入漏洞. 一.prepareStat ...

  4. Ruby中Block, Proc, 和Lambda

    Block Blocks就是存放一些可以被执行的代码的块,通常用do...end 或者 {}表示 例如: [1, 2, 3].each do |num| puts num end [1, 2, 3]. ...

  5. Springmvc + mybatis + spring 配置,spring事物

    今天配置了半天,发现,事物不起效果,主要出现如下错误: org.mybatis.spring.transaction.SpringManagedTransaction] - [JDBC Connect ...

  6. PHP跳转页面的几种实现方法详解

    •PHP页面跳转一.header()函数header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器.header() ...

  7. leetcode 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  8. Pythhon 字典 key in dict 比 dict.has_key (key)效率高 为什么?

    has_key是去取key对应的值,时间复杂度在最优情况下为O(1); in 是直接去dict.__contains__这个保存这key的list中去获取,相当与是去数组中获取. 所以in 比has_ ...

  9. [OpenJudge0054]特务会议召开

    [OpenJudge0054]特务会议召开 试题描述 在敌占区的特务时常会碰头.敌占区有n个城市,为保证安全,中央经过侦查,只选择了n-1条较安全的道路作为特务们碰头可以走的道路.每次开会,中央会选择 ...

  10. 他们在军训,我在搞 OI(Ending)

    Day 7 上午看看数学书,老师让我把导数相关的概念学了.这也没有多高大上,就是一坨公式需要背,什么 (a)' = 0 啦,什么 (xn)' = n·xn-1 啦,什么 sin'(x) = cos(x ...