poj 3233 Matrix Power Series (矩阵快速幂+分治)
题意:
Given a n × n matrix A and a positive integer k, find the sum S = A + _A_2 + _A_3 + … + Ak.
思路: 对k进行二分。
比如,当k=6时,有: A + A^2 + A^3 + A^4 + A^5 + A^6 =(A + A^2 + A^3) + A^3*(A + A^2 + A^3) 应用这个式子后,规模k减小了一半。我们二分求出A^3后再递归地计算A + A^2 + A^3,即可得到原问题的答案。
以及错误的递归方式:
无形中增加了多少次。。。。。。怎么像小学生呢。。。
正确的写法。。。
/* ***********************************************
Author :111qqz
Created Time :Tue 18 Oct 2016 05:10:53 PM CST
File Name :code/poj/3233.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=35;
int n,k,mod;
struct Mat
{
int mat[N][N];
void clear()
{
ms(mat,0);
}
void out()
{
for ( int i = 0 ; i < n ; i++ )
{
for ( int j = 0 ; j <n-1 ; j++) printf("%d ",mat[i][j]);
printf("%d\n",mat[i][n-1]);
}
}
}M;
Mat operator * ( Mat a ,Mat b )
{
Mat c;
c.clear();
for ( int i = 0 ; i < n ; i++)
for ( int j = 0 ; j < n; j ++)
for ( int k = 0 ; k < n; k++)
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%mod;
return c;
}
Mat operator + (Mat a,Mat b)
{
Mat c;
c.clear();
for ( int i = 0 ; i < n ;i ++)
for ( int j = 0 ; j < n ; j++)
c.mat[i][j] = (a.mat[i][j] + b.mat[i][j])%mod;
return c;
}
Mat operator ^ (Mat a,int b)
{
Mat c;
for ( int i = 0 ; i < n ; i++)
for ( int j = 0 ;j <n ; j++)
c.mat[i][j] = (i==j);
while (b>0)
{
if (b&1) c = c * a;
b = b >> 1;
a = a* a;
}
return c;
}
Mat solve( int k)
{
if (k==1) return M;
Mat res;
res.clear();
for ( int i = 0 ; i < n ; i++) res.mat[i][i] = 1;
res = res + (M^(k>>1));
res = res * solve(k>>1);
if (k%2==1) res = res + (M^k);
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif
scanf("%d%d%d",&n,&k,&mod);
for ( int i = 0 ; i < n ; i++)
for ( int j = 0 ; j < n ; j++)
scanf("%d",&M.mat[i][j]);
Mat ans;
ans.clear();
ans = solve(k);
ans.out();
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}