POJ-3233 Matrix Power Series 矩阵A^1+A^2+A^3...求和转化
S(k)=A^1+A^2...+A^k.
保利求解就超时了,我们考虑一下当k为偶数的情况,A^1+A^2+A^3+A^4...+A^k,取其中前一半A^1+A^2...A^k/2,后一半提取公共矩阵A^k/2后可以发现也是前一半A^1+A^2...A^k/2。因此我们可以考虑只算其中一半,然后A^k/2用矩阵快速幂处理。对于k为奇数,只要转化为k-1+A^k即可。n为矩阵数量,m为矩阵大小,复杂度O[(logn*logn)*m^3]
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#define LL long long
using namespace std; struct mx
{
LL n, m;
LL c[][];//需要根据题目开大
void initMath(LL _n)//初始化方阵
{
m = n = _n;
}
void initOne(LL _n)//初始化单位矩阵
{
m = n = _n;
for (LL i = ; i<n; i++)
for (LL j = ; j<m; j++)
c[i][j] = (i == j);
}
void print()//测试打印
{
for (LL i = ; i<n; i++)
{
for (LL j = ; j < m; j++)
{
cout << c[i][j];
if (j != m - )cout << ' ';
}
cout << endl;
}
}
};
int mod = ;
mx Mut(mx a, mx b)
{
mx c;
c.n = a.n, c.m = b.m;
for (LL i = ; i<a.n; i++)
for (LL j = ; j<b.m; j++)
{
LL sum = ;
for (LL k = ; k<b.n; k++)
sum += a.c[i][k] * b.c[k][j], sum %= mod;
c.c[i][j] = sum;
}
return c;
}
mx fastMi(mx a, LL b)
{
mx mut; mut.initOne(a.n);
while (b)
{
if (b % != )
mut = Mut(mut, a);
a = Mut(a, a);
b /= ;
}
return mut;
}
LL n, k;
mx a, ans, b;
mx s(LL kx)
{
if (kx == )
{
return a;
}
if (kx % ==)
{
mx p = s(kx / );
mx y = fastMi(a, kx/);
y = Mut(y,p);
for (int i = ; i < n; i++)for (int j = ; j < n; j++)
{
y.c[i][j] += p.c[i][j];
y.c[i][j] %= mod;
}
return y;
}
else
{
mx p = s(kx-);
mx y = fastMi(a, kx);
for (int i = ; i < n; i++)for (int j = ; j < n; j++)
{
y.c[i][j] += p.c[i][j];
y.c[i][j] %= mod;
}
return y;
}
}
int main(int argc, const char * argv[]) {
cin.sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
cin >> n >> k;
b.initMath(n);
ans.initMath(n);
a.initMath(n);
for(int i=;i<n;i++)
for (int j = ; j < n; j++)
{
cin >> a.c[i][j];
}
ans = s(k);
ans.print();
}
return ;
}
POJ-3233 Matrix Power Series 矩阵A^1+A^2+A^3...求和转化的更多相关文章
- 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(矩阵二分,高速幂)
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 ...
随机推荐
- C++ 11 多线程下std::unique_lock与std::lock_guard的区别和用法
这里主要介绍std::unique_lock与std::lock_guard的区别用法 先说简单的 一.std::lock_guard的用法 std::lock_guard其实就是简单的RAII封装, ...
- java JFR
1. 参数: -XX:+UnlockCommercialFeatures -XX:+FlightRecorder 2. 运行命令: jcmd <PID> JFR.start name=te ...
- Parallels Desktop 重装系统
安装教程,大家可以在网上找找 现在我想重装系统,怎么弄呢? 1.~/Documents/Parallels 目录下那个PVM后缀的文件直接删除 2.重装找开虚拟机,会弹出一个框,说找不到系统,点击删除 ...
- HDU 1160
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...
- GDscript风格指南
(惯例感谢godot开发组~~·) 缩进 缩进类型:Tabs (编辑器默认) 缩进大小:4 (编辑器默认) 每个缩进级别必须大于包含它的代码块. 良好的: for i in range(10): pr ...
- Spring整合MyBatis(简单登录Demo)
SpringMvc简单整合(登录模块) 1.1 整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得s ...
- solr简单搜索案例
solr简单搜索案例 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字搜索商品信息,根据商品分类.价格过滤搜索结果,也可以根据价格进行排序,实现分页. 架构分为: 1. solr服务器 2. ...
- ajax return 的问题
平时都是在AJAX里执行逻辑,实然想到能不能return返回数据呢? ajax 是异步请求,return拿值得时候 ajax并没有取到值,所以是undefind. 需要把ajax的请求方式改为同步 v ...
- 解决 Bash On Windows 下载慢或无法下载的问题
解决 Bash On Windows "无法从 Windows 应用商店下载.请检查网络连接."的问题 Fiddler和Bash On Windows 源离线压缩包:http:// ...
- shift键有什么用?怎么用?shift键的妙用
一.当你用QQ和别人聊天时,是不是有时信息发送的特别慢呀,不要紧,只要你发信息时按shift 键信息就会很快的发送出去的! 二.当你面对一大堆窗口,却要一个一个把它们关掉时.是不是很烦啊.只要你按sh ...