Hihocoder 1430:a boring problem (a complex problem)

Source: Internet
Author: User

Hihocoder #1430: A boring problem (a complex topic) time limit: 1000ms single point time limit: 1000ms memory Limit: 256MBDescription-topic description

as a student of the School of Electronics Engineering and computer science in Peking Un Iversity, Kyle took the course named Advanced Algebra in his freshman year, which, Unluckily, became he nightmare.
His teacher, Mr. X, have an approximately paranoid requirements in the ability of calculation, from which his student s suffer a lot.
One day, Mr. X got a whim so he wanted to know for a given an integer k and a long numeric string S whose length is N , what's the result of   for each i (1≤i≤n), where. S[l] means the Lth digit in S, and L starts from 1.
Mr. X added the problem to the midterm test. Give a hand to Kyle and tell him the answer mod 1000000007.

as a student at PKU College of Information Science and technology, Kyle had no idea what a nightmare it was until he got a class called Advanced algebra in his freshman year. His teacher, Mr X, has a frenzied demand for computational power. The students suffered from the damage. One day, Mr x whim, an integer k with a number string of length N, what is the result for each I (1 ≤i≤n) Ōi j=1f (J, i)? Here F (j, I) = (Ōi l=j s[l]) ^K. S[L] Indicates the number of S, L, starting from 1. Mr x Put the problem in the mid-term exam. Help Kyle and tell him the answer after model 1000000007. 
CN

Input-Enter

There is multiple test cases.
The first line of the input contains an integer T which means the number of the test cases.
The first line of each test case contains the integers, above mentioned N and K.
The next line is the above mentioned string S. s consists of only digits (' 0-' 9 ').

multiple sets of test cases. The first behavior of the input is an integer t that represents the number of test cases. The first line of each set of test cases has two integers, that is, N and K above. The next behavior is a number string s. s contains only numbers ('9'). 
CN

Output-Outputs

For each test case, print a single line representing the result of the For each I (1≤i≤n).

For each test case, the output line number represents the result of each I (1 ≤i≤n) in the Ōi j=1f (J, i).
CN

Sample input-Sample inputs

25 1123455 154321

Sample output-sample outputs

1 5 14 30 555 13 22 30 35

NOTE-Notice

T≤5
n≤50,000, k≤100

Exercises

  In order to prevent confusion with the following instructions, the string S[L] using ai instead

first, according to the meaning of the topic to make a few, grow a bit like the backpack.

If you do this at this time, the complexity of the time is O (n*n*log2k), will explode.

If you use look, the middle of the sub-string is not good to say ... And there seems to be no way to simplify (anyway, this slag doesn't come out).

Then add no, use subtraction?

before use n and (prefix and) can be used to represent each substring in subtraction.

here Si represents the first and the previous I, and S0 = 0.

The transformation is as follows:

Then rewrite each item into two-item expansion:

Merge two-item expansion

with SSi represents the sum of the first i S and can be obtained through:

at this point, the complexity of Time falls O (NK) .

Pay attention to the modulo and the final output, then there is no problem.

Code C + +

1#include <cstdio>2 #defineMoD 10000000073 #defineMX 500054 #definell Long Long5ll c[ the][ the], s[mx][ the], ss[mx][ the];6 CharRD[MX];7 intMain () {8     intT, N, K, I, J;9 ll opt, tmp;Ten      for(i =0; I <= -; ++i) { Onec[i][0] =1; A          for(j =1; J <= I; ++J) C[i][j] = (C[i-1][j-1] + c[i-1][J])%MoD; -     } -      for(i =0; i < MX; ++i) s[i][0] = ss[i][0] =1; the  -      for(SCANF ("%d", &t); T --t) { -scanf"%d%d", &n, &k); Gets (RD); -          for(i =0; I < n; ++i) S[i +1][1] = Rd[i]-'0'; +          for(i =1; I <= N; ++i) { -s[i][1] = (s[i][1] + s[i-1][1]) %MoD; +              for(j =2; J <= K; ++J) S[i][j] = (S[i][j-1] * s[i][1]) %MoD; A         } at          for(i =1; I <= N; ++i) { -              for(j =0; J <= K; ++J) Ss[i][j] = (S[i][j] + ss[i-1][J])%MoD; -         } -  -          for(i =1; I <= N; ++i) { -opt =0; in              for(j =0; J <= K; ++j) { -TMP = (c[k][j] * s[i][j])%MoD; to                 if((K-J) &1) opt-= (Tmp*ss[i-1][K-J])%MoD; +                 Elseopt + = (tmp*ss[i-1][K-J])%MoD; -Opt%=MoD; the             } *printf"%lld%c", (opt + MoD)% mod,"\ n"[i = =n]); $         }Panax Notoginseng     } -     return 0; the}

Hihocoder 1430:a boring problem (a complex problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.