## 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.

Problem

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.

Input:

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.

Output:

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'.

Constraints:

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

Solution

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.

## Thursday, January 10, 2013

### View Angle Problem

Yesterday i was participating in a contest and faced a nice geometry problem named View Angle. After the contest i managed to solve this problem and here are the problem statement and my solution.

Flatland has recently introduced a new type of an eye check for the driver's licence. The check goes like that: there is a plane with mannequins standing on it. You should tell the value of the minimum angle with the vertex at the origin of coordinates and with all mannequins standing inside or on the boarder of this angle. As you spend lots of time "glued to the screen", your vision is impaired. So you have to write a program that will pass the check for you.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of mannequins. Next n lines contain two space-separated integers each: xi, yi (|xi|, |yi| ≤ 1000) — the coordinates of the i-th mannequin. It is guaranteed that the origin of the coordinates has no mannequin. It is guaranteed that no two mannequins are located in the same point on the plane.
Output
Print a single real number — the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10 - 6.

Sample test(s)
Input
```2
2 0
0 2```
Output
`90.0000000000`
Input
```3
2 0
0 2
-2 2```
Output
`135.0000000000`
Input
```4
2 0
0 2
-2 0
0 -2```
Output
`270.0000000000`
Input
```2
2 1
1 2```
Output
`36.8698976458`

Solution for first test case

Solution for third test case

Solution for forth test case

Solution

In order to solve this problem its obvious we need to get the angles between every two adjacent points and getting the max angle say A, and the answer will be 360-A "The angle that will include all other points".

C++ Solution