Matrix Power Series
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 20309   Accepted: 8524

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

POJ Monthly--2007.06.03, Huang, Jinsong

题目意思:

已知n,k,m,S = A + A2 + A3 + … + Ak求:S%m

解题思路:

模板题,快速幂取模+二分优化。

补充一个关于等比序列分治的结论:

对于 
Sn=(A^1+A^2+A^3+……+A^(n-1)+A^n) mod p 
当n为偶数的时候 
s[n]=(1+A^(n/2))* (A^1+A^2+A^3+……+A^(n/2)) =(1+A^(n/2))*S[n/2] 
当n为奇数的时候 
s[n]=(1+A^((n-1)/2+1))* (A^1+A^2+A^3+……+A^(n-1)/2)+A^((n-1)/2+1] 
=(1+A^((n-1)/2+1))* S[(n-1)/2] + A^((n-1)/2+1) =(1+A^(n/2+1))*S[n/2] + A^(n/2+1)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0xfffffff
using namespace std;
const long long MAXN=;
long long n,mod;
struct Mat
{
long long m[MAXN][MAXN];
};
Mat a,per;
void init()
{
long long i,j;
for(i=; i<n; ++i)
for(j=; j<n; ++j)
{
cin>>a.m[i][j];
a.m[i][j]%=mod;
per.m[i][j]=(i==j);
}
} Mat mul(Mat A,Mat B)
{
Mat ans;long long i,j,k;
for(i=; i<n; i++)
for(j=; j<n; j++)
{
ans.m[i][j]=;
for(k=; k<n; k++)
ans.m[i][j]+=(A.m[i][k]*B.m[k][j]);
ans.m[i][j]%=mod;
}
return ans;
}
Mat power(long long k)
{
Mat p,ans=per;
p=a;
while(k)
{
if(k&)
{
ans=mul(ans,p);
--k;
}
else
{
k/=;
p=mul(p,p);
}
}
return ans;
} Mat add(Mat a,Mat b)
{
Mat c;long long i,j;
for(i=; i<n; ++i)
for(j=; j<n; ++j)
c.m[i][j]=(a.m[i][j]+b.m[i][j])%mod;
return c;
}
Mat sum(long long k)
{
if(k==) return a;
Mat temp,b;
temp=sum(k/);
if(k&)
{
b=power(k/+);
temp=add(temp,mul(temp,b));
temp=add(temp,b);
}
else
{
b=power(k/);
temp=add(temp,mul(temp,b));
}
return temp;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
long long i,j,k;
while(cin>>n>>k>>mod)
{
init();
Mat ans=sum(k);
for(i=; i<n; ++i)
{
for(j=; j<n-; ++j)
cout<<ans.m[i][j]<<" ";
cout<<ans.m[i][j]<<endl;
}
}
return ;
}

POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)的更多相关文章

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

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

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

  3. POJ - 3233 Matrix Power Series (矩阵等比二分求和)

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

  4. POJ 3233 Matrix Power Series (矩阵乘法)

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

  5. [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:  ...

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

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

  7. 线性代数(矩阵乘法):POJ 3233 Matrix Power Series

    Matrix Power Series   Description Given a n × n matrix A and a positive integer k, find the sum S = ...

  8. POJ 3233 Matrix Power Series(二分等比求和)

    Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...

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

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

随机推荐

  1. 浏览器cookie数 跨站请求伪造 欧盟Cookie指令

    <?php for ($w=0; $w < 200 ; $w++) { setcookie('name'.$w,'value'.$w, time()+3600*10 ); } var_du ...

  2. 深入浅出ES6教程『async函数』

    大家好,本人名叫苏日俪格,大家叫我 (格格) 就好,在上一章节中我们学到了Symbol & generator的用法,下面我们一起来继续学习async函数: async [ə'zɪŋk]:这个 ...

  3. openresty开发系列12--lua介绍及常用数据类型简介

    openresty开发系列12--lua介绍及常用数据类型简介 lua介绍  1993 年在巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de ...

  4. 002-06-RestTemplate-请求示例-form、json、multipart、okhttp3

    一.概述 请求示例集合 服务端:https://github.com/bjlhx15/common-study.git 中的 http-client-webserver 服务端:RequestBody ...

  5. 转:HR schema

    ###createe RemRem $Header: hr_cre.sql 29-aug-2002.11:44:03 hyeh Exp $RemRem hr_cre.sqlRemRem Copyrig ...

  6. 【441】JSON format

      Ref: json -- JSON encoder and decoder JSON(JavaScript Object Notation) can help us to see data mor ...

  7. 123457123457#0#----com.DoraGame.ShiZi75--前拼后广--识字dora

    com.DoraGame.ShiZi75--前拼后广--识字dora

  8. Python第一阶段04

    1.文件操作: # 指明编码 f = open("sisi", encoding="utf-8") # 读 data = f.read() print(data ...

  9. prometheus数据格式

    注意区分以下两种“数据格式”: 1.自定义exporter的时候所需要遵循的给prometheus提供数据的数据格式: https://yunlzheng.gitbook.io/prometheus- ...

  10. 【warning】set the environment variable MXNET_CUDNN_AUTOTUNE_DEFAULT to 0 to disable

    前言 运行mxnet程序的时候出现这个warning信息,但是不影响整个程序的运行,不过宝宝喜欢将warning信息尽量也clear,强迫症嘛?!哈哈哈哈 问题描述 [::] src/operator ...