# Luogu P5303 [GXOI/GZOI2019]逼死强迫症

50pts暴力代码

#include<cstdio>
#define RI register int
using namespace std;
const int N=1e5+5,mod=1e9+7;
int t,n,ans,fib[N],sum[N];
inline void init(void)
{
sum[0]=fib[0]=fib[1]=1; for (RI i=sum[1]=2;i<N;++i)
fib[i]=(fib[i-2]+fib[i-1])%mod,sum[i]=(sum[i-1]+fib[i])%mod;
}
int main()
{
for (scanf("%d",&t),init();t;--t)
{
RI i; for (scanf("%d",&n),ans=0,i=3;i<=n;++i)
(ans+=2LL*sum[i-3]*fib[n-i]%mod)%=mod; printf("%d\n",ans);
}
return 0;
}

100pts矩乘CODE

#include<cstdio>
#include<cstring>
#define RI register int
#define CI const int&
using namespace std;
const int N=5,mod=1e9+7;
struct Matrix
{
int n,m; long long mat[N][N];
inline Matrix(CI N=0,CI M=0) { n=N; m=M; memset(mat,0,sizeof(mat)); }
inline long long* operator [] (CI x) { return mat[x]; }
friend inline Matrix operator * (Matrix A,Matrix B)
{
Matrix C(A.n,B.m); for (RI i=0;i<C.n;++i)
for (RI j=0;j<C.m;++j) for (RI k=0;k<A.m;++k)
(C[i][j]+=A[i][k]*B[k][j])%=mod; return C;
}
friend inline Matrix operator ^ (Matrix A,long long p)
{
Matrix T(A.n,A.m); for (RI i=0;i<A.n;++i) T[i][i]=1;
for (;p;p>>=1LL,A=A*A) if (p&1) T=T*A; return T;
}
}; int t,n;
int main()
{
for (scanf("%d",&t);t;--t)
{
scanf("%d",&n); if (n<=2) { puts("0"); continue; }
Matrix S(5,1),D(5,5); S[2][0]=S[3][0]=1; S[4][0]=2;
D[0][0]=D[0][1]=D[1][0]=D[2][2]=D[2][3]=D[3][2]=D[4][4]=1;
D[0][2]=2; D[0][4]=mod-1; S=(D^(n-1))*S; printf("%d\n",S[0][0]);
}
return 0;
}
posted @ 2019-06-01 21:22 hl666 阅读(...) 评论(...) 编辑 收藏