[CF520E]Pluses everywhere

题目

传送门

题解

每个数对答案的贡献单独考虑.

对于一个数 \(a_p\),它的贡献只和它后面填的第一个加号的距离有关,当第一个加号的位置出现在离它的距离为 \(i\),它的贡献就固定了,为 \(10^i\times a_p\),而其他的加号可以在除了从 \(p\)\(p+i\) 这一段的任意空位中填(因为要保证我们固定填的加号是第一个出现在 \(a_p\) 之后的),所以就可以算出这样的贡献,为

\[10^i\times a_p\times C_{n-1-(i-p+1)}^{k-1} \]

为了方便处理,我们重新定义 \(i\) 为在 \(a_p\) 之后的 \(a_i\) 后面填上了一个加号,这时 \(a_p\) 的贡献,那么我们就有

\[Ans=\sum_{p=1}^n\left (\sum_{i=p}^{n} a_p\times 10^{i-p}\times {{n+p-i-1-[i\neq n]}\choose k-[i\neq n]}\right ) \]

至于为什么组合数中有 \([i\neq n]\),是因为我们在最后一个数后面填的 \(+\) 是虚假的,只是为了帮助我们计算答案而非真实存在,故算其他括号的时候不能将其计算上.

这个柿子显然是 \(\mathcal O(n^2)\) 的,我们不能接受,现在考虑化简这个式子.

\(j=i-p\),那么就有 \(i=j+p\).

由于 \(i\le n\),故 \(j+p\le n\)\(j\le n-p\),我们用 \(j\) 重新写这个式子:

\[\begin{aligned} Ans&=\sum_{p=1}^n\left (\sum_{i=p}^{n} a_p\times 10^{i-p}\times {{n+p-i-1-[i\neq n]}\choose k-[i\neq n]}\right ) \\ &=\sum_{p=1}^n\left (\sum_{j=0}^{n-p} a_p\times 10^j\times {{n+p-(j+p)-1-[j+p\neq n]}\choose k-[j+p\neq n]}\right ) \\ &=\sum_{p=1}^n\left (\sum_{j=0}^{n-p} a_p\times 10^j\times {{n-j-1-[j+p\neq n]}\choose k-[j+p\neq n]}\right ) \\ &=\sum_{j=0}^n\sum_{p=1}^{n-j}a_p\times 10^j\times {{n-j-1-[j+p\neq n]}\choose k-[j+p\neq n]} \\ &=\sum_{j=0}^n10^j\times \sum_{p=1}^{n-j}a_p\times {{n-j-1-[j+p\neq n]}\choose k-[j+p\neq n]} \\ \end{aligned} \]

由于 \([j+k\neq n]\) 太烦了,我们考虑将其单独处理,那么式子就变成了

\[\begin{aligned} Ans&=\sum_{j=0}^n10^j\times \sum_{p=1}^{n-j}a_p\times {{n-j-1-[j+p\neq n]}\choose k-[j+p\neq n]} \\ &=\sum_{j=0}^n10^j\times \left [ \sum_{p=1}^{n-j-1}a_p\times {{n-j-2}\choose {k-1}} +a_p\times {{n-p-1}\choose k}\right ] \\ &=\sum_{j=0}^n10^j\times \left [ {{n-j-2}\choose {k-1}}\sum_{p=1}^{n-j-1}a_p +a_{n-j}\times {{n-p-1}\choose k}\right ] \end{aligned} \]

其中,\(10^j\) 可以预处理,而 \(\sum_{p=1}^{n-j-1}a_p\) 即前缀和,这样式子就是 \(\mathcal O(n)\) 的了.

代码

#include<cstdio>

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define fi first
#define se second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x<y?y:x;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int mod=1e9+7;
const int maxn=1e5;

inline int qkpow(int a,int n){
    int ret=1;
    for(;n>0;n>>=1,a=1ll*a*a%mod)if(n&1)ret=1ll*ret*a%mod;
    return ret;
}

int fac[maxn+5],invfac[maxn+5];
int pow10[maxn+5];
inline void Init(){
    fac[0]=1;rep(i,1,maxn)fac[i]=1ll*fac[i-1]*i%mod;
    invfac[maxn]=qkpow(fac[maxn],mod-2);
    fep(i,maxn-1,1)invfac[i]=1ll*invfac[i+1]*(i+1)%mod;
    invfac[0]=1;
    pow10[0]=1;rep(i,1,maxn)pow10[i]=1ll*pow10[i-1]*10%mod;
}
inline int C(const int n,const int m){
    if(n<m || n<0 || m<0)return 0;
    if(n==0 || n==m)return 1;
    return 1ll*fac[n]*invfac[m]%mod*invfac[n-m]%mod;
}

int n,k;
int a[maxn+5];
int pre[maxn+5];

signed main(){
    Init();
    n=read(1),k=read(1);
    rep(i,1,n)scanf("%1d",&a[i]);
    rep(i,1,n){
        pre[i]=pre[i-1]+a[i];
        if(pre[i]>=mod)pre[i]-=mod;
    }
    int ans=0;
    rep(j,0,n){
        int sum=1ll*C(n-j-2,k-1)*pre[n-j-1]%mod+1ll*a[n-j]*C(n-j-1,k)%mod;
        if(sum>=mod)sum-=mod;
        ans=ans+1ll*pow10[j]*sum%mod;
        if(ans>=mod)ans-=mod;
    }
    writc(ans,'\n');
    return 0;
}
posted @ 2020-09-08 20:41  Arextre  阅读(134)  评论(0编辑  收藏  举报