POJ 3233 Matrix Power Series(矩陣+二分)
題目大意:求由矩陣 A構成的矩陣 S = A + A^2 + A^3 + … + A^k。k的取值範圍是:10^9資料很大,應該二分。
對於一個k來說,s(k) = (1+A^(k/2)) *( A+A^2+……+A^(k/2))。如果k為奇數的話需要加上A^(k/2 + 1)。
所以二分求和,複雜度就降下來了,當然還得用到矩陣快速冪。
Matrix Power Series
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 15477 | Accepted: 6621 |
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 nnonnegative 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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
///#define mod 9973
int mod;
const int maxn = 2010;
using namespace std;
struct matrix
{
int f[40][40];
};
matrix mul(matrix a, matrix b, int n)///矩陣乘法
{
matrix c;
memset(c.f, 0, sizeof(c.f));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
for(int k = 0; k < n; k++) c.f[i][j] += a.f[i][k]*b.f[k][j];
c.f[i][j] %= mod;
}
}
return c;
}
matrix pow_mod(matrix a, int b, int n)///矩陣快速冪
{
matrix s;
memset(s.f, 0 , sizeof(s.f));
for(int i = 0; i < n; i++) s.f[i][i] = 1;
while(b)
{
if(b&1) s = mul(s, a, n);
a = mul(a, a, n);
b >>= 1;
}
return s;
}
matrix Add(matrix a,matrix b, int n) ///矩陣加法
{
matrix c;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
c.f[i][j] = a.f[i][j]+b.f[i][j];
c.f[i][j] %= mod;
}
}
return c;
}
matrix Matrix_Sum(matrix a, int k, int n)
{
if(k == 1) return a;
matrix dx,dy;
dx = Matrix_Sum(a, k/2, n);///二分,遞迴;
if(k&1)
{
dy = pow_mod(a, k/2+1, n);
dx = Add(dx, mul(dx, dy, n), n);
dx = Add(dy,dx, n);
}
else
{
dy = pow_mod(a, k/2, n);
dx = Add(dx,mul(dx, dy, n), n);
}
return dx;
}
int main()
{
int n, k;
while(cin >>n>>k>>mod)
{
matrix c;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
scanf("%d",&c.f[i][j]);
c.f[i][j] %= mod;
}
}
matrix d = Matrix_Sum(c, k, n);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-1; j++) cout<<d.f[i][j]<<" ";
cout<<d.f[i][n-1]<<endl;
}
}
return 0;
}
相關文章
- POJ 3233 Matrix Power Series (矩陣快速冪+等比數列二分求和)矩陣
- 【矩陣乘法】Matrix Power Series矩陣
- Cellular Matrix 蜂窩矩陣(一)矩陣
- POJ 3613 Cow Relays 矩陣乘法Floyd+矩陣快速冪矩陣
- flutter佈局-5-Matrix4矩陣變換Flutter矩陣
- 張量(Tensor)、標量(scalar)、向量(vector)、矩陣(matrix)矩陣
- 動手畫混淆矩陣(Confusion Matrix)(含程式碼)矩陣
- POJ1743 Musical Theme(字尾陣列 二分)陣列
- 非科班程式設計師才不知道的矩陣Matrix程式設計師矩陣
- poj--2778DNA Sequence+AC自動機+矩陣快速冪矩陣
- SciTech-Matrix Analysis of Management+Theory-管理科學的“矩陣式分析”矩陣
- 巨大的矩陣(矩陣加速)矩陣
- 鄰接矩陣、度矩陣矩陣
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- POJ 2749 二分 + 2-SAT
- POJ - 3041 Asteroids 【二分圖匹配】AST
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 矩陣矩陣
- 求任意矩陣的伴隨矩陣矩陣
- 矩陣和陣列矩陣陣列
- 理解矩陣矩陣
- 海浪矩陣矩陣
- 矩陣相乘矩陣
- 稀疏矩陣矩陣
- 螺旋矩陣矩陣
- 矩陣乘法矩陣
- 8.6 矩陣?矩陣
- 找矩陣矩陣
- 矩陣分解矩陣
- 二分類問題中混淆矩陣、PR以及AP評估指標矩陣指標
- POJ--2406Power Strings+KMP求字串最小週期KMP字串
- 快手矩陣管理平臺,矩陣管理有方法矩陣
- 機器學習中的矩陣向量求導(五) 矩陣對矩陣的求導機器學習矩陣求導
- POJ-3061 Subsequence(字首和+二分/尺取)
- 矩陣:如何使用矩陣操作進行 PageRank 計算?矩陣
- 演算法學習:矩陣快速冪/矩陣加速演算法矩陣
- 高斯消除矩陣矩陣
- 矩陣求導矩陣求導
- 置換矩陣矩陣