【BZOJ4161】—Shlw loves matrixI(常系数齐次线性递推)

传送门

模板题

#include<bits/stdc++.h>
using namespace std;
#define gc getchar
inline int read(){
    char ch=gc();
    int res=0,f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
#define re register
#define pb push_back
#define cs const
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline void Dec(int &a,int b){(a-=b)<0?(a+=mod):0;}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){
    for(;b;b>>=1,a=mul(a,a))(b&1)&&(res=mul(res,a));return res;
}
inline void chemx(ll &a,ll b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=4005;
int n,k;
int a[N],h[N],p[N];
int c[N],ans[N];
inline void calc(int *a,int *b,int *res){
    static int tmp[N];
    memset(tmp,0,sizeof(tmp));
    for(int i=0;i<k;i++)
    for(int j=0;j<k;j++)
    Add(tmp[i+j],mul(a[i],b[j]));
    for(int i=2*k-2;i>=k;i--){
        for(int j=k-1;~j;j--)
        Dec(tmp[i+j-k],mul(tmp[i],p[j]));
        tmp[i]=0;
    }
    for(int i=0;i<k;i++)res[i]=tmp[i];
}
int main(){
    n=read(),k=read();
    for(int i=1;i<=k;i++)a[i]=(read()+mod)%mod;
    for(int i=0;i<k;i++)h[i]=(read()+mod)%mod;
    for(int i=1;i<=k;i++)p[k-i]=dec(0,a[i]);p[k]=1;
    c[1]=1,ans[0]=1;
    for(;n;n>>=1,calc(c,c,c))if(n&1)calc(ans,c,ans);
    int res=0;
    for(int i=0;i<k;i++)Add(res,mul(ans[i],h[i]));
    cout<<res;
}

posted @ 2019-08-10 11:30  Stargazer_cykoi  阅读(102)  评论(0编辑  收藏  举报