Codeforces 866G : Flowers and Chocolate

传送门

题解:
考虑我们已知k步能够拼接出的状态,怎么才能得到答案.

可以DP: fnfnbi f n → f n − b i

这其实是一个多项式取模的过程,我们对 F=xmaxbijx(maxbi)bj F = x max b i − ∑ j x ( max b i ) − b j 取模即可。 最后再对小的部分做一次DP,取 f0 f 0 即为答案。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int RLEN=1<<18|1;
inline char nc() {
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob) ? -1 : *ib++;
} 
inline LL rd() {
    char ch=nc(); LL i=0,f=1;
    while(!isdigit(ch)) {if(ch=='-')f=-1l; ch=nc();}
    while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=nc();}
    return i*f;
}

const int mod=1e9+7;
inline int add(int x,int y) {return (x+y>=mod) ? (x+y-mod) : (x+y);}
inline int dec(int x,int y) {return (x-y<0) ? (x-y+mod) : (x-y);}
inline int mul(int x,int y) {return (LL)x*y%mod;}
inline int power(int a,int b,int rs=1) {for(;b;b>>=1,a=mul(a,a)) if(b&1) rs=mul(rs,a); return rs;}

const int N=120;
int n,m,a[N],b[N]; LL k;
struct poly {
    vector <int> a;
    int deg;
    poly(int d=0) {deg=d; a.resize(d+1);}
    friend inline poly operator *(const poly &a,const poly &b) {
        poly c(a.deg+b.deg);
        for(int i=0;i<=a.deg;i++)
            for(int j=0;j<=b.deg;j++)
                c.a[i+j]=add(c.a[i+j],mul(a.a[i],b.a[j]));
        return c;
    }
    friend inline poly operator +(const poly &a,const poly &b) {
        int d=max(a.deg,b.deg); poly c(d);
        for(int i=0;i<=a.deg;i++) c.a[i]=add(a.a[i],c.a[i]);
        for(int i=0;i<=b.deg;i++) c.a[i]=add(b.a[i],c.a[i]);
        return c;
    }
    friend inline poly operator %(const poly &a,const poly &b) {
        if(a.deg<b.deg) return a;
        poly c=a;
        const int inv=power(b.a.back(),mod-2);
        for(int i=c.deg;i>=b.deg;i--) {
            int t=mul(c.a[i],inv);
            for(int p=i,j=b.deg;j>=0;p--,j--)
                c.a[p]=dec(c.a[p],mul(b.a[j],t));
        }
        c.deg=b.deg-1; c.a.resize(c.deg+1); 
        return c;
    }
}p1,p2;

int main() {
    n=rd(), m=rd(), k=rd();
    for(int i=1;i<=n;i++) a[i]=rd();
    for(int i=1;i<=m;i++) b[i]=rd();
    int d=*max_element(b+1,b+m+1);
    p2=poly(d); p2.a[d]=1;
    for(int i=1;i<=m;i++) p2.a[d-b[i]]=add(p2.a[d-b[i]],mod-1);
    for(int i=1;i<=n;i++) {
        poly val(1); val.a[1]=1; 
        poly c(0); c.a[0]=1; 
        for(;a[i];a[i]>>=1,val=val*val%p2) 
            if(a[i]&1) c=c*val%p2;
        p1=p1+c;
    } poly c(0); c.a[0]=1;
    for(;k;k>>=1,p1=p1*p1%p2) 
        if(k&1) c=c*p1%p2;
    for(int i=c.deg;i>=0;i--)
        for(int j=1;j<=m;j++)
            if(b[j]<=i) c.a[i-b[j]]=add(c.a[i-b[j]],c.a[i]);
    cout<<c.a[0]<<'\n';

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值