LuoGu P3390 【模板】矩阵快速幂
板子传送门
今天想去搞一下这个矩阵快速幂嘛(因为考试T2要用矩阵加速,不会QwQ所以来学一学)
然后写成了这个样子:
#include <iostream>
#include <cstring>
#include <cstdio>
#define F(i,a,b) for(int i=a;i<=b;++i)
#define ll long long
using namespace std;
const ll mod=1e9+7;
const ll N=1e3+5;
struct _MatriX{ll mx[N][N];};
ll n,k;_MatriX f;_MatriX p;
inline _MatriX MatriX_mul(_MatriX a,_MatriX b){
_MatriX t;
F(i,1,n) F(j,1,n) t.mx[i][j]=0;
F(i,1,n) F(j,1,n) F(k,1,n) t.mx[i][j]=(t.mx[i][j]%mod+(a.mx[i][k]%mod*b.mx[k][j]%mod)%mod)%mod;
return t;
}
inline _MatriX quick(_MatriX a,ll mi){
_MatriX t=f;
while(mi){
if(mi&1) t=MatriX_mul(t,a);
a=MatriX_mul(a,a);mi>>=1;
}
return t;
}
int main(){
scanf("%lld%lld",&n,&k);
F(i,1,n) F(j,1,n) scanf("%lld",&p.mx[i][j]);
F(i,1,n) f.mx[i][i]=1;
_MatriX ans=quick(p,k);
F(i,1,n){
F(j,1,n) printf("%lld ",ans.mx[i][j]%mod);
putchar('\n');
}
return 0;
}
看起来挺对的不是吗?但是它崩掉了哎
为什么呢?因为......const ll N=1e3+5;这一句,开大了,似乎是导致了爆栈
但是emmm算了半天都只有30MB啊(4个N*N的long long数组啊)
不管怎么样,改成1e2+5就行了,A掉了(虽然跑的死慢死慢的)
May you return with a young heart after years of fighting.