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 (矩陣快速冪+等比數列二分求和)矩陣
- POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩陣快速冪取模)矩陣
- 【矩陣乘法】Matrix Power Series矩陣
- Cellular Matrix 蜂窩矩陣(一)矩陣
- goldengate 認證矩陣matrixGo矩陣
- Spark Distributed matrix 分散式矩陣Spark分散式矩陣
- HDU 4920 Matrix multiplication(矩陣相乘)矩陣
- 矩陣類 poj3420矩陣
- POJ 3613 Cow Relays 矩陣乘法Floyd+矩陣快速冪矩陣
- Python Numpy的陣列array和矩陣matrixPython陣列矩陣
- 【二維樹狀陣列】poj 2155 Matrix陣列
- [CareerCup] 1.7 Set Matrix Zeroes 矩陣賦零矩陣
- HDU 4965 Fast Matrix Calculation(矩陣快速冪)AST矩陣
- (原創)一般矩陣 Matrix類矩陣
- POJ 3744 概率dp+矩陣矩陣
- 旋轉矩陣(Rotate Matrix)的性質分析矩陣
- poj1050 最大子矩陣和矩陣
- flutter佈局-5-Matrix4矩陣變換Flutter矩陣
- POJ 1050-To the Max(最大子矩陣和)矩陣
- 動手畫混淆矩陣(Confusion Matrix)(含程式碼)矩陣
- 張量(Tensor)、標量(scalar)、向量(vector)、矩陣(matrix)矩陣
- POJ 3150 Cellular Automaton(矩陣快速冪)矩陣
- NMF 非負矩陣分解(Non-negative Matrix Factorization)實踐矩陣
- POJ 2778-DNA Sequence(AC自動機+構建鄰接矩陣+矩陣快速冪)矩陣
- POJ1743 Musical Theme(字尾陣列 二分)陣列
- 【Ac自動機+矩陣加速】poj 2778 DNA Sequence矩陣
- poj 2778 AC自動機與矩陣連乘矩陣
- 非科班程式設計師才不知道的矩陣Matrix程式設計師矩陣
- POJ3744 Scout YYF I (概率DP + 矩陣優化)矩陣優化
- 生成螺旋矩陣(方陣、矩陣)矩陣
- [CareerCup] 11.6 Search a 2D Matrix 搜尋一個二維矩陣矩陣
- 鄰接矩陣、度矩陣矩陣
- POJ 2155-Matrix(二維樹狀陣列-區間修改 單點查詢)陣列
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- poj--2778DNA Sequence+AC自動機+矩陣快速冪矩陣
- POJ3070 Fibonacci[矩陣乘法]【學習筆記】矩陣筆記
- POJ 2406-Power Strings(重複子串-KMP中的next陣列)KMP陣列
- 推公式+二分--poj1759公式