CodeForces-520E Pluses everywhere

Title Description

A given length of \ (n-\) string, is given a non-negative integer \ (K \) , requires adding to the middle of the string \ (K \) th $ \ (+ \) 'number, into a table

Expressions such as " \ (1000101 \) ", add two \ (+ \) numbers, may become " \ (10 + 001 + 01 \) " or " \ (1 + 01 1000 + \) ", value of the expression are \ (12 \) and \ (1002 \) .

Expression program asked all the added plus of value and how much.

Input

Two integers \ (n-, K \) , a string S $ $ \ ((0 <K = <n-<= 1E5). \)

Output

An integer mold $ 1,000,000,007 $

Sample Input

3 1
108

3 2
108

Sample Output

27
9

Count \ (the DP \)

First of all, to see so much of the data range is certainly calculate the contribution for each number.

So how do we calculate the contribution it?

For the given topic string \ (S \) , each digit sequentially from left to right as \ (a_ {n-1} , a_ {n-2} .... a_ {1}, a_0 \) .

Now, we have to \ (a_t \) , to discuss its contribution.

\ (a_t \) right is \ (T \) positions plus can be placed, while a total of \ (n-1 \) position a plus sign.

If \ (a_t \) right of the first position plus placed, the \ (a_t \) is as bits, and \ (n-2 \) vacancies, \ (. 1-K \) plus sign, a total of \ (C (k-1, n-2) \) kinds of programs.

Which contribute to the \ (C (. 1-K,. 1-n-) * 10 ^ {} * a_t 0 \) .

If \ (a_t \) a first empty the right position, the second position a plus sign, then \ (a_t \) is ten, and \ (n-3 \) vacancies, \ (. 1-K \ ) plus sign, a total of \ (C (k-1, n-3) \) kinds of programs.

Contributions \ (C (. 1-K, 2-n-) * 10 ^ {} * a_t. 1 \) .

And so on,

If \ (a_t \) to the right is empty, then the contribution to the \ (C (K, NT-2) {T} * 10 * a_t ^ \) .

Therefore, \ (a_t \) contribution to this number is \ ((10 ^ {t} * C (k, nt-2) + \ sum ^ {t} _ {j = 0} 10 ^ {j} * C ( . 1-K, 2-NJ)) a_t * \) .

So we can get the first \ (I \) contribution calculated number \ (A_i = 10 ^ {i } * C (k, ni-2) + \ sum ^ {i} _ {j = 0} 10 ^ { * C} J (. 1-K, 2-NJ) \) .

Finally, \ (Ans = \ ^ {n-SUM-I. 1 = 0}} _ {a_i * A_i \) .

code show as below

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define int long long
#define reg register
#define Raed Read
#define clr(a,b) memset(a,b,sizeof a)
#define Mod(x) (x>=mod)&&(x-=mod)
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a))
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(int i=(G).Head[x]; i; i=(G).Nxt[i])
#pragma GCC target("avx,avx2,sse4.2")
#pragma GCC optimize(3)

inline int Read(void) {
    int res=0,f=1;
    char c;
    while(c=getchar(),c<48||c>57)if(c=='-')f=0;
    do res=(res<<3)+(res<<1)+(c^48);
    while(c=getchar(),c>=48&&c<=57);
    return f?res:-res;
}

template<class T>inline bool Min(T &a, T const&b) {
    return a>b?a=b,1:0;
}
template<class T>inline bool Max(T &a, T const&b) {
    return a<b?a=b,1:0;
}


const int N=1e5+5,M=1e6+5,mod=1e9+7;

bool MOP1;

int Fac[N],Inv[N];

inline int Pow(int x) {
    int res=1,y=mod-2;
    while(y) {
        if(y&1)res=(res*x)%mod;
        x=(x*x)%mod,y>>=1;
    }
    return res;
}

inline int C(int x,int y) {
    if(!x)return 1;
    return (Fac[y]*((Inv[x]*Inv[y-x])%mod))%mod;
}

int A[N],Pow_10[N];

char S[N];

bool MOP2;

inline void _main() {
    int n=Read(),k=Read(),Ans=0,tot=0;
    scanf("%s",S),Fac[0]=Pow_10[0]=Inv[0]=1;
    if(!k) {
        int Ans=0;
        ret(i,0,n)Ans=(Ans*10+(S[i]^48))%mod;
        printf("%lld\n",Ans);
        return;
    }
    rep(i,1,n) {
        Fac[i]=(Fac[i-1]*i)%mod;
        Pow_10[i]=(Pow_10[i-1]*10)%mod;
        Inv[i]=Pow(Fac[i]);
    }
    A[0]=C(k-1,n-2);
    int P=n-k-1;
    rep(i,0,P)A[i]=(A[i-1]+Pow_10[i]*C(k-1,n-i-2))%mod;
    rep(i,P+1,n)A[i]=A[i-1];
    rep(i,0,P)A[i]=(A[i]+Pow_10[i]*C(k,n-i-2));
    ret(i,0,n)Ans=(Ans+(S[i]^48)*A[n-i-1])%mod;
    printf("%lld\n",Ans);
}

signed main() {
    _main();
    return 0;
}

Guess you like

Origin www.cnblogs.com/dsjkafdsaf/p/11402180.html