Matrix Power Series
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 15739   Accepted: 6724

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

当k为奇数时

Sk = A + A2 + A3 + … + Ak  

    =(1+Ak/2)*(A + A2 + A3 + … + Ak/2  )+{Ak}

    =(1+Ak/2)*(Sk/2 )+{Ak}

当k为偶数时

Sk = A + A2 + A3 + … + Ak  

    =(1+Ak/2)*(A + A2 + A3 + … + Ak/2  )+{Ak}

    =(1+Ak/2)*(Sk/2 )

就能够二分递归求Sk

代码:

//829ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
struct matrix
{
int ma[50][50];
} a;
matrix multi(matrix x,matrix y)//矩阵相乘
{
matrix ans;
memset(ans.ma,0,sizeof(ans.ma));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(x.ma[i][j])//稀疏矩阵优化
for(int k=1; k<=n; k++)
{
ans.ma[i][k]=(ans.ma[i][k]+x.ma[i][j]*y.ma[j][k])%m;
}
}
}
return ans;
}
matrix add(matrix x,matrix y)//矩阵相加
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
x.ma[i][j]=(x.ma[i][j]+y.ma[i][j])%m;
return x;
}
matrix pow(matrix a,int m)
{
matrix ans;
for(int i=1; i<=n; i++) //单位矩阵
{
for(int j=1; j<=n; j++)
{
if(i==j)
ans.ma[i][j]=1;
else
ans.ma[i][j]=0;
}
}
while(m)//矩阵高速幂
{
if(m&1)
{
ans=multi(ans,a);
}
a=multi(a,a);
m=(m>>1);
}
return ans;
}
matrix solve(matrix x,int k)//递归求Sk
{
if(k==1)
return x;
matrix ans;
for(int i=1; i<=n; i++) //单位矩阵
{
for(int j=1; j<=n; j++)
{
if(i==j)
ans.ma[i][j]=1;
else
ans.ma[i][j]=0;
}
}
ans=add(ans,pow(x,k/2));
ans=multi(ans,solve(x,k/2));
if(k&1)
ans=add(ans,pow(x,k));
return ans;
}
int main()
{
int k;
while(~scanf("%d%d%d",&n,&k,&m))
{
matrix ans,a;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
scanf("%d",&a.ma[i][j]);
}
ans=solve(a,k);
for(int i=1; i<=n; i++)
{
for(int j=1; j<n; j++)
printf("%d ",ans.ma[i][j]);
printf("%d\n",ans.ma[i][n]);
}
}
return 0;
}

poj 3233 Matrix Power Series(矩阵二分,高速幂)的更多相关文章

  1. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  2. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

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

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

  4. poj 3233 Matrix Power Series 矩阵求和

    http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...

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

  6. POJ 3233 Matrix Power Series(矩阵等比求和)

    题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...

  7. 矩阵十点【两】 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的迹(就是主对角线上各项的 ...

  8. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  9. [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)

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

随机推荐

  1. BZOJ 4820 [Sdoi2017]硬币游戏 ——期望DP 高斯消元

    做法太神了,理解不了. 自己想到的是建出AC自动机然后建出矩阵然后求逆计算,感觉可以过$40%$ 用一个状态$N$表示任意一个位置没有匹配成功的概率和. 每种匹配不成功的情况都是等价的. 然后我们强制 ...

  2. Java众神之路(1)-语言介绍

    Java语言介绍 1.Java的历史 我个人认为,学习一种技术,不止要关注技术本身,也应该去了解一下它的发展史,这一方面是对技术本身的尊重,另一方面也是希望能够通过该技术的发展历史推测出其未来可能的发 ...

  3. foj Problem 2275 Game

    Problem D Game Accept: 145    Submit: 844Time Limit: 1000 mSec    Memory Limit : 262144 KB Problem D ...

  4. 乘法逆元__C++

    在开始之前我们先介绍3个定理: 1.乘法逆元(在维基百科中也叫倒数,当然是 mod p后的,其实就是倒数不是吗?): 如果ax≡1 (mod p),且gcd(a,p)=1(a与p互质),则称a关于模p ...

  5. vue.js源码学习分享(五)

    //配置项var config = { /** * Option merge strategies (used in core/util/options)//选项合并策略 */ optionMerge ...

  6. 【MFC】禁用鼠标拖拽标题栏移动窗口

    解决方案:重载WM_NCLBUTTONDOWN消息 (1) .h 文件 afx_msg void OnNcLButtonDown(UINT nHitTest, CPoint point); (2) . ...

  7. UVA - 10196:Check The Check

    类型:简单模拟 大致题意:已知国际象棋行棋规则,给你一个局面,问是否将军?谁将谁的军?(保证不会同时将军) 思路:都以小写字母 测试 是否将 大写字母. 然后一个局面测两次(一次直接测,一次反转棋盘, ...

  8. Java面向对象--static关键字

  9. ios 使用keychain具体方法

    Dictionary  写入: if ([self.currentUserAccount length] > 0) {                                Keycha ...

  10. SharePreferences使用

    获取数据: @SuppressLint("InlinedApi") private String getFromSharePreference(String key) { if ( ...