BZOJ 5118

时间:2022-03-09 09:21:57

矩阵乘也是可以欧拉定理的HHH

所以幂次就是$(2^n-1) ~ mod ~ \varphi(p)$就好了

const ll p=1125899839733759ll;
inline ll mu(ll n,ll m,ll p){ll r=0;for(;m;m>>=1,n=(n+n)%p)if(m&1)r=(n+r)%p;return r;}
inline ll pw(ll n,ll m){ll r=1;for(;m;m>>=1,n=mu(n,n,p-1))if(m&1)r=mu(r,n,p-1);return r;}
#define prm(a) //{For(i,0,2)For(j,0,2)printf("%lld%c",(a)[i][j]," \n"[j==1]);}
template<int H>struct Matrix{
ll a[H][H];Matrix(ll t=0){For(i,0,H)For(j,0,H)a[i][j]=(i!=j?0:t);}
ll*operator[](const int&t){return a[t];}
Matrix operator*(const Matrix&t)const{
Matrix r;
For(k,0,H)For(i,0,H)For(j,0,H)
r[i][j]=(r[i][j]+mu(a[i][k],t.a[k][j],p))%p;
return r;
}
Matrix operator^(ll k){Matrix r(1),n=*this;prm(r);for(;k;k>>=1,n=n*n)if(k&1)r=r*n;return r;}
};
Matrix<2>a;
int main(){
int T;ll n;cin>>T;
while(T--){
cin>>n;
n=pw(2ll,n)-1;
a[0][0]=a[0][1]=a[1][0]=1,a[1][1]=0;
a=a^(n);
prm(a);
cout<<a[0][0]<<endl;
}
}

还有一个奇怪的操作:

$\sqrt{5}$ 在这个模数的意义下是存在的,所以直接带入 $f_n = \dfrac{\sqrt{5}}{5}[(\dfrac{\sqrt{5}+1}{2})^n+(\dfrac{1-\sqrt{5}}{5})^n]$