BM算法

第一次知道这种东西 有点强 但是不知道原理
例题:给定 n,k ,1≤n≤1018,1≤k≤100 求:

矩乘也可以做 式子拆开:

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define pb push_back
typedef long long ll;
#define SZ(x) ((ll)(x).size())
typedef vector<ll> VI;
typedef pair<ll,ll> PII;
const ll mod=998244353;
ll powmod(ll a,ll b) { ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1) { if(b&1)res=res*a%mod;a=a*a%mod; } return res; }
ll _;
namespace linear_seq 
{
    const ll N=10010;
    ll res[N],base[N],_c[N],_md[N];
    vector<ll> Md;
    void mul(ll *a,ll *b,ll k) 
    {
        rep(i,0,k+k) _c[i]=0;
        rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
        for (ll i=k+k-1;i>=k;i--) if (_c[i])
            rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
        rep(i,0,k) a[i]=_c[i];
    }
    ll solve(ll n,VI a,VI b) 
    { 
        // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
        //        printf("%d\n",SZ(b));
        ll ans=0,pnt=0;
        ll k=SZ(a);
        assert(SZ(a)==SZ(b));
        rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
        Md.clear();
        rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
        rep(i,0,k) res[i]=base[i]=0;
        res[0]=1;
        while ((1ll<<pnt)<=n) pnt++;
        for (ll p=pnt;p>=0;p--) 
        {
            mul(res,res,k);
            if ((n>>p)&1) 
            {
                for (ll i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
                rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
            }
        }
        rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
        if (ans<0) ans+=mod;
        return ans;
    }
    VI BM(VI s) {
        VI C(1,1),B(1,1);
        ll L=0,m=1,b=1;
        rep(n,0,SZ(s)) {
            ll d=0;
            rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
            if (d==0) ++m;
            else if (2*L<=n) {
                VI T=C;
                ll c=mod-d*powmod(b,mod-2)%mod;
                while (SZ(C)<SZ(B)+m) C.pb(0);
                rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
                L=n+1-L; B=T; b=d; m=1;
            } else {
                ll c=mod-d*powmod(b,mod-2)%mod;
                while (SZ(C)<SZ(B)+m) C.pb(0);
                rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
                ++m;
            }
        }
        return C;
    }
    ll gao(VI a,ll n) 
    {
        VI c=BM(a);
        c.erase(c.begin());
        rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
        return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
    }
};

// int main() 
// {
//     while (~scanf("%lld",&n)) 
//     {
//         vector<ll>v;
//         v.push_back(1);//前几项
//         v.push_back(5);
//         v.push_back(15);
//         v.push_back(35);
//         v.push_back(70);
//         v.push_back(126);
//         printf("%lld\n",linear_seq::gao(v,n-1));
//     }
// }
ll n,k,sum;
ll f[1010];
int main()
{
    vector<ll>v;
    scanf("%lld%lld",&n,&k);
    f[1]=1,sum=1;
    v.push_back(sum);
    for(int i=2;i<=1000;i++)
    {
        f[i]=(f[i-1]+f[i-2])%mod;
        ll pow=1;
        for(int j=1;j<=k;j++) pow=(pow*i)%mod;
        sum=(sum+pow*f[i])%mod;
        v.push_back(sum);
    }
    printf("%lld",linear_seq::gao(v,n-1));
}

发表评论

邮箱地址不会被公开。 必填项已用*标注