### Skeef79's blog

By Skeef79, history, 5 weeks ago, translation,

Hello codeforces!

I am wondering is there a way to caculate prefix sums $k$ times modulo $p$ fast, especially when we can't use matrix multiplication?

By calculating prefix sums $k$ times I mean this piece of code:

for(int it = 0; it < k; it++) {
for(int i = 1; i < n; i++)
pref[i]+= pref[i-1];
}


So if we start with array: $1, 0, 0, 0$ we will get:

1. $1, 1, 1, 1$;

2. $1, 2, 3, 4$;

3. $1, 3, 6, 10$;

4. $1, 4, 10, 20$; ...

• +40

 » 5 weeks ago, # |   +17 you have written your blog in russian so the users with the english interface don't see it
•  » » 5 weeks ago, # ^ |   0 Thank you for mentioning
 » 5 weeks ago, # |   0 Auto comment: topic has been translated by Skeef79 (original revision, translated revision, compare)
 » 5 weeks ago, # |   0 Fast meaning you want O(n) time independent of the value of k? or what exactly
•  » » 5 weeks ago, # ^ |   +1 Yes, $k$ can be upto $10^{18}$, but modulo I think should be around $10^5$
 » 5 weeks ago, # |   +110 If $b$ is the array after these operations then $b_i = \sum\binom{k+j}{k}a_{i-j}$, which is the $i$-th coefficient of some polynomial product, so this works in $O(n\log{n})$.
•  » » 5 weeks ago, # ^ |   +22 How did you get this formula? Is there any article?
•  » » » 5 weeks ago, # ^ |   +77 Rotate the second half of your post 45 degrees clockwise and it is Pascal's triangle.
•  » » » » 5 weeks ago, # ^ |   +17 Really...Thank you!
•  » » » 5 weeks ago, # ^ |   +16 Here is some informal handwaving. Each occurrence of $a_{i-j}$ into $b_i$ is basically a sequence $(d_1, \ldots, d_k)$ of nonnegative "steps" with sum being equal to $j$. It has the following meaning: when we expand the last a[i] := sum(a[0..i]), we look specifically at $a_{i-d_1}$ from the previous step. During the previous step, this value was assigned the sum of its prefix, but we look only at $a_{i-d_1-d_2}$, and so on. In the end we will look at $a_{i-j}$, and this is how it contributed into the final value along this path: through positions $(i-j)\to (i-j+d_k)\to\ldots \to i$.On the other hand, the number of such sequences can be computed using stars and bars technique and equals that binomial coefficient.
 » 5 weeks ago, # | ← Rev. 2 →   +21 we can get k-times prefix sum of an array by a transformation reference. $$f_{k} = (\sum_{i=0}^{n-1} a_{i}*x^i)(1/(1-x)^k)$$
 » 5 weeks ago, # | ← Rev. 2 →   +11 Btw similar problem exists on project euler 739Golovanov399 was 4th person to solve it. Its soln is similar to what Golovanov399 mentioned above. If you AC it you would be able to access its discussion forum were people have mentioned how did they solved that problem.
 » 5 weeks ago, # |   +24 Here's the same problem on Codeforces: 223C. Partial Sums