[bzoj4161]Shlw loves matrixI

题目大意

常系数齐次递推

生成函数

构造特征多项式
然后用快速幂做多项式取模
多项式乘法暴力即可

#include
#include
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int maxn=4000+10,mo=1000000007;
int a[maxn],b[maxn],c[maxn],d[maxn],e[maxn],f[maxn],o[maxn],ans[maxn],one[maxn],sta[80];
int i,j,k,l,t,n,m,top,num;
void FFT(int *a,int *b,int *c){
    int i,j;
    fo(i,0,k-1) e[i]=a[i],f[i]=b[i];
    fo(i,0,2*k-2) o[i]=0;
    fo(i,0,k-1)
        fo(j,0,k-1)
            o[i+j]=(o[i+j]+(ll)a[i]*b[j])%mo;
    fd(i,2*k-2,k){
        fo(j,1,k) o[i-j]=(o[i-j]-(ll)o[i]*d[k-j])%mo;
        o[i]=0;
    }
    fo(i,0,k-1) c[i]=o[i];
}
int main(){
    scanf("%d%d",&n,&k);
    fo(i,1,k) scanf("%d",&c[i]);
    fo(i,0,k-1) scanf("%d",&a[i]);
    d[k]=1;
    fd(i,k-1,0) d[i]=-c[k-i];
    b[1]=1;
    ans[0]=1;
    while (n){
        sta[++top]=n%2;
        n/=2;
    }
    while (top){
        FFT(ans,ans,ans);
        if (sta[top]) FFT(ans,b,ans);
        top--;
    }
    fo(i,0,k-1) num=(num+(ll)ans[i]*a[i])%mo;
    (num+=mo)%=mo;
    printf("%d\n",num);
}

你可能感兴趣的:(快速幂,快速傅里叶变换)