【CF521C】【排列组合】Pluses everywhere

时间:2022-12-25 22:37:23

Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out nnumbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect.

The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him!

Input

The first line contains two integers, n and k (0 ≤ k < n ≤ 105).

The second line contains a string consisting of n digits.

Output

Print the answer to the problem modulo 109 + 7.

Sample test(s)
input
C++
3 1
108
1
2
3 1
108
output
C++
27
1
27
input
C++
3 2
108
1
2
3 2
108
output
C++
9
1
9
Note

In the first sample the result equals (1 + 08) + (10 + 8) = 27.

In the second sample the result equals 1 + 0 + 8 = 9.

【分析】

排列组合计算每一位对答案的贡献。

 /*
宋代朱敦儒
《西江月·世事短如春梦》
世事短如春梦,人情薄似秋云。不须计较苦劳心。万事原来有命。
幸遇三杯酒好,况逢一朵花新。片时欢笑且相亲。明日阴晴未定。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const int MAXN = + ;
const long long MOD = ;
const double Pi = acos(-1.0);
long long G = ;//原根
const int MAXM = * + ;
using namespace std;
typedef long long ll;
void read(ll &x){//读入优化
char ch;x = ;
ll flag = ;
ch = getchar();
while (ch < '' || ch > '') {if (ch == '') flag = -; ch = getchar();}
while (ch >= '' && ch <= '') {x = x * + (ch - ''); ch = getchar();}
x *= flag;
}
//分别为阶乘和前缀和
ll Ans, fac[MAXN], sum[MAXN];
ll n, m;
char str[MAXN]; ll pow(ll a, ll b){
if (b == ) return 1ll;
if (b == ) return (a % MOD);
ll tmp = pow(a, b / );
if (b % == ) return (tmp * tmp) % MOD;
else return (((tmp * tmp) % MOD) * (a % MOD)) % MOD;
}
//计算组合数
ll C(ll n, ll m){
if(m > n || m < ) return ;
return fac[n] * pow((fac[n - m] * fac[m]) % MOD, MOD - ) % MOD;//逆元
}
void init(){
read(n);
read(m);
fac[] = ;
for (int i = ; i <= ; i++) fac[i] = (fac[i - ] * i) % MOD;
ll k = ;
for (int i = ; i <= n; i++){
sum[i] = (sum[i - ] + k * C(n - i - , m - ) % MOD) % MOD;
k = (k * ) % MOD;
}
}
void work(){
ll k = ;
scanf("%s", str + );
Ans = ;
for (int i = n; i > ; i--){
int t = str[i] - '';
Ans = (Ans + ((t * k) % MOD) * C(i - , m) % MOD) % MOD;
Ans = (Ans + (t * sum[n - i]) % MOD) % MOD;
k = (k * ) % MOD;
}
printf("%lld\n", Ans);
} int main(){
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
work();
return ;
}