日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > php教程 > Codeforces 520E. Pluses everywhere 數(shù)學(xué)

Codeforces 520E. Pluses everywhere 數(shù)學(xué)

來源:程序員人生   發(fā)布時(shí)間:2015-08-24 08:31:47 閱讀次數(shù):4322次


520E - Pluses everywhere/521C - Pluses everywhere

Idea: Endagorion

Preparation: gchebanov, DPR-pavlin

Consider some way of placing all the pluses, and a single digit di (digits in the string are numbered starting from 0 from left to right). This digit gives input of di?10l to the total sum, where l is the distance to the nearest plus from the right, or to the end of string if there are no pluses there. If we sum up these quantities for all digits and all ways of placing the pluses, we will obtain the answer.

For a given digit di and some fixed l, how many ways are there to place the pluses? First of all, consider the case when the part containing the digit di is not last, that is, i?+?l?<?n?-?1. There are n?-?1 gaps to place pluses in total; the constraint about di and the distance l means that after digits di...di?+?l?-?1 there are no pluses, while after the digit di?+?l there should be a plus. That is, the string should look as follows:

Here a dot means a gap without a plus, and a question mark means that it's not important whether there is a plus or not. So, out of n?-?1possible gaps there are l?+?1 gaps which states are defined, and there is one plus used in these gaps. That means that the other (n?-?1)?-?(l?+?1)?=?n?-?l?-?2 gaps may contain k?-?1 pluses in any possible way; that is, the number of such placements is . A similar reasoning implies that if the digit di is in the last part, that is, i?+?l?=?n?-?1, the number of placements is .

To sum up, the total answer is equal to

Let us transform the sum:

To compute these sums, we will need to know all powers of 10 up to n-th (modulo 109?+?7), along with the binomial coefficients. To compute the binomials, recall that , so it is enough to know all the numbers k! for k upto n, along with their modular inverses. Also we should use the prefix sums of di, that is, the array . The rest is simple evaluation of the above sums.

The total complexity is , because the common algorithms for modular inverses (that is, Ferma's little theorem exponentiation or solving a diophantine equation using the Euclid's algorithm) have theoritcal worst-case complexity of . However, one can utilize a neat trick for finding modular inverses for first n consecutive numbers in linear time for a total complexity of O(n); for the description of the method refer to this comment by Kaban⑸ (not sure why it has a negative rating, I found this quite insightful; maybe anyone can give a proper source for this method?).

Challenge: now we want to find the sum of all expressions that are made by placing k pluses with a?≤?k?≤?b; that is, we want to find the sum of the answers for the original problem with k?=?a,?...,?b; here a and b can be any integers with 0?≤?a?≤?b?≤?n?-?1. There is an obviousO(n2) solution: just find the answers for all k separately. Can you find a linear solution?




E. Pluses everywhere
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers 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
3 1 108
output
27
input
3 2 108
output
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.


/* *********************************************** Author :CKboss Created Time :2015年03月04日 星期3 19時(shí)46分18秒 File Name :E.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; typedef long long int LL; const LL mod = 1e9+7LL; const int maxn=100100; int n,k; LL a[maxn]; char str[maxn]; LL inv[maxn],presum[maxn]; LL jc[maxn],jcv[maxn],e[maxn]; void init() { /// inv jc e inv[1]=1; jc[0]=1; jcv[0]=1; jc[1]=1; jcv[1]=1; e[0]=1; e[1]=10; for(int i=2;i<maxn;i++) { inv[i]=inv[mod%i]*(mod-mod/i)%mod; jc[i]=(jc[i⑴]*i)%mod; jcv[i]=(jcv[i⑴]*inv[i])%mod; e[i]=(e[i⑴]*10LL)%mod; } } LL COMB(LL n,LL m) { if(m<0||m>n) return 0LL; if(m==0||m==n) return 1LL; /// n!/((n-m)!*m!) LL ret=((jc[n]*jcv[n-m])%mod*jcv[m])%mod; return ret; } void solve(int n,int k) { LL ans=0; if(k==0) { for(int i=n⑴;i>=0;i--) ans=(ans+(e[n⑴-i]*a[i])%mod)%mod; cout<<ans%mod<<endl; return ; } for(int l=0;l<=n⑵;l++) ans=(ans+(e[l]*COMB(n-l⑵,k⑴)%mod*presum[n-l⑵])%mod)%mod; for(int i=0;i<=n⑴;i++) ans=(ans+((a[i]*e[n⑴-i])%mod*COMB(i,k))%mod)%mod; cout<<ans%mod<<endl; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); init(); scanf("%d%d",&n,&k); scanf("%s",str); for(int i=0;i<n;i++) { a[i]=str[i]-'0'; if(i==0) presum[i]=a[i]; else presum[i]=(presum[i⑴]+a[i])%mod; } solve(n,k); return 0; }




生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 自拍偷拍亚洲精品 | 日本一区二区不卡 | 久久久久久久久久久久久久久久久久久久 | 老熟妇午夜毛片一区二区三区 | 日韩在线视频在线 | 欧美日韩中文字幕在线 | 久久99精品久久久久久琪琪 | 国产精品视频一区二区三区, | 久久有精品| 国产在线视频网 | 成人av免费 | 国产视频二区三区 | 五月婷婷综合激情 | 国产午夜激情视频 | 亚洲小视频 | 在线v| 男人懂的网站 | 91射区| www一区| 可以在线观看的av | 精品一区二区电影 | 日韩成人一区二区 | 国产成人av一区二区三区 | 亚洲成av人片一区二区 | 伊人网伊人网 | 国产高清免费 | 日韩一区二区三区视频 | 国产一二三区免费视频 | 精品视频久久久久久久 | 免费在线一区二区 | 青草一区二区 | 欧美国产综合视频 | 国产精品99久久 | 国产成人精品免费视频大全最热 | 久久久久久久久国产精品 | 美女二区 | av噜噜噜| 另类 欧美 日韩 国产 在线 | 成年人视频免费在线观看 | 91大片| 国产成人一区二区 |