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. python 连接ORacle11g

    一,准备软件 (1)cx_Oracle (2)instantclient 注: (1)instantclient是Oracle客户端,plsql称为数据库第三方可视化工具,即便装了plsql也还是要装 ...

  2. 用java输出杨辉三角

    杨辉三角:它的两个边都是1,内部其它都是肩上两个数的和 第一种: package aaa; public class YangHui { public static void main(String[ ...

  3. ArcMap与快捷键冲突

    ArcMap与快捷键冲突 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 问题:armap进入鼠标自动导航状态,arcmap失控,系统紊乱,导致 ...

  4. Visual Studio 2019更新到16.1.4

    Visual Studio 2019更新到16.1.4 此次更新仍然是修复bug为主,涉及到Xcode 11无法启动模拟器.Xamarin.Android SDK 9.3.0.23的调试bug.

  5. mysql 安装参考

    https://blog.csdn.net/qq_38756992/article/details/84929787 https://www.cnblogs.com/joyny/p/10991194. ...

  6. Flutter利用GridView实现网格的商品布局

    GridView.count 生成的是静态网格 效果: 代码: import 'package:flutter/material.dart'; void main() { runApp(MyApp() ...

  7. getField和getDeclaredField的区别

    这两个方法都是用于获取字段getField 只能获取public的,包括从父类继承来的字段.getDeclaredField 可以获取本类所有的字段,包括private的,但是不能获取继承来的字段. ...

  8. 为什么在MySQL数据库中无法创建外键?(MyISAM和InnoDB详解)

    问题描述:为什么在MySQL数据库中不能创建外键,尝试了很多次,既没有报错,也没有显示创建成功,真实奇了怪,这是为什么呢? 问题解决:通过查找资料,每次在MySQL数据库中创建表时默认的情况是这样的: ...

  9. Swift4.0复习操作符方法与操作符的定制

    1.对已有操作符的重载: 2.可定制的操作符: 3.定制前缀操作符: 4.定制后缀操作符: 5.定制中缀操作符:

  10. Andrew Ng机器学习课程9

    Andrew Ng机器学习课程9 首先以一个工匠为例,说明要成为一个出色的工匠,就需要掌握各种工具的使用,才能知道在具体的任务中选择什么工具来做.所以今天要讲的就是机器学习的理论部分. bias va ...