【CF521C】【排列组合】Pluses everywhere
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!
The first line contains two integers, n and k (0 ≤ k < n ≤ 105).
The second line contains a string consisting of n digits.
Print the answer to the problem modulo 109 + 7.
1
2
|
3 1
108
|
1
|
27
|
1
2
|
3 2
108
|
1
|
9
|
In the first sample the result equals (1 + 08) + (10 + 8) = 27.
In the second sample the result equals 1 + 0 + 8 = 9.
【分析】
排列组合计算每一位对答案的贡献。
1 /* 2 宋代朱敦儒 3 《西江月·世事短如春梦》 4 世事短如春梦,人情薄似秋云。不须计较苦劳心。万事原来有命。 5 幸遇三杯酒好,况逢一朵花新。片时欢笑且相亲。明日阴晴未定。 6 */ 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10 #include <cmath> 11 #include <queue> 12 #include <vector> 13 #include <iostream> 14 #include <string> 15 #include <ctime> 16 #define LOCAL 17 const int MAXN = 100000 + 10; 18 const long long MOD = 1000000007; 19 const double Pi = acos(-1.0); 20 long long G = 15;//原根 21 const int MAXM = 60 * 2 + 10; 22 using namespace std; 23 typedef long long ll; 24 void read(ll &x){//读入优化 25 char ch;x = 0; 26 ll flag = 1; 27 ch = getchar(); 28 while (ch < '0' || ch > '9') {if (ch == '0') flag = -1; ch = getchar();} 29 while (ch >= '0' && ch <= '9') {x = x * 10 + (ch - '0'); ch = getchar();} 30 x *= flag; 31 } 32 //分别为阶乘和前缀和 33 ll Ans, fac[MAXN], sum[MAXN]; 34 ll n, m; 35 char str[MAXN]; 36 37 ll pow(ll a, ll b){ 38 if (b == 0) return 1ll; 39 if (b == 1) return (a % MOD); 40 ll tmp = pow(a, b / 2); 41 if (b % 2 == 0) return (tmp * tmp) % MOD; 42 else return (((tmp * tmp) % MOD) * (a % MOD)) % MOD; 43 } 44 //计算组合数 45 ll C(ll n, ll m){ 46 if(m > n || m < 0) return 0; 47 return fac[n] * pow((fac[n - m] * fac[m]) % MOD, MOD - 2) % MOD;//逆元 48 } 49 void init(){ 50 read(n); 51 read(m); 52 fac[0] = 1; 53 for (int i = 1; i <= 100000; i++) fac[i] = (fac[i - 1] * i) % MOD; 54 ll k = 1; 55 for (int i = 1; i <= n; i++){ 56 sum[i] = (sum[i - 1] + k * C(n - i - 1, m - 1) % MOD) % MOD; 57 k = (k * 10) % MOD; 58 } 59 } 60 void work(){ 61 ll k = 1; 62 scanf("%s", str + 1); 63 Ans = 0; 64 for (int i = n; i > 0; i--){ 65 int t = str[i] - '0'; 66 Ans = (Ans + ((t * k) % MOD) * C(i - 1, m) % MOD) % MOD; 67 Ans = (Ans + (t * sum[n - i]) % MOD) % MOD; 68 k = (k * 10) % MOD; 69 } 70 printf("%lld\n", Ans); 71 } 72 73 int main(){ 74 #ifdef LOCAL 75 freopen("data.txt", "r", stdin); 76 freopen("out.txt", "w", stdout); 77 #endif 78 init(); 79 work(); 80 return 0; 81 }