The 2019 Asia Nanchang First Round Online Programming Contest H. The Nth Item(矩阵快速幂+优化)

题意:
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Macarons_i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值