多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > Codeforces 520E. Pluses everywhere 數學

Codeforces 520E. Pluses everywhere 數學

來源:程序員人生   發布時間:2015-08-24 08:31:47 閱讀次數:4316次


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時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; }




生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 真实男女xx00动态视频120秒 | 欧美v日韩v亚洲v最新 | free日本xxxx另类hd | 91久久打屁股调教网站 | 亚洲精品乱码中文字幕无线 | 国内一级一级毛片a免费 | www.黄色一片 | 琪琪在线观看 | 欧美free嫩交video | 中国国产成人精品久久 | 亚洲国产欧美国产第一区二区三区 | 老司机成人午夜精品福利视频 | 亚洲欧美日韩中文字幕久久 | 国产激情一区二区三区在线观看 | 久久精品视频观看 | 老司机免费福利视频 | 亚洲国产专区 | 久久se精品一区二区影院 | 伊人久久精品亚洲午夜 | 欧美国产精品不卡在线观看 | 性欧美欧美| 欧美成人精品一区二三区在线观看 | 欧美性xxxxx极品 | 久久爱老牛影视一区二区 | 亚洲最大色视频 | 中文字幕视频二区 | 国产精品福利在线观看免费不卡 | 午夜小视频网站 | 欧美日本综合一区二区三区 | 18网站在线观看 | 欧美一区永久视频免费观看 | 12306午夜被窝播播影院yw188 | 国内精品久久久久影院亚洲 | 日本免费乱人伦在线观看 | 中文字幕在线观看一区 | 亚洲精品天堂在线 | 一区二区在线视频 | 亚洲欧美日韩另类 | 一区二区三区精品 | xxxxx国产老太 | 国产国语一级毛片在线放 |