poj 3233 S = A + A^2 + A^3 + … + A^k A是一个n X n矩阵 (矩阵快速幂)
S = A + A^2 + A^3 + … + A^k A是一个n*n矩阵
Sample Input
2 2 4 //n k MOD
0 1
1 1
Sample Output
1 2
2 3
先求 I + A + A^2 + A^3 + … + A^k I为单位矩阵
我们来设置这样一个矩阵 B=
A I
O I
其中O是零矩阵,I是单位矩阵
将它乘方,得到
A^2 I+A
O I
乘三方,得到
A^3 I+A+A^2
O I
乘四方,得到
A^4 I+A+A^2+A^3
O I
然后B^(k+1) 的右上小矩阵 就是 I + A + A^2 + A^3 + … + A^k
记得 数组开大一点...因为要扩展成2n*2n的矩阵, 第一次开小了,数组越界=.=
# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; int MOD ;
int n ; struct Matrix
{
LL mat[][];
}; Matrix mul(Matrix a,Matrix b)
{
Matrix c;
for(int i=;i<*n;i++)
for(int j=;j<*n;j++)
{
c.mat[i][j]=;
for(int k=;k<*n;k++)
{
c.mat[i][j]=(c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%MOD;
}
}
return c;
}
Matrix pow_M(Matrix a,int k )
{
Matrix ans;
memset(ans.mat,,sizeof(ans.mat));
for (int i=;i<*n;i++)
ans.mat[i][i]=;
Matrix temp=a;
while(k)
{
if(k&)ans=mul(ans,temp);
temp=mul(temp,temp);
k>>=;
}
return ans;
} int main ()
{
//freopen("in.txt","r",stdin) ;
int k ;
while(cin>>n>>k>>MOD)
{
Matrix A ;
int i , j ;
for (i = ; i < n ; i++)
for (j = ; j < n ; j++)
cin>>A.mat[i][j] ;
Matrix B ;
memset(B.mat,,sizeof(B.mat));
for (i = ; i < n ; i++) //扩展成2n * 2n的矩阵
{
for (j = ; j < n ; j++)
{
B.mat[i][j] = A.mat[i][j] ;
}
B.mat[n+i][n+i] = ;
B.mat[i][n+i] = ; }
B = pow_M(B,k+) ;
LL t ;
for (i = ; i < n ; i++)
for (j = ; j < n ; j++)
{
t = B.mat[i][n+j] % MOD ;
if (i == j)
t = (t + MOD - )%MOD ;
if (j+ != n)
cout<<t<<" " ;
else
cout<<t<<endl ;
} } return ;
}
poj 3233 S = A + A^2 + A^3 + … + A^k A是一个n X n矩阵 (矩阵快速幂)的更多相关文章
- Poj 3233 Matrix Power Series(矩阵二分快速幂)
题目链接:http://poj.org/problem?id=3233 解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k ...
- 矩阵儿快速幂 - POJ 3233 矩阵力量系列
不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...
- POJ 3233 Matrix Power Series (矩阵+二分+二分)
题目地址:http://poj.org/problem?id=3233 题意:给你一个矩阵A,让你求A+A^2+……+A^k模p的矩阵值 题解:我们知道求A^n我们可以用二分-矩阵快速幂来求,而 当k ...
- 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 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- poj 3233 Matrix Power Series 矩阵求和
http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- 矩阵十点【两】 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 矩阵快速幂
地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方 结果模m的相加和是多少 Given a n × n matrix A and a positive i ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
随机推荐
- keepalive+nginx 热备跟负载均衡
结构图 keepalived配置 master跟backup除了state跟优先级,其它一样,优先级master需大于backup ! Configuration File for keepalive ...
- vue自学入门-2(vue创建项目)
本人也是刚学习VUE,边找资料,边学习,边给大家分享.1.创建项目 2.启动项目 3.注意上面和下面全部用cnpm
- Linux安装mysql过程(转+完善)
http://blog.csdn.net/jerome_s/article/details/52883234yum 安装MySQL 1. 检查安装情况 查看有没有安装过: ...
- mongoDB - 日常操作三
MongoDB 进程控制 进程控制 db.currentOp() # 查看活动进程 db.$cmd.sys.inprog.findOne() # 查看活动进程 与上面一样 opid # 操作进程号 o ...
- Attempting to badge the application icon but haven't received permission from the user to badge the application错误解决办法
今天刚刚学习UIApplication对象,当我希望利用这个对象在我们的应用图标上显示个数字的时候,xcode报了这个错误: 解决办法 : - (IBAction)applicationClicke ...
- ResNet152网络复现(Caffe)
一.准备数据集 1) 下载数据集 Imagnet网站上下载了三类图片,分别是big cat.dog.fish,其中训练集的图片数一共是4149,测试集的图片数是1003,训练集和测试集的图片数比例4 ...
- Django开发笔记二
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.xadmin添加主题.修改标题页脚和收起左侧菜单 # ...
- 【转】Python之列表生成式、生成器、可迭代对象与迭代器
[转]Python之列表生成式.生成器.可迭代对象与迭代器 本节内容 语法糖的概念 列表生成式 生成器(Generator) 可迭代对象(Iterable) 迭代器(Iterator) Iterabl ...
- linux内核capable源代码分析【转】
转自:https://blog.csdn.net/sanwenyublog/article/details/50856849 linux内核里对于进程的权限管理有一个很重要的函数capable,以前看 ...
- wget安装pip和pip3
pip的安装 1.1 pip下载 wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2 ...