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

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

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

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

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define LL long long
 5 
 6 using namespace std;
 7 
 8 const int MOD = 1e9+7;
 9 const int maxn = 3e5+10;
10 
11 LL f[maxn],inv[maxn],fac[maxn];
12 
13 void init()
14 {
15     fac[0] = fac[1] = inv[0] = inv[1] = f[0] = f[1] = 1;
16     for(int i=2;i<maxn;i++)
17     {
18         fac[i] = fac[i-1]*i%MOD;
19         LL t = MOD/i,k = MOD%i;
20         f[i] = (MOD-t)*f[k]%MOD;
21         inv[i] = inv[i-1]*f[i]%MOD;
22     }
23 }
24 
25 LL C(LL n,LL m)
26 {
27     if(n < m || m < 0) return 0;
28     return fac[n]*inv[m]%MOD*inv[n-m]%MOD;
29 }
30 
31 char s[maxn];
32 int N,K;
33 LL save[maxn];
34 
35 int main()
36 {
37     init();
38     while(~scanf("%d%d",&N,&K))
39     {
40         scanf("%s",s);
41         LL base = 1;
42         for(int i=1;i<=N;i++)
43         {
44             save[i] = (save[i-1]+C(N-1-i,K-1)*base%MOD)%MOD;
45             base = base*10%MOD;
46         }
47         LL ans = 0;base = 1;
48         for(int i=N-1;i>=0;i--)
49         {
50             ans += (s[i]-'0')*(save[N-1-i]+C(i,K)*base%MOD)%MOD;
51             ans %= MOD;
52             base = base*10%MOD;
53         }
54         printf("%I64d\n",ans);
55     }
56 }

 

posted @ 2016-02-21 16:55  Helica  阅读(287)  评论(0编辑  收藏  举报