Friday, September 13, 2013

Red and Blue balls puzzle

Today while reading some posts on Facebook i found this puzzle on the ACM ICPC FB page, and i find it very easy because i know about impartial games which you can read about it on my blog here.

Here is the problem:


Here is my solution: each time we can pick one of the following combinations:

1. Red/Red
2. Red/Blue
3. Blue/Red
4. Blue/Blue

Last two cases will leave the box with same state, so we can use the following:

Lets define a state called solve(r, b, out) which defines the state of having r Red balls, B blue balls inside the box and out Red balls outside the box.

We can move from one state to another state by doing one of the moves described above.

What about base cases?

Here are the 2 base cases we have:

1. If we have 2 balls of the same color, we can move them and put a red ball in the box "Then the answer will be red"
2. If we have 2 balls of different colors, we can move them and return back the blue ball in the box "Then the answer will be blue"

Also we can use dynamic programming memoization to optimize our solution.

Here is my code for this problem. Enjoy it ;)


Thursday, March 7, 2013

Configuring Apache HTTP Server along with Glassfish Application Server

This month i was given tasks in a new project that is using Oracle Glassfish as an application server, and after finishing the development phase, my team leader assigned to me a task to install the testing server environment.

One of the sub-tasks of the installation is to configure Apache HTTP Server along with Glassfish.. And here are what i have done to accomplish my task..

1. Install Apache HTTP Server, and Mod_JK
2. Configure workers.properties under APACHE_HOME/conf/

3. Enable mod_jk in httpd.conf "its commented by default"
4. Configure apache httpd.conf located under APACHE_HOME/conf/httpd.conf

5. Restart Apache HTTP Server "./apachectl restart"
6. Create Glassfish listener to recvieve requests

asadmin --user admin --host localhost --port 4848 set configs.config.server-config.network-config.network-listeners.network-listener.jk-connector-8009.jk-enabled=true

7. Restart Glassfish

FYI, There is another way but very slow compared to this way, using mod_proxy and configuring a virtual host to redirect to the application server.

Now enjoy redirecting all your requests to the Glassfish Application Server, and now you can deploy it on another machine "For security purposes"..

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 second 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


Monday, December 24, 2012

Segment Trees and lazy propagation

In this topic i will explain a very interesting data structure that can be used to solve a specific set of problems. I will start by explaining its definition and the proceeding with an example problem to solve with it.

Table of contents:
  • What  is segment trees?
  • Order of growth of segment trees operations
  • Show me your code
  • Lazy propagation
  • Sample problems to try 
  • References
What is segment trees?

Segment Trees is a Tree data structure for storing intervals, or segments, It allows querying which of the stored segments contain a given point. It is, in principle, a static structure; that is, its content cannot be modified once the structure is built. It only uses O(N lg(N)) storage.

A segment trees has only three operations: build_tree, update_tree, query_tree.

Building tree: To init the tree segments or intervals values
Update tree: To update value of an interval or segment
Query tree: To retrieve the value of an interval or segment

Example Segment Tree:
  • The first node will hold the information for the interval [i, j]
  • If i<j the left and right son will hold the information for the intervals [i, (i+j)/2] and [(i+j)/2+1, j]
Notice that the height of a segment tree for an interval with N elements is [logN] + 1. Here is how a segment tree for the interval [0, 9] would look like:

 

Order of growth of segment trees operations
  • build_tree: O(N lg(N)) 
  • update_tree: O(lg(N + k)) 
  • query_tree: O(lg(N + k))
K = Number of retrieved intervals or segments

Show me your code

 


Lazy Propagation 

Sometimes a segment tree operation wouldn't survive if the problem constraints is too large, here it come lazy propagation along with the segment tree.

In the current version when we update a range, we branch its childs even if the segment is covered within range. In the lazy version we only mark its child that it needs to be updated and update it when needed.




Note: Read my solution for problem Can you answer these queries I in this article.


Sample Problems to try
References

Wednesday, December 19, 2012

Lucas' Theorem to solve binomial coefficients

I am happy cause only one problem and i will be done solving interviewstreet mathematical problems. After solving the Binomial Coefficients  problem i think i have something useful to share with you.

In this problem you are given two number N and P, where P is a prime number. You are requested to find how many binomial coefficients of n become to 0 after modulo by P.

So the output should be the number of  \tbinom nks (0<=k<=n)  each of which after modulo operation by P is 0.

I hear you saying its an easy problem, but you won't say that after knowing about the input constraints.

Constraints:
n is less than 10500.
P is less than 109.

For such large number i will use Java to solve this problem, thanks to the BigInteger Class in java. After doing some search i found that there is a theorem called Lucas' Theorem which is as follows:

Given n, k, and prime p, binomial (n,k) is divisible by p if and only if at least one of  the base p digits of k is larger than the corresponding digit of m (also base p digit).

So to solve this problem we need to find the base P representation of n, and counting how many numbers (R) that have all digits (in base P) less than the corresponding digit in n.

1. We have k is between 0 and n, so we have (n+1) numbers to check with n.
2. Now need to find R.

Now if n in base P is as follows: abc...

Then we need to get all numbers that have:

1. First digit is 0, 1, 2, .... a-1
2. Second digit is 0, 1, 2, .... b-1
3. Third digit is 0, 1, 2, ...., c-1

Obviously R = (a+1)*(b+1)*(c+1).

So we have (n+1) total numbers, subtracting  R, then the result would be n+1-R.

Friday, December 7, 2012

Hacking linux keyboard

Now again with another interesting kernel hack, in this post i will show you a sample input device that report keyboard events without pressing any keys. In this sample i will report the left arrow key pressed event whenever the user pressing any key.

Actually i was building a new keyboard hardware with its device driver, but as i don't have time to share a video with you now about my new se7so keyboard, i thought about something interesting to share with you :).

As the user will be forced to write from right to left, instead of writing from left to right, I was trying to login my machine and because of the module was loaded i was forced to write my password from right to left :D "what comes around, goes around".

You can download the code from sourceforge.

Enjoy the video ;).