NWAYS - Editorial








Hockey Stick Identity


Find the number of ways to move from all points in set X = \{(0,i): 1 \leq i \leq n \} to all point in set Y = \{(k,i): 1 \leq i \leq n \} using minimum number of steps.


We are given two sets of points X and Y (described above) in 2-D grid, and we have to find the number of ways to move from all points in X to all points in Y using minimum number of steps. Note that only vertical and horizontal travelling is allowed and since we have to move from one point to another in minimum number of steps, we cannot visit a point twice.

The number of shortest paths from (0,0) to (m,n) in 2D grid is m+n \choose n. Using this result, in this problem, we essentially have to find the following term in which the first summation loops over the points in set X and second summation loops over the points in set Y.

S = \sum_{i=1}^n \sum_{j=1}^n {|j-i|+k \choose k}

Now the absolute value of j-i is a bug and we have to some how get rid of that absolute sign. The easiest way to do so is to consider the case when j \geq i and j < i . So, for any point i in set X, divide the points in set Y into 3 parts - lower y-points , upper y-points and y-point which is at the same level. Note that the upper and lower point cases are symmetrical. Hence, S can be written as follows

S = 2\sum_{i=1}^n \sum_{j=i}^n {j-i+k \choose k} - n

Now the only thing which is left, is to calculate \sum_{i=1}^n \sum_{j=i}^n {j-i+k \choose k}.

\begin{align*} \sum_{i=1}^n \sum_{j=i}^n {j-i+k \choose k} &= \sum_{i=1}^n \sum_{j=k}^{n-i+k} \binom{j}{k} \\ &= \sum_{i=1}^n \binom{n-i+k+1}{k+1} \qquad ext{ Hockey Stick Identity } \\ &= \sum_{i=k+1}^{n+k} \binom{i}{k+1} \\ &= \binom{n+k+1}{k+2} \qquad \qquad \quad \ ext{ Hockey Stick Identity } \end{align*}

The binomial terms can be calculated in \mathcal{O}(1) time if we have precomputed the factorial and inverse factorial. Hence, answer for every test case can be computed in \mathcal{O}(1) time.




could someone tell me where im going wrong.
I used dynamic programming using paths*[j] where i=(difference in x coordinates) j=(diff in y) and paths is the number of paths with (diff in x=i) and (diff in y=j)


@siehpe your code gives WA for case 1000 1000 as while calculating the matrix you are not taking modulus which may cause overflow and especially the sum calculation part, take modulo there also and it will be fine


@apptica thanks that worked!


What is wrong with my solution @saurabh060792 ? http://www.codechef.com/viewsolution/7055277


If I had wrote it in the form of double summation… Life would have been easier


how is this no (2000006) achieved in many solutions to solve ?
j-i can be @ max 10^6-1 and k can be max 10^6 so that makes 2*10^6-1 ?



This question may be good for other contests like Long Challenge or Cook-offs but they certainly aren’t supposed to be given for Lunchtimes . I have explained this in this post.


please check my code
what wrong with this??


@deepak100 here is AC of your code http://www.codechef.com/viewsolution/7070722. Your statement sum = (2*sum%m - n)%m; has problem because modular operations of (a%m-b)%m is not valid always as a%m < b also basically what you have to do is add m to it and calculate modulo


what is the formula to get 2nd last line from 3rd last line in your explaination??
∑i=1n(n−i+k+1k+1) Hockey Stick Identity


thank u @apptica
i was confused with expression
sum=(2sum%m - n+m)%m; and sum=(2sum%m - n)%m; because if we add any no. m to a no. n and take mod with m then it must resultant 0 …ie. (n+m)%m==n


I think he has used the identity C(n+r,r) for range of r . I read this from https://proofwiki.org/wiki/Sum_of_r%2Bk_Choose_k_up_to_n


is there a need to evaluate inverse factorial

can’t we evaluate nCr(n, r) as (fact[n]/(fact[r]*fact[n-r])) % mod ???


@mohitbindal644 you cant do this because to store factorial of large numbers without mod will overflow the size of the defined variable whether it is even long double. Now in fact[] array you will store fact* as mod so you need to use inverse modulo


I do not get how did you changed the mod to that formula with -n at the end… Can someone stepwise and split and derive please?


Hi i still couldn’t understand how is it ∑i=1 n(n−i+k+1 k+1) = ∑i=k+1 n+k(i k+1)
It will be good help if someone breaks down them into sub steps?
I went through link posted by amitkpandeykgp


links to solutions are not working!


generally people just approximate the max usage of memory. I think this must be the reason.I used 2000005


Exactly. !