Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
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
给定矩阵A,求A + A^2 + A^3 + … + A^k的结果
/*
矩阵乘法经典题.
一开始并没有想出来orz.
发现正解好神奇.
这种题就应该先推出递推式子再构造矩阵.
本来还想矩阵套矩阵来着
弱啊.
学习了一下单位矩阵的用法.
题解:http://www.cnblogs.com/justPassBy/p/4448630.html
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 61
#define LL long long
using namespace std;
LL n,m,k,ans[MAXN][MAXN],b[MAXN][MAXN],c[MAXN][MAXN];
LL mul(LL x,LL y)
{
LL tot=0;
while(y)
{
if(y&1) tot=(tot+x)%m;
x=(x+x)%m;
y>>=1;
}
return tot;
}
void mi()
{
while(k)
{
if(k&1)
{
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
for(int k=1;k<=n*2;k++)
c[i][j]=(c[i][j]+ans[i][k]*b[k][j]%m)%m;
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
ans[i][j]=c[i][j],c[i][j]=0;
}
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
for(int k=1;k<=n*2;k++)
c[i][j]=(c[i][j]+b[i][k]*b[k][j]%m)%m;
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
b[i][j]=c[i][j],c[i][j]=0;
k>>=1;
}
}
void slove()
{
k--;
mi();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<ans[i][j]<<" ";
printf("\n");
}
}
void Clear()
{
memset(b,0,sizeof b);
memset(ans,0,sizeof ans);
}
int main()
{
while(~scanf("%lld%lld%lld",&n,&k,&m))
{
Clear();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>b[i][j];b[i][j]%=m;
ans[i][j]=ans[i][n+j]=b[i][j];
}
b[n+i][i]=b[n+i][n+i]=1;
}
slove();
}
return 0;
}
Poj 3233 Matrix Power Series(矩阵乘法)的更多相关文章
- poj 3233 Matrix Power Series(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- 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 矩阵快速幂
设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 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 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 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的迹(就是主对角线上各项的 ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
随机推荐
- quartz2.3.0(八)使用日历排除不应该执行任务的时间段
Job任务类 package org.quartz.examples.example8; import java.util.Date; import org.slf4j.Logger; import ...
- 从create-react-app开始,构建项目架构
1.生成项目 命令行执行:create-react-app myapp,生成如下结构: 2.安装sass依赖,让你在项目中可以使用scss模块化,index.module.scss: npm i n ...
- ④ Python3.0字符串
字符串无论是python或者其他语言,是最常用的数据类型之一: 这儿注意在python中可以通过使用引号( ' 或 " )来创建字符串.使用三引号('''或""" ...
- 【面试突击】- Mybatis-#{}和${}的区别
原文链接:mybatis中#{}和${}的区别 1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时 ...
- JavaScript字符串Format
一直用C#编程,在日常字符串拼接中string.Format()一直是个很好用很常用的方法,不用自己+++,既影响开发效率也影响可读性 然而在js中并没有这样的函数可供使用,so整理了一个js的字符串 ...
- Oracle表数据转换为XML格式数据
转自:https://blog.csdn.net/smile_caijx/article/details/83352927 使用DBMS_XMLGEN可以解决问题 SELECT DBMS_XMLGEN ...
- Map转url ,url转Map工具类
/** * 将url参数转换成map * @param param aa=11&bb=22&cc=33 * @return */ public static Map<String ...
- 转 Python3 ssl模块不可用的问题
编译安装完Python3之后,使用pip来安装python库,发现了如下报错: $ pip install numpy pip is configured with locations tha ...
- ASP.NET 取得 Request URL 的各个部分和通过ASP.NET获取URL地址的方法
网址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc Request.ApplicationPath / Request.Phys ...
- I、Mac 下的Vue
Mac 下的Vue 1. 安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/i ...