POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵
| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 27277 | Accepted: 11143 |
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
A^i是一个矩阵
很显然,把每个A^i算出来是不行的,所以我们得找找关系
因为这里牵扯到了矩阵相加求和,所以我们可以想到构建一个包含A的矩阵(只要包含A和两个一就行,这样是为了最后能得到A^1+A^2+...+A^K的式子)

其中1是单位矩阵,单位矩阵是左对角线为1的矩阵
然后容易得到:

可以看出这个分块矩阵的左下角那块就可以得到要求的解S
我们取这一块,再减去一个单位矩阵1即可。
参考博客:https://www.cnblogs.com/pdev/p/4063669.html
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 110;
const int mod = 2;
typedef long long ll;
struct matrix {
ll a[maxn][maxn];
};
matrix base, ans;
ll n, t, m;
matrix multip( matrix x, matrix y ) {
matrix tmp;
for( ll i = 0; i < 2*n; i ++ ) {
for( ll j = 0; j < 2*n; j ++ ) {
tmp.a[i][j] = 0;
for( ll k = 0; k < 2*n; k ++ ) {
tmp.a[i][j] = ( tmp.a[i][j] + x.a[i][k] * y.a[k][j] ) % m;
}
}
}
return tmp;
}
void f( ll x ) {
while( x ) {
if( x&1 ) {
ans = multip( ans, base );
}
base = multip( base, base );
x /= 2;
}
}
int main() {
while( cin >> n >> t >> m ) {
memset( ans.a, 0, sizeof(ans.a) );
memset( base.a, 0, sizeof(base.a) );
for( ll i = 0; i < n; i ++ ) {
for( ll j = 0; j < n; j ++ ) {
cin >> base.a[i][j];
}
}
for( ll i = n; i < 2*n; i ++ ) { //两个单位矩阵
base.a[i][i-n] = base.a[i][i] = 1;
}
//上面两个for循环是为了构建出新的包含A的矩阵
for( ll i = 0; i < 2*n; i ++ ) {
ans.a[i][i] = 1;
}
f(t+1); //由上面举的例子可以看出要求出n次方,得算n+1次
for( ll i = n; i < 2*n; i ++ ) {
for( ll j = 0; j < n; j ++ ) {
if( i == j+n ) {
ans.a[i][j] --;
}
if( j != n-1 ) {
cout << ( ans.a[i][j] + m ) % m << " ";
} else {
cout << ( ans.a[i][j] + m ) % m << endl;
}
}
}
}
return 0;
}
POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵的更多相关文章
- POJ3233 Matrix Power Series(快速幂求等比矩阵和)
题面 \(solution:\) 首先,如果题目只要我们求\(A^K\) 那这一题我们可以直接模版矩乘快速幂来做,但是它现在让我们求$\sum_{i=1}^{k}{(A^i)} $ 所以我们思考一下这 ...
- [POJ3233]Matrix Power Series 分治+矩阵
本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia [POJ3233]Matrix Power Series 分治+矩阵 题目大意 A为n×n(n<= ...
- 矩阵乘法&&矩阵快速幂&&最基本的矩阵模型——斐波那契数列
矩阵,一个神奇又令人崩溃的东西,常常用来优化序列递推 在百度百科中,矩阵的定义: 在数学中,矩阵(Matrix)是一个按照长方阵列排列的复数或实数集合 ,最早来自于方程组的系数及常数所构成的方阵.这一 ...
- POJ3233:Matrix Power Series(矩阵快速幂+二分)
http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k ...
- poj3233 Matrix Power Series(矩阵快速幂)
题目要求的是 A+A2+...+Ak,而不是单个矩阵的幂. 那么可以构造一个分块的辅助矩阵 S,其中 A 为原矩阵,E 为单位矩阵,O 为0矩阵 将 S 取幂,会发现一个特性: Sk +1右上角 ...
- 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 ...
- POJ3233]Matrix Power Series && [HDU1588]Gauss Fibonacci
题目:Matrix Power Series 传送门:http://poj.org/problem?id=3233 分析: 方法一:引用Matrix67大佬的矩阵十题:这道题两次二分,相当经典.首先我 ...
- poj3233—Matrix Power Series
题目链接:http://poj.org/problem?id=3233 题目意思:给一个矩阵n*n的矩阵A和一个k,求一个式子 S = A + A2 + A3 + … + Ak. 这个需要用到等比数列 ...
- POJ3233:Matrix Power Series(矩阵快速幂+递推式)
传送门 题意 给出n,m,k,求 \[\sum_{i=1}^kA^i\] A是矩阵 分析 我们首先会想到等比公式,然后得到这样一个式子: \[\frac{A^{k+1}-E}{A-E}\] 发现要用矩 ...
随机推荐
- Thymeleaf 模板 springboot集成使用
一.Thymeleaf是什么? 简单说, Thymeleaf 是一款用于渲染XML/XHTML/HTML5内容的模板引擎,类似我之前用过的FreeMarker .由于它支持 html 原型,然后在 h ...
- 旁友数独会伐啦?python秒解数独了解下伐啦?
前几天和隔壁邻居玩斗地主被发现了,牌被没收了,斗地主是斗不了了,但我还想和邻居玩耍.如果你还想斗斗地主,戳:趁老王不在,和隔壁邻居斗斗地主,比比大小 想破脑袋终于让我想到一个游戏,数独!什么叫数独?数 ...
- 洛谷P3572题解
这道题实在是一道 毒瘤 题,太坑爹了.那个写 \(deque\) 的题解亲测只有80分,原因 不言而明 ,这道题居然 丧心病狂 到 卡STL . 好了,不吐槽了,进入正题 题目分析: 这是一道十分 简 ...
- 7.源码分析---SOFARPC是如何实现故障剔除的?
我在服务端引用那篇文章里面分析到,服务端在引用的时候会去获取服务端可用的服务,并进行心跳,维护一个可用的集合. 所以我们从客户端初始化这部分说起. 服务连接的维护 客户端初始化的时候会调用cluste ...
- caddy & grpc(3) 为 caddy 添加一个 反向代理插件
caddy-grpc 为 caddy 添加一个 反向代理插件 项目地址:https://github.com/yhyddr/caddy-grpc 前言 上一次我们学习了如何在 Caddy 中扩展自己想 ...
- http测试工具
http测试工具: https://github.com/denji/awesome-http-benchmark wrk https://github.com/wg/wrk wrk2 https:/ ...
- Android——倒计时跳转+sharedpreferences
public class MainActivity extends Activity { // 3秒钟后,从图1跳转到图2(10) private Handler handler=new Handle ...
- JS鼠标吸粉特效
HTML <canvas id=canvas></canvas> CSS * { margin: 0; padding: 0; } html { overflow: hidde ...
- C++11以上的新特性整理
1.nullptr void foo(char *); void foo(int);foo(NULL) //编译出错,不知道调用哪个,可能调用了foo(int)foo(nullptr) //ok ,调 ...
- (十)c#Winform自定义控件-横向列表
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...