BJFU 1440 fudq的ACM

时间:2021-12-17 05:35:54

矩阵快速幂

 #include<cstdio>
#include<algorithm>
using namespace std;
const int Nmax = ;
const int INF =1e9;
const int mod=; long long n;
int error;
struct Matrix
{
int n,m;
long long map[Nmax][Nmax];
Matrix(int x,int y)
{
n=x;m=y;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
map[i][j]=;
}
Matrix operator * (const Matrix b)
{
Matrix c(n,b.m);
if(m==b.n)
{
for(int i=;i<=c.n;i++)
for(int j=;j<=c.m;j++)
for(int k=;k<=m;k++)
c.map[i][j]=(c.map[i][j]+(map[i][k]*b.map[k][j])%mod)%mod;
return c;
}
printf("error!!!!!!!!!!!!!!\n");
}
}; Matrix get(long long n)
{
Matrix base(,);
base.map[][]=;
base.map[][]=;
base.map[][]=;
base.map[][]=;
Matrix ans(,);
ans.map[][]=;
ans.map[][]=;
ans.map[][]=;
ans.map[][]=; while(n>)
{
if(n & )
ans=ans*base;
base=base*base;
n>>=;
} return ans;
} int main()
{
//freopen("bjfu.in","r",stdin);
Matrix base(,);
base.map[][]=;
base.map[][]=; while(scanf("%I64d",&n)==)
{
if(n>=)
{
Matrix ans=get(n-);
ans=ans*base;
printf("%I64d\n",ans.map[][]);
}
else
{
printf("%I64d\n",base.map[-n][]);
}
}
return ;
}