poj 3233 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(矩阵二分,高速幂)的更多相关文章
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- 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://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- 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(矩阵等比求和)
题目链接 模板题. #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 ...
- [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: ...
随机推荐
- C# 实现图像快速 水平 垂直 翻转
C# 图像水平,垂直翻转的方法,速度很快 重新把图像绘制成翻转的方法 /// <summary> /// 图像水平翻转 /// </summary> /// <para ...
- es6总结(九)--Iterator & for of
- file中的一些常用方法
1.exists();判断文件(目录)是否存在 2.mkdir();创建一级目录:mkdirs()创建多级目录 3.delete();删除文件(目录) 4.isDirectory();判断是否是一个目 ...
- 一个页面多个ng-app注意事项
1.一个页面会自动加载第一个ng-app 2.如果想启动其它ng-app,需要通过下列代码的红色部分来启动,此时一共启动了2个ng-app 3.特别注意:代码红色部分一定要放在最后,比如,不能放在蓝色 ...
- iOS内购总结
内购流程: 1. 用户先拿到购买产品的单子, 2. 拿着单子去苹果那里交钱,交完钱让苹果在单子上盖个章 3.拿着盖了章的单子传给自己的服务器来验证是否真的支付成功,服务器是跟苹果验证(我们客户端也是可 ...
- 安卓WebView在项目中总结
一.简介 在安卓开发中我们会遇到许多处理网页的功能,以下就是我在实际中用到的分享给大家 1.显示和渲染Web页面 2.可和JavaScript交互调用 二.常用的方法 //激活WebView为活跃状态 ...
- 多协议底层攻击工具Yesinia
多协议底层攻击工具Yesinia Yesinia是一款底层协议攻击工具.它提供多种运行模式,如终端文本模式.GTK图形模式.NCurses模式.守护进程模式.它利用各种底层协议的漏洞实施攻击,支持 ...
- [LibreOJ β Round #4] 子集
显然是个二分图,直接求最大独立就行了. #include<bits/stdc++.h> #define ll long long #define pb push_back using na ...
- (入门SpringBoot)SpringBoot结合redis(四)
SpringBoot整合redis: 1.引入jar <!-- 引入redis依赖 --><dependency> <groupId>org.springf ...
- Flex 布局教程学习
转载自:阮一峰的网络日志(http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html) 网页布局(layout)是 CSS 的一个重点应用. 布局 ...