POJ 3233 Matrix Power Series (矩陣快速冪+等比數列二分求和)
Matrix Power Series
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
POJ Monthly--2007.06.03, Huang, Jinsong
|
題意:
求 S = A + A2 + A3 + … + Ak.%m
POINT:
等比數列二分求和:
(1)當時,
(2)當時,那麼有
(3)當時,那麼有
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define ll long long
int n,m;
struct mx
{
int c[33][33];
};
mx chen(mx a,mx b)
{
mx ans;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
ans.c[i][j]=0;
for(int k=1;k<=n;k++)
{
(ans.c[i][j]+=a.c[i][k]*b.c[k][j])%=m;
}
}
}
return ans;
}
mx add(mx a,mx b)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
(a.c[i][j]+=b.c[i][j])%=m;
}
}
return a;
}
mx mxqkm(mx base,int mi)
{
mx ans;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
ans.c[i][j]=i==j;
}
}
while(mi)
{
if(mi&1) ans=chen(ans,base);
mi>>=1;
base=chen(base,base);
}
return ans;
}
mx sum(mx A,int k)
{
if(k == 1) return A;
mx t=sum(A,k/2);
if(k&1)
{
mx cur=mxqkm(A,k/2+1);
return add(t,add(chen(t,cur),cur));
}
else
{
mx cur=mxqkm(A,k/2);
return add(t,chen(cur,t));
}
}
int main()
{
int k;
while(~scanf("%d %d %d",&n,&k,&m))
{
mx a;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a.c[i][j]);
a.c[i][j]%=m;
}
}
a=sum(a,k);
for(int i=1;i<=n;i++)
{
int p=0;
for(int j=1;j<=n;j++)
{
if(!p) printf("%d",a.c[i][j]);
else printf(" %d",a.c[i][j]);
p++;
}
printf("\n");
}
}
}
相關文章
- POJ 3233 Matrix Power Series(矩陣+二分)矩陣
- POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩陣快速冪取模)矩陣
- 【矩陣乘法】Matrix Power Series矩陣
- HDU 4965 Fast Matrix Calculation(矩陣快速冪)AST矩陣
- POJ 3613 Cow Relays 矩陣乘法Floyd+矩陣快速冪矩陣
- POJ 3150 Cellular Automaton(矩陣快速冪)矩陣
- 矩陣快速冪矩陣
- 矩陣快速冪(快忘了)矩陣
- 矩陣快速冪總結矩陣
- POJ 2778-DNA Sequence(AC自動機+構建鄰接矩陣+矩陣快速冪)矩陣
- 【矩陣乘法】【快速冪】遞推矩陣
- poj--2778DNA Sequence+AC自動機+矩陣快速冪矩陣
- BZOJ 3329 Xorequ:數位dp + 矩陣快速冪矩陣
- HDU 4549 M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- HDU 3059 Fibonacci數列與矩陣求和 矩陣大小不固定矩陣
- HDU 1575 Tr A(矩陣快速冪)矩陣
- HDU 4565 So Easy!(矩陣快速冪)矩陣
- HDU 4686 (推公式+矩陣快速冪)公式矩陣
- HDU 4549M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- 費馬小定理 + 費馬大定理 + 勾股數的求解 + 快速冪 + 矩陣快速冪 【模板】矩陣
- 斐波那契數列的第N項(1≤n≤10^18 矩陣快速冪)矩陣
- Python Numpy的陣列array和矩陣matrixPython陣列矩陣
- P3390 【模板】矩陣快速冪矩陣
- 【二維樹狀陣列】poj 2155 Matrix陣列
- HDU 2157 How many ways?? (矩陣快速冪)矩陣
- HDU 1005 Number Sequence(矩陣快速冪)矩陣
- HDU 2256Problem of Precision(矩陣快速冪)矩陣
- 從斐波那契到矩陣快速冪矩陣
- HDU 1575 Tr A【矩陣快速冪取模】矩陣
- HDU 1005 Number Sequence:矩陣快速冪矩陣
- HDU5411CRB and Puzzle(矩陣快速冪)矩陣
- HDU3221Brute-force Algorithm(矩陣快速冪&&指數降冪)Go矩陣
- bzoj3240: [Noi2013]矩陣遊戲(矩陣乘法+快速冪)矩陣遊戲
- LightOJ 1070 Algebraic Problem:矩陣快速冪 + 數學推導AI矩陣
- HDU 2276 - Kiki & Little Kiki 2 (矩陣快速冪)矩陣
- HDU 4291 A Short problem(矩陣快速冪+迴圈節)矩陣
- UVA 10655 Contemplation! Algebra (矩陣快速冪)矩陣
- BZOJ3329: Xorequ(二進位制數位dp 矩陣快速冪)矩陣