Friday, January 11, 2013

Arithmetic Progression

Just solved the hard problem Arithmetic Progression at interviewstreet, and i found it interesting so i decided to share its idea with you.

Let F(a,d) denote an arithmetic progression (AP) with first term 'a' and common difference 'd'. i.e  F(a,d) denotes an infinite AP => a, a+d, a+2d, a+3d,... You are given 'n' APs => F(a1,d1), F(a2,d2), F(a3,d3),... F(an,dn). Let G(a1,a2,... an,d1,d2,... dn) denote the sequence obtained by multiplying the 'n' APs.

Multiplication of 2 sequences is defined as follows: Let the terms of the first sequence be A1, A2, Am, and terms of the second sequence be B1, B2,... Bm. The sequence obtained by multiplying these two sequences is: A1*B1, A2*B2,... Am*Bm. We also give the definition for the kth difference of a sequence. If A1, A2,... Am,... be the terms of a sequence, then the terms of the 1st difference of this sequence are given by A2-A1, A3-A2,... Am-Am-1,... In general, if A1, A2,... Am,... be the terms of the kth difference of a sequence, then the terms of the k+1th difference are given by A2-A1, A3-A2,... Am-Am-1,... We say that the kth difference of a sequence is a constant, if all the terms of the kth difference are equal.

Let F'(a,d,p) be a sequence defined as => ap, (a+d)p, (a+2d)p,... Similarly, G'(a1,a2,... an,d1,d2,... dn,p1,p2,... pn) is defined as => product of F'(a1,d1,p1), F'(a2,d2,p2),... F'(an,dn,pn). Now, your task is to find the smallest 'k' for which the kth difference of the sequence G' is a constant. You are also required to find this constant value.

You will be given many operations. Each operation is of one of the 2 forms:
1.) 0 i j => 0 indicates a query (1<=i<=j<=n).You are required to find the smallest 'k' for which the  kth difference of G'(ai,ai+1,... aj,di,di+1,... dj,pi,pi+1,... pj) is a constant. You should also output this constant value.
2.) 1 i j v => 1 indicates an update (1<=i<=j<=n). For all i<=k<=j, we update pk = pk+v.


The first line of input contains a single integer 'n', denoting the number of APs. Each of the next 'n' lines consists of 3 integers ai, di, pi (1<=i<=n). The next line consists of a single integer 'q', denoting the number of operations. Each of the next 'q' lines consist of one of the two operations mentioned above.


For each query, output a single line containing 2 space separated integers 'K' and 'V'. 'K' is the least value for which the Kth difference of the required sequence is a constant. 'V' is the value of this constant. Since 'V' might be large, output the value of 'V' modulo 1000003.

Note: 'K' will always be such that it fits into a signed 64-bit integer. All indices for query and update are 1-based. Do not take modulo 1000003 for 'K'.


1<=ai, di, pi<=10000
For updates of the form "1 i j v", 1 <= v <= 10000


Unfortunately i tried to find anything related to number theory to find an equation to help solving this problem, but i couldn't find anything useful, actually that's why i managed to share it with you. I did some calculations by my own and i got some interesting equations to solve this problem, and here we go:

To get the K and V for an AP F(A, D, P):

K = P, V = D^P*P!

To get the K and V for F(A1, D1, P1)*F(A2, D2, P2)*F(A3, D3, P3):

I found that K will always equal to: P1+P2+P3.

And with the help of my friend Neal Zane, he helped me to find that V = (D1^P1*D2^P2*D3^P3)*(P1+P2+P3)!.

Now by knowing all the equations needed to solve this problem, we will use segment trees to solve this problem.


  1. How can you store the product and update them by interval tree?
    Assuming you have P_1, P_2, P_3
    then you update 1 1 3 2.
    How you update the product by the lazy manner?
    Maintain the sum is easy.

    And how to calculate the remainder under the factorial?

    1. o, it's sum too, not product.You got a typo in the formula of V.

  2. When building the tree:

    For leaf nodes: v = d^p
    For parent nodes: v = left.v*right.v

    when updating p for some node, this will affect all its childs.. and will make its
    v *= power(mul_d, value)%MOD ..
    here mul_d = d1*d2*d3....*dn where nodes 1, 2, 3, ... n are the leaf childs for the current node.

    updating lazy[node*2] = lazy[node*2+1] = value

    1. Yes. I figured out it by myself. Thanks all the way.

  3. Input:
    1 2 1
    5 3 1
    0 1 2
    1 1 1 1
    0 1 1
    2 12
    2 8
    Please explain the output here?
    If you have any typos in the formula for V, please correct because I am not able to get correct answer using the given formula.

    1. may be you have something wrong in your code or you there is a bug in your segment trees implementation.. try to do more debugging its a hard problem :)

  4. No. I haven't yet implemented and I know how to do that. I am just asking you to show how the desired output in the above example came, using the formula for K and V. In addition, you can also help me by writing the formula for K and V including proper brackets for the sake of no confusion. :)

    1. Eagerly waiting for your reply :)

    2. I think yes, there is a typo error and i fixed it, in the equation of solving V :)..

  5. Just one more query I am stuck with. How did you manage to get the "(p1+p2+p3)!" ? Since the value of sum "p1+p2+p3" can take any value of a signed 64-bit integer. So, how did you manage to get factorial of such big numbers?