题意:
F(n)=3F(n-1)+2F(n-2),F(0)=0,F(1)=1;
给出N1和Q,需要求出F(N1)^F(N2) ^… ^F(Nk)
其中Ni=(F(Ni-1)*F(Ni-1))^Ni-1;
(结果取模)
题解:
对于F(i)直接矩阵快速幂求出,但Q的大小是1e7,直接暴力求必T
然而模数很小,所以直接记录下求出来的F(n)就可以大大节约时间了
code:
#include<bits/stdc++.h>
#define ll long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define MS(x,y) memst(x,y,sizeof(x))
using namespace std;
const int mod=998244353;
const int maxn=3;
unordered_map<ll,ll>f;
unordered_map<ll,ll>fx;
unordered_map<ll,ll>fy;
struct Matrix{
ll f[maxn][maxn],n,m;
Matrix(){memset(f,0,sizeof f);}
void O(){memset(f,0,sizeof f);}
void E(){fo(i,1,m)f[i][i]=1;}
};
inline Matrix operator *(Matrix a,Matrix b){
Matrix c;c.n=a.n;c.m=b.m;
fo(i,1,c.n)fo(j,1,c.m)fo(k,1,b.n)
c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j]%mod)%mod;
return c;
}
inline Matrix power(Matrix a,ll p){
Matrix c;c.n=a.n;c.m=a.m;c.E();
while(p){
if(p&1)c=c*a;
a=a*a;
p>>=1;
}
return c;
}
ll Get_f(ll x,ll last){
if(f[x])return f[x];
Matrix e;
e.m=e.n=2;
e.f[1][1]=3;e.f[1][2]=2;e.f[2][1]=1;
if(!last||x-last-1<0){
e=power(e,x-1);
fx[x]=e.f[1][1];
fy[x]=e.f[1][2];
return f[x]=fx[x];
}
e=power(e,x-last);
fx[x]=(fx[last]*e.f[1][1]%mod+fy[last]*e.f[2][1]%mod)%mod;
fy[x]=(fx[last]*e.f[1][2]%mod+fy[last]*e.f[2][2]%mod)%mod;
return f[x]=fx[x];
}
int main(){
ll q,n;
scanf("%lld%lld",&q,&n);
ll ans=0;
ll last=0;
while(q--){
ll p=Get_f(n,last);
last=n;
ans=ans^p;
n=(p*p)^n;
}
ans%=mod;
printf("%lld",ans);
return 0;
}