CodeForces512C-Pluses everywhere-模拟/数学/排列组合模板

时间:2021-03-14 03:49:51

经过研究可以发现,每一位的贡献是C(n-2,k-1)+C(n-3,k-1)...C(k-1,k-1)

同时还要注意加号全部在左边的情况。

这里还用了O(n)预处理O(1)组合数的模板。//妙啊。。妙。。。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long using namespace std; const int MOD = 1e9+;
const int maxn = 3e5+; LL f[maxn],inv[maxn],fac[maxn]; void init()
{
fac[] = fac[] = inv[] = inv[] = f[] = f[] = ;
for(int i=;i<maxn;i++)
{
fac[i] = fac[i-]*i%MOD;
LL t = MOD/i,k = MOD%i;
f[i] = (MOD-t)*f[k]%MOD;
inv[i] = inv[i-]*f[i]%MOD;
}
} LL C(LL n,LL m)
{
if(n < m || m < ) return ;
return fac[n]*inv[m]%MOD*inv[n-m]%MOD;
} char s[maxn];
int N,K;
LL save[maxn]; int main()
{
init();
while(~scanf("%d%d",&N,&K))
{
scanf("%s",s);
LL base = ;
for(int i=;i<=N;i++)
{
save[i] = (save[i-]+C(N--i,K-)*base%MOD)%MOD;
base = base*%MOD;
}
LL ans = ;base = ;
for(int i=N-;i>=;i--)
{
ans += (s[i]-'')*(save[N--i]+C(i,K)*base%MOD)%MOD;
ans %= MOD;
base = base*%MOD;
}
printf("%I64d\n",ans);
}
}