题目链接:[点击打开链接](http://poj.org/problem?id=3233)
题意:求S[k] = A + A^2 + ..... + A^k
利用矩阵快速幂可以很快的求出A矩阵的k次方, 但是该题是求和, 如果还按照原来的方法, 将要计算k次, 复杂度无法承受。
我们可以构造一个矩阵 (A 0)
(E E)
此时令S[k] = E + A + A^2 + ..... + A^(k-1)
那么 ( A^k ) ( A 0)(A^(k-1)) (A 0 )^k (E)
= =
(S[k] ) (E E)(S[k-1] ) (E E ) (0)
那么, 我们只要计算出S[k+1] = E + A + .... + A^k
然后将对角线元素-1就行了。
细节参见代码:
~~~
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int mod = 10007;
const int maxn = 100;
int T,n,m,k;
typedef vector<int> vec;
typedef vector<vec> mat;
mat mul(mat &a, mat &b) {
mat c(a.size(), vec(a[0].size()));
for(int i = 0; i < a.size(); i++) {
for(int k = 0; k < b.size(); k++) {
for(int j = 0; j < b[0].size(); j++) {
c[i][j] = (c[i][j] + a[i][k]*b[k][j]) % m;
}
}
}
return c;
}
mat pow(mat a, ll n) {
mat b(a.size(), vec(a[0].size()));
for(int i = 0; i < a.size(); i++) {
b[i][i] = 1;
}
while(n > 0) {
if(n & 1) b = mul(b, a);
a = mul(a, a);
n >>= 1;
}
return b;
}
int main() {
scanf("%d%d%d",&n,&k,&m);
mat a(2*n, vec(2*n));
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
scanf("%d",&a[i][j]);
}
}
for(int i=n;i<2*n;i++) {
for(int j=0;j<2*n;j++)
if(i - n == j || i == j) a[i][j] = 1;
}
a = pow(a, k+1);
for(int i=n;i<2*n;i++) {
for(int j=0;j<n;j++) {
if(i - n == j) a[i][j] = (a[i][j] - 1 + m) % m;
printf("%d%c",a[i][j], j == n-1 ? '\n' : ' ');
}
}
return 0;
}
~~~
- 前言
- 1608 - Non-boring sequences(折半递归。。暂且这么叫吧)
- 11491 - Erasing and Winning(贪心)
- 1619 - Feel Good(高效算法-利用数据结构优化-优先队列)
- hdu-4127 Flood-it!(IDA*算法)
- UESTC 1132 酱神赏花 (用数据结构优化DP)
- HDU 2874 Connections between cities(LCA离线算法)
- Codeforces Round #317 A. Lengthening Sticks(组合+容斥)
- HDU 3085 Nightmare Ⅱ(双向BFS)
- HDU 5592 ZYB&#39;s Premutation(二分+树状数组)
- Codeforces Round #320 (Div. 1) C. Weakness and Poorness(三分)
- HDU 5212 Code(容斥)
- HDU 5596 GTW likes gt(multiset)
- FZU 2159 WuYou(贪心)
- HDU 3450 Counting Sequences(DP + 树状数组)
- HDU 5493 Queue(二分+树状数组)
- HDU 1166 敌兵布阵(线段树版)
- HDU 1394 Minimum Inversion Number(树状数组||线段树)
- HDU 2795 Billboard(线段树)
- POJ 2828 Buy Tickets(树状数组)
- 《完全版线段树》- NotOnlySuccess
- POJ 2886 Who Gets the Most Candies?(树状数组+二分)
- HDU 1698 Just a Hook(线段树区间修改)
- POJ 3468 A Simple Problem with Integers(线段树|区间加减&amp;&amp;区间求和)
- POJ 2528 Mayor&#39;s posters(线段树区间修改+离散化)
- HDU 5606 tree(并查集)
- POJ 3734 Blocks(矩阵优化+DP)
- POJ 3233 Matrix Power Series(矩阵优化)
- HDU 5607 graph(矩阵优化+概率DP)
- POJ 2777 Count Color(线段树区间修改+位运算)
- POJ 1436 Horizontally Visible Segments(线段树区间修改)
- UVA 1513 - Movie collection(树状数组)
- UVA 1232 - SKYLINE(线段树区间更新)
- 11525 - Permutation(二分+树状数组)
- 11402 - Ahoy, Pirates!(线段树区间更新(标记重叠的处理))
- Educational Codeforces Round 6 E. New Year Tree(DFS序+线段树)