Monday, August 31, 2015

Bit Manipulation

On this post, we will look into how to use "Bitwise Operators" to manipulate bits of a number. Bit manipulation is a useful tool that can sometimes perform complicated tasks in a simple manner.

We will work on integer values and assume each number has $32$ bits in them. If a number has only $0$ or $1$ as its digit, then assume that it is in binary form. All position of bits is $0$ indexed.

Since binary values can only be $0$ or $1$, we sometimes refer them like light bulbs. A bit being "Off" means it has value $0$ and being "On" means it has value $1$. Bit $1$ is also referred as "Set" and bit $0$ as "Reset".

We need to have a strong grasp of how AND, OR, Negation, XOR and Shift Operators work to understand how bit manipulation works. If you have forgotten about them, then make sure you revise.

Checking Bit at Position X

Given a number $N$, find the value of its bit at position $X$. For example, if $N=12=(1100)_2$ and $X=2$, then value of bit is $1$. 

So how do we find it? We can find this value in three steps:
  1. Let, $mask = 1 << X$
  2. Let, $res = N \: \& \: mask$
  3. If $res$ is $0$, then bit is $0$ at that position, else bit is $1$.
Let us see it in action with the example above, $N = 12$ and $X = 2$.

$1100$
$\underline{0100} \:\&$ ( $1 << 2$ is just $1$ shifted to left twice )
$0100$

Now, what happened here? In step $1$, when we left shifted $1$ $X$ times, we created a number $mask$ which has bit $1$ only at position $X$. This is a useful application of left shift operator. Using this, we can move $1$ bit anywhere we want. 

Next, at step $2$, we find the result of performing AND operator between $N$ and $mask$. Since $mask$ has $0$ in all positions except $X$, the result will have $0$ in all places other than $X$. That's because performing AND with $0$ will always give $0$. Now, what about position $X$. Will the result at position $X$ have $0$ too? That depends on the bit of $N$ at position $X$. Since, the $X$ bit in the mask is $1$, the only way result will have $0$ at that position if $N$ has $0$ in that position.

So, when all position of $res$ is $0$, that is $res$ has value $0$, we can say that $N$ has $0$ bit in position $X$, otherwise it doesn't.

Set Bit at Position $X$

Given a value $N$, turn the bit at position $X$ of $N$ to $1$. For example, $N=12=1100$ and $X=0$, then $N$ will become $13=1101$. 

This can be done in two steps:
  1. Let, $mask = 1 << X$
  2. $N = N \:|\: mask$
$1100$
$\underline{0001}\:|$
$1101$

In step $1$, we shift a $1$ bit to position $X$. Then we perform OR between $N$ and $mask$. Since $mask$ has 0 in all places except $X$, in result all those places remain unchanged. But $mask$ has $1$ in position $X$, so in result position $X$ will be $1$. 

Reset Bit at Position $X$

Given a value $N$, turn the bit at position $X$ of $N$ to $0$. For example, $N=12=1100$ and $X=3$, then $N$ will become $4=0100$. 

This cane be done in three steps:
  1. Let, $mask = 1 << X$
  2. $mask = \sim mask$
  3. $N = N \:\&\: mask$
$1100$
$\underline{0111}\:\&$ ( We first got $1000$ from step $1$. Then used negation from step 2.)
$0100$

In step $1$ we move $1$ bit to position $X$. This gives us a number with $1$ in position $X$ and $0$ in all other places. Next we negate the $mask$. This flips all bits of $mask$. So now we have $0$ in position $X$ and $1$ in all other places. Now when we perform AND between $mask$ and $N$, it forces the bit at the $X$ position to $0$ and all other bits stay intact.

Toggle Bit at Position X

Given a value $N$, toggle the bit at position $X$, i.e, if bit at $X$ is $0$ then make it $1$ and if it is already $1$, make it $0$. 

This can be done in two steps:
  1. Let, $mask = 1 << X$
  2. $N = N \:\wedge\: mask$
First we shift bit $1$ to our desired position $X$ in step $1$. When XOR is performed between a bit and $0$, the bit remains unchanged. But when XOR performed between a bit and $1$, it toggles. So all position except $X$ toggles during step 2, since all position in mask except $X$ is $0$.

Coding Tips

When coding these in C++, make sure you use lots of brackets. Bitwise operators have lower precedence than $==$ and $!=$ operator which often causes trouble. See here

What would be the output of this code:
if ( 0 & 6 != 7 ) printf ( "True\n" );
else printf ( "False\n" );
You might expect things to follow this order: $0 \:\&\: 6 = 0$ and $0 \:!= 7$, so "True" will be output. But due to precedence of operators, the output comes out "False". First $6\: != 7$ is checked. This is true, so $1$ is returned. Next $0 \:\&\:1$ is performed which is $0$.

The best way to avoid these kind of mishaps is to use brackets whenever you are using bitwise operators. The correct way to write the code above is:
if ( (0 & 6) != 7 ) printf ( "True\n" );
else printf ( "False\n" );
This prints "True" which is our desired result.

Conclusion

These are the basics of bit manipulation and by no means the end of it. There are lots of other tricks that are yet to be seen, such as "Check Odd or Even", "Check Power of Two", "Right Most Bit". We will look into them some other day.

Reference

  1. forthright48 - Bitwise Operators
  2. CPPReference - C++ Operator Precedence

Thursday, August 27, 2015

Bitwise Operators

We know about arithmetic operators. The operators $+,-,/$ and $\times$ adds, subtracts, divides and multiplies respectively. We also have another operator $\%$ which finds the modulus.

Today, we going to look at $6$ more operators called "Bitwise Operators". Why are they called "Bitwise Operators"? That's because they work using the binary numerals (bits, which are the individual digits of the binary number) of their operands. Why do we have such operators? That's because in computers all information is stored as strings of bits, that is, binary numbers. Having operators that work directly on them is pretty useful.

We need to have a good idea how Binary Number System works in order to understand how these operators work. Read more on number system in [1]Introduction to Number Systems. Use the $decimalToBase()$ function from [1] to convert the decimal numbers to binary and see how they are affected.

Bitwise AND ($\&$) Operator

The $\&$ operator is a binary operator. It takes two operands and returns single integer as the result. Here is how it affects the bits.

$0 \: \& \: 0 = 0$
$0 \: \& \:  1 = 0$
$1 \: \& \:  0 = 0$
$1 \: \& \:  1 = 1$

It takes two bits and returns another bit. The $\&$ operator will take two bits $a, b$ and return $1$ only if both $a$ AND $b$ are $1$. Otherwise, it will return $0$.

But that's only for bits. What happens when we perform $\&$ operation on two integer number?

For example, what is the result of  $A \: \& \:  B$ when $A = 12$ and  $ B =10$?

Since $\&$ operator works on bits of binary numbers we have to convert $A$ and $B$ to binary numbers.

$A = (12)_{10} = (1100)_2$
$B = (10)_{10} = (1010)_2$

We know how to perform $\&$ on individual bits, but how do we perform $\&$ operation on strings of bits? Simple, take each position of the string and perform and operations using bits of that position.

$1100$
$\underline{1010}\: \&$
$1000$

Therefore, $12 \: \& \:  10 = 8$.

In C++, it's equivalent code is:
printf ("%d\n", 12 & 10 );

Bitwise OR ($|$) Operator

The $|$ operator is also a binary operator. It takes two operands and returns single integer as the result. Here is how it affects the bits:

$0 \: | \: 0 = 0$
$0 \: | \: 1 = 1$
$1 \: | \: 0 = 1$
$1 \: | \: 1 = 1$

The $|$ operator takes two bits $a, b$ and return $1$ if $a$ OR $b$ is $1$. Therefore, it return $0$ only when both $a$ and $b$ are $0$.

What is the value of $A\:|\:B$ if $A=12$ and $B=10$? Same as before, convert them into binary numbers and apply $|$ operator on both bits of each position.

$1100$
$\underline{1010}\: |$
$1110$

Therefore $12\:|\:10 = 14$.
printf ( "%d\n", 12 | 10 );

Bitwise XOR ($\wedge$) Operator

Another binary operator that takes two integers as operands and returns integer. Here is how it affects two bits:

$0 \: \wedge \: 0 = 0$
$0 \: \wedge \: 1 = 1$
$1 \: \wedge \: 0 = 1$
$1 \: \wedge \: 1 = 0$

XOR stands for Exclusive-OR. This operator returns $1$ only when both operand bits are not same. Otherwise, it returns $0$.

What is the value of $A\:\wedge\:B$ if $A=12$ and $B=10$?

$1100$
$\underline{1010}\: \wedge$
$0110$

Therefore, $12\:\wedge\:10 = 6$.

In mathematics, XOR is represented with $\oplus $, but I used $\wedge$ cause in C++ XOR is performed with $\wedge$.
printf ( "%d\n", 12 ^ 10 );

Bitwise Negation ($\sim$) Operator

This is a unary operator. It works on a single integer and flips all its bits. Here is how it affects individual bits:

$\sim\:0 = 1$
$\sim\: 1 = 0$

What is the value of $\sim \: A$ if $A = 12$?

$\sim \: (1100)_2 = (0011)_2 = (3)_{10}$

But this will not work in code cause $12$ in C++ is not $1100$, it is $0000...1100$. Each integer is $32$ bits long. So when each of the bits of the integer is flipped it becomes $11111...0011$. If you don't take unsigned int, the value will even come out negative.
printf ( "%d\n", ~12 );

Bitwise Left Shift ($<<$) Operator

The left shift operator is a binary operator. It takes two integers $a$ and $b$ and shifts the bits of $a$ towards LEFT $b$ times and adds $b$ zeroes to the end of $a$ in its binary system.

For example, $(13)_{10} << 3 = (1101)_2 << 3 = (110100)_2$.

Shifting the bits of a number $A$ left once is same as multiplying it by $2$. Shifting it left three times is same as multiplying the number by $2^3$.

Therefore, the value of $A << B = A \times 2^B$.
printf ( "%d\n", 1 << 3 );

Bitwise Right Shift ($>>$) Operator

The $>>$ Operator does opposite of $<<$ operator. It takes two integer $a$ and $b$ and shifts the bits of $a$ towards RIGHT $b$ times. The rightmost $b$ bits are lost and $b$ zeroes are added to the left end.

For example, $(13)_{10} >> 3 = (1101)_2 >> 3 = (1)_2$.

Shifting the bits of a number $A$ right once is same as dividing it by $2$. Shifting it right three times is same as dividing the number by $2^3$.

Therefore, the value of $A >> B = \lfloor \frac{A}{2^B} \rfloor$.
printf ( "%d\n", 31 >> 3 );

Tips and Tricks

  1. When using $<<$ operator, careful about overflow. If $A << B$ does not fit into $int$, make sure you type cast $A$ into $long\:long$. Typecasting $B$ into $long\:long$ does not work.
  2. $A \:\&\:B \leq MIN(A,B)$ 
  3. $A\:|\:B \geq MAX(A,B)$



That's all about bitwise operations. These operators will come useful during "Bits Manipulation". We will look into it on next post.

Resource

SPOJ LCMSUM - LCM Sum

Problem

Problem Link - SPOJ LCMSUM

Given $n$, calculate the sum $LCM(1,n) + LCM(2,n) + ... + LCM(n,n)$, where $LCM(i,n)$ denotes the Least Common Multiple of the integers $i$ and $n$.

Solution

I recently solved this problem and found the solution really interesting. You can find the formula that solves this problem on OEIS. I will show you the derivation here.

In order to solve this problem, you need to know about Euler Phi Function, finding Divisor using Sieve and some properties of LCM and GCD.

Define SUM

Let us define a variable SUM which we need to find.

$SUM = LCM(1,n) + LCM(2,n) + ... + LCM(n,n)$ (take $LCM(n,n)$ to the other side for now )
$SUM - LCM(n,n) = LCM(1,n) + LCM(2,n) + ... + LCM(n-1,n)$

We know that $LCM(n,n) = n$.

$SUM - n = LCM(1,n) + LCM(2,n) + ... + LCM(n-1,n)$ ($eq 1$)

Reverse and Add

$SUM - n = LCM(1,n) + LCM(2,n) + ... + LCM(n-1,n)$ (Reverse $eq 1$ to get $eq 2$)
$SUM - n = LCM(n-1,n) + LCM(n-2,n) + ... + LCM(1,n)$ ( $eq 2$ )

No what will we get if we add $eq 1$ with $eq 2$? Well, we need to do more work to find that.

Sum of $LCM(a,n) + LCM(n-a,n)$

$x = LCM(a,n) + LCM(n-a,n)$
$x = \frac{an}{gcd(a,n)} + \frac{(n-a)n}{gcd(n-a,n)}$ ($eq 3$)

Arghh. Now we need to prove that $gcd(a,n)$ is equal to $gcd(n-a,n)$.

If $c$ divides $a$ and $b$, then, $c$ will divide $a+b$ and $a-b$. This is common property of division. So, if $g = gcd(a,n)$ divides $a$ and $n$, then it will also divide $n-a$. Hence, $gcd(a,n)=gcd(n-a,n)$.

So $eq 3$ becomes:

$x = \frac{an}{gcd(a,n)} + \frac{(n-a)n}{gcd(a,n)}$
$x = \frac{an + n^2 -an}{gcd(a,n)}$.
$x = \frac{n^2}{gcd(a,n)}$.

Now, we can continue adding $eq 1$ with $eq 2$.

Reverse and Add Continued

$SUM - n = LCM(1,n) + LCM(2,n) + ... + LCM(n-1,n)$ ($eq 1$)
$SUM - n = LCM(n-1,n) + LCM(n-2,n) + ... + LCM(1,n)$ ( $eq 2$ Add them )
$2(SUM-n)= \frac{n^2}{gcd(1,n)} +  \frac{n^2}{gcd(2,n)} + ...  \frac{n^2}{gcd(n-1,n)}$
$2(SUM-n ) = \sum_{i=1}^{n-1}\frac{n^2}{gcd(i,n)}$
$2(SUM-n ) = n\sum_{i=1}^{n-1}\frac{n}{gcd(i,n)}$  ( take $n$ common )

Group Similar GCD

What are the possible values of $g = gcd(i,n)$? Since $g$ must divide $n$, $g$ needs to be a divisor of $n$. So we can list the possible values of $gcd(i,n)$ by finding the divisors of $n$.

Let $Z = n\sum_{i=1}^{n-1}\frac{n}{gcd(i,n)}$. Now, every time $gcd(i,n) = d$, where $d$ is a divisior of $n$, $n \times \frac{n}{d}$ is added to $Z$. Therefore, we just need to find, for each divisor $d$, how many times $n \times \frac{n}{d}$ is added to $Z$.

How many values $i$ can we put such that $gcd(i,n) = d$? There are $\phi(\frac{n}{d})$ possible values of $i$ for which we get $gcd(i,n)=d$. Therefore:

$2(SUM-n ) =  n\sum_{i=1}^{n-1}\frac{n}{gcd(i,n)}$
$2(SUM-n ) =  n\sum_{d\:|\:n, d \neq n } \phi(\frac{n}{d}) \times \frac{n}{d}$ (but, $\frac{n}{d}$ is also a divisor of $n$ )
$2(SUM-n ) =  n\sum_{d\:|\:n, d \neq 1 } \phi(d) \times d$ ( when $d = 1$, we get $\phi(1)\times 1$ ) 
$2(SUM-n ) =  n( \sum_{d\:|\:n } ( \phi(d) \times d ) -1 )$ 
$2(SUM-n ) =   n \sum_{d\:|\:n } (\phi(d) \times d ) - n$
$2SUM - 2n + n  = n\sum_{d\:|\:n } \phi(d) \times d $
$2SUM - n = n\sum_{d\:|\:n } \phi(d) \times d $
$$2SUM = n( \sum_{d\:|\:n } \phi(d) \times d )+ n \\2SUM = n ( \sum_{d\:|\:n } ( \phi(d) \times d ) + 1 )\\ \therefore SUM = \frac{n}{2} ( \sum_{d\:|\:n } ( \phi(d) \times d ) + 1 )$$
Using this formula we can solve the problem.

Code

#include <bits/stdc++.h>

#define FOR(i,x,y) for(vlong i = (x) ; i <= (y) ; ++i)

using namespace std;
typedef long long vlong;

vlong res[1000010];
vlong phi[1000010];

void precal( int n ) {
    ///Calculate phi from 1 to n using sieve
    FOR(i,1,n) phi[i] = i;
    FOR(i,2,n) {
        if ( phi[i] == i ) {
            for ( int j = i; j <= n; j += i ) {
                phi[j] /= i;
                phi[j] *= i - 1;
            }
        }
    }

    ///Calculate partial result using sieve
    ///For each divisor d of n, add phi(d)*d to result array
    FOR(i,1,n){
        for ( int j = i; j <= n; j += i ) {
            res[j] += ( i * phi[i] );
        }
    }
}

int main () {
    precal( 1000000 );

    int kase;
    scanf ( "%d", &kase );

    while ( kase-- ) {
        vlong n;
        scanf ( "%lld", &n );

        ///We already have partial result in res[n]
        vlong ans = res[n] + 1;
        ans *= n;
        ans /= 2;

        printf ( "%lld\n", ans );
    }

    return 0;
}
We nee to precalculate partial result using a sieve for all values of $N$. Precalculation has a complexity of $O(N\: lnN)$. After pre-calculation, for each $N$ we can answer in $O(1)$.

Reference

  1. OEIS - A051193

Tuesday, August 25, 2015

Contest Analysis: BUET Inter-University Programming Contest - 2011, UVa 12424 - 12432

Last week (2015-08-21) we practiced with this set. Some of the problems didn't have proper constraints, which caused some headaches. This contest originally followed Google Code Jam format with two subproblems ( small and large ) for each problem. Each of the sub-problems had a different constraint on them. But when they uploaded it online, they removed both constraints from some problems, making things confusing. We ended up guessing the constraints when solving them.

During contest time, we managed to solve $B, E$, and $H$. I was stuck with $F$. Should have moved on. $C$ and $D$ were far easier than $F$. Bad decision from my end.

UVa 12424 - Answering Queries on a Tree

Complexity: $O(logN)$
Category: Segment Tree, DFS, LCA

At first it seemed like a problem on Heavy Light Decomposition. But after a moment I realized it can be done without HLD.

Run a dfs from any node and make the tree rooted tree. Now consider the color $X$. For every node that have color $X$, the edge from its parent will have cost $1$. Since the root can also have color $X$, we need to add a dummy node $0$ and make that root. Now, run another dfs and calculate the distance from the root to each node. We repeat this for all $10$ color and build $10$ trees. Let us call tree with color of node $X$, $T_X$.

Now, whenever we have an update to change node $u$ from color $X$ to color $Y$, it means that the edge entering $u$ in tree $T_X$ will now have a cost $0$. Since its cost decreased, the distance from root of all the nodes in the subtree of $u$ will be lowered by $1$. We can apply this change using preorder traversal dfs + segment tree. Do the opposite for $T_Y$. Increase the value of all nodes under subtree $u$ in $T_Y$.

And for query $u$ and $v$, find the distance between these two nodes in each of the $10$ trees. This can be done in $O(logN)$ using LCA + Segment Tree.

Code: http://ideone.com/UEU9J4

UVa 12425 - Best Friend

Complexity: $O(\sqrt{N} + \sqrt[3]{N} \times \text{Complexity of } \phi (N) )$
Category: Number Theory, Euler Phi, GCD

So for given integers $N$ and $X$, we have to find out how many number $I$ are there such that $gcd(N, I) \leq X$.

In order to calculate $gcd(N,I) \leq X$, I first need to be able to calculate how many numbers are there such that $gcd(N,I) = Y$. I started with a small value of $Y$ first. So the first thing I asked myself, how many numbers are there such that $gcd(N, I) = 1$? The answer is $\phi (N)$. Then I asked how many are there such that $gcd(N, I) = 2$? 

This took a moment, but I soon realized, if $gcd(N,I)$ is equal $2$, then if I divide both $N$ and $I$ by $2$, then $gcd(\frac{N}{2},\frac{I}{2})$ will be equal to $1$. Now, all I had to calculate how many $I$ are there such that $gcd(\frac{N}{2},\frac{I}{2}) = 1$. The answer is $\phi (\frac{N}{2})$.

Therefore, there are $\phi (\frac{N}{Y})$ number of $I$ such that $gcd(N,I) = Y$.

Great. Now we can calculate number of $I$ such that  $gcd(N,I) = Y$. Now all we need to do is find the sum of all number of $I$ for which $gcd(N, I) = Y$ and $Y \leq X$. GCD of $N$ and $I$ can be as high as $10^{12}$, so running a loop won't do.

Next I thought, what are the possible values of $g = gcd(N,I)$? Since $g$ is a number that divides $N$ and $I$, $g$ must be a divisor of $N$. So I simply calculated all the divisors of $N$ using a $\sqrt{N}$ loop. Next for each divisor $d$, I calculated $\phi ( \frac{N}{d})$ which is the number of ways to chose $I$ such that $gcd(N,I) = d$.

Now, for each query $X$, all I needed to do was find sum of $\phi ( \frac{N}{d} ) $, where $d$ is a divisor of $N$ and $d \leq X$. Since all values of $\phi ( \frac{N}{d} ) $ was precalculated, using cumalitive sum I answered it in $O(1)$.


Complexity: $O(N^2\:logN)$
Category: Two Pointer, Geo, Binary Search

We need to choose three points of a convex hull such that the area does not exceed $K$. 

First, we need to know, for two points $i$ and $j$, $j>i$, what is the point $m$ such that $(i,j,m)$ forms the largest triangle possible. This can be done by selecting two points with two loops and then using ternary search. But I used two pointer technique to find $m$ for every pair of $(i,j)$ in $O(N)$.

After that, for ever pair of $(i,j)$ I used binary search twice. Once on points $[j+1,m]$ and once on points $[m+1,n]$. Using binary search I found the point $x$ which gives the highest area that does not exceed $K$. Then I added $x-j-1$ in first BS and $n-1-m$ in second case.

Careful about not adding a degenerate triangle. Hanlde the edge cases properly.


Complexity: $O(1)$
Category: Game Theory, Combinatorics, Nim Game, Bogus Nim

This game can be converted to into a game of pile. Let the distance between the left boundary and gold coin be $x$ and the distance between diamond and silver coin be $y$. Now, instead of playing on the board, we can play with two piles of height $x$ (first pile) and $y$ (second pile).

Moving gold coin left is same as removing one coin from the first pile. Moving diamond coin left is same as increasing second pile by one. Moving silver coin left is same as removing one coin from the second pile. Moving gold and silver coin left is same as removing one coin from both first and second pile.

Now, note that is a state of $(x,y)$ is losing and I try to increase the second pile by one, my opponent then can simply take one from second pile and put me back to same situation. That is, increasing the second pile has no effect in this game. This is a "Bogus" move. We will ignore this move.

Next I drew a $5 \times 5$ table and calculated the Win or Lose state of each possible $(x,y)$. I found that only when both $x$ and $y$ are even, the first person loses. That is the second person, me, wins the game when $x$ and $y$ both are even.

What are the possible values of $x$? $x$ can only be between $a_1$ and $a_2$. Find number of even numbers in this range. Let this be $r_1$. Next, $y$ can be between $c_1$ and $c_2$. Let the number of even numbers in this range be $r_2$. The distance between gold and diamond has no effect in this game. So we can put any amount of gap there.

Therefore, number of possible combination is $r_1 \times r_2 \times (b_2 - b_1 + 1 )$.

Complexity: $O(\sqrt{M})$
Category: Adhoc

This was the easiest problem in the set. In this problem, we are given $N$ nodes and $M$ edges, we have to find out how many leaves can this graph have if we maximize leaves.

The problem says the graph will be connected. So I created a star-shaped graph with it using $N-1$ edges. Let the center node be $0$ and remaining nodes be the numbers from $1$ to $N-1$. This gives me $N-1$ critical edges. Then I checked if I still have more edges left? If I do, then I need to sacrifice a critical edge.

The first edge that gets sacrifice is a special case. This is because, no matter which two nodes you connect, it will reduce the number of critical edge by $2$. There is no helping it. So let's connect $1$ and $2$ and reduce our edge by $1$ and critical edge by $2$. 

Then if we still have more edges left, then we need to sacrifice another critical length. From now on, at each step only one critical edge will be removed. Also, this time we can add $2$ edges to the graph by removing one critical edge. Connect an edge between $(3,1)$ and $(3,2)$. Reduce $M$ by $2$ and critical edge by $1$.

Next is node $4$. We can add $3$ edges by removing $1$ critical edge now. We need to keep on doing this until all edges finish.

I guess it is possible to solve the problem in $O(1)$ but I didn't bother with it. Number of test case was small anyway.

Complexity: $O(N\:logN)$
Category: Binary Indexed Tree, Number Theory

We have to find the number of triplets such that $a + b^2 \equiv c^3$ and $a \leq b \leq c$. Here is what I did.

Iterate over $c$ from $N$ to $1$ using a loop. Let the loop iterator be $i$. Now for each $i$, we do the following:
  1. Find $x = ( i \times i \times i ) \:\%\: k$ and store $x$ in a BIT. BIT stores all occurrences of $c$ and by iterating in reverse order we make sure that all $c$ in BIT is greater or equal to $i$.

  2. Let $b = ( i \times i ) \: \% \: k$. This is the current $b$ we are working with. In BIT we have $c$ which are $c \geq b$. Now all we need is to find possible values of $a$.

  3. Now, take any value of $c$ from BIT. For this $c$ and current $b$, how many ways can we choose $a$ so that $a + b \equiv c$. Notice that $a$ can be anything from $1$ to $k$, or $0$ to $k-1$. Therefore, no matter what is the value of $c$ we can chose $1$ number from $0$ to $k-1$ so that $a + b \equiv c$.

    Let $y = \frac{i}{k}$. This means we have $y$ segments, in which value of $a$ spans from $0$ to $k-1$. So we add $y$ for each possible $c$ we take from BIT. Let $total$ be number of $c$ we have inserted till now. So we add $total \times y$ to $result$.

  4. But it's not over yet. What about the values of $a$ that we did not use. To be more exact, let $r = i \: \% \: k$. If $ r > 0$, then we have unused values of $a$. We need to use these.

    But we have to be careful. We have used up values of $a$ from $1$ to $y\times k$. So the remaining $a$ we have is $1$ to $r$. $0$ is not included.

    Since we don't have all values from $0$ to $k-1$, we are not able to handle all possible values $c$ any more. How many can we handle exactly?

    $a + b \equiv c$
    $a \equiv c - b$

    But we need $a$ can only be between $1$ and $r$.

    $1 \leq c - b \leq r$
    $\therefore 1+b \leq c \leq r + b$

    Find out how many $c$ are there with values between $1+b$ and $r+b$ from BIT. Let this value be $z$. For each of those $c$ we can use one value of $a$ between $1$ to $r$. So add $z$ to $result$.
And that's it. Be careful when handling edge cases in BIT.

Code: http://ideone.com/iMBUmU

UVa 12430 - Grand Wedding

Complexity: $O(N \:logN)$
Category: DFS, Bicoloring, Binary Search

Constraint was missing. I assumed $N <= 10^6$.

We have to remove some edges and then assign guards such that no edge is left unguarded and no edge has guards in both ends. We have to output the maximize the value of the edges that we remove.

Binary search over the value of edges which we need to remove. Now, let's say we removed all edges with value greater or equal to $x$. How do we decide that a valid assignment of guard is possible in this problem? 

Suppose all the nodes in the graph are colored white. If we assign guard in a node, then we color it black. Now, two white nodes cannot be adjacent cause that would mean the edge is left unguarded. Two black nodes cannot be adjacent otherwise guards will chat with each other. That is both ends of an edge cannot have same color in the graph. This is possible when the graph has no cycle of odd length. If a graph has no cycle, then it is bicolorable. So all we need to do is check if the graph is bicolorable.

Now, two special cases. If we can bicolor the graph without removing any edge, then answer is $0$. If we have to remove all edges then answer is $-1$. Why? Cause in the problem it is said we can remove proper subset of edges. The full set is not a proper subset.


UVa 12431 - Happy 10/9 Day

Complexity: $O(\:(logN)^2\:)$
Category: Divide and Conquor, Modular Exponentiation, Repeated Squaring

We have to find the value of:

$ ( d\times b^0 + d\times b^1 + d \times b^2 + ... + d \times b^{n-1} ) \: \%\: M$
$ ( d ( b^0 + b^1 + b^2 + ... + b^{n-1} ) ) \: \%\: M$.

Therefore, the problem boils down to finding the value of $( b^0 + b^1 + b^2 + ... + b^{n-1} ) \: \%\: M$. We cannot use the formula of geometric progression since $M$ and $b-1$ may not be coprime. So what do we do? We can use divide and conquor. How? Let me show you an example.

Suppose $f(n) = (b^0 + b^1 + b^2 + ... + b^n)$. Let us find $f(5)$.
$f(5) = b^0 + b^1 + b^2 + b^3 + b^4 + b^5$
$f(5) = (b^0 + b^1 + b^2) + b^3 ( b^0 + b^1 + b^2)$
$f(5) = f(2) + b^3 \times f(2)$
$f(5) = f(2) \times (1 + b^3)$.

From this we can design a D&C in following way:

$\text{n is odd: }f(n) = f(\frac{n}{2}) \times ( 1 + b^{n/2 + 1 })$
$\text{n is even: }f(n) = f(n-1) + b^n$
$f(0) = 1$

Just apply modular arithmatic with the whole things. Also note that $M$ can be as larg as $10^{12}$, so $(a \times b)$ will overflow if they are multiplied directly, even after making them small by modulus $M$. So use repeated squaring metho to multiply them.


UVa 12432 - Inked Carpets

Complexity:
Category:

I didn't try this problem yet. I guess this was the stopper.


It was a good set. I espcially enjoyed solving $B, D$ and $F$. 

Thursday, August 20, 2015

Contest Analysis: IUT 6th National ICT Fest 2014, UVa 12830 - 12839

I participated in this contest last year. Our team "NSU Shinobis" managed to solve 5 and placed in top 10 ( failed to find the rank list ). The IUT 7th ICT National Fest 2015 is going to be held next month, so I thought I would try to up solve the remaining problems. I tried to compile detailed hints for the problems here.

UVa 12830 - A Football Stadium

Complexity: $O(N^3)$
Category: Loop, DP, Kadane's Algorithm

Given $N$ points, we have to find the largest area of the rectangle which can fit such that there is no point inside the rectangle. Points on boundaries of the rectangle are allowed.

Now imagine a rectangle which has no point inside it and also no point on its boundary. Now focus on the top boundary of that rectangle. Since there is no point on that boundary, we can extend it up until it hits a point or limit. Same can be said for the bottom, left and right boundary. That is, the largest rectangle will have its boundary aligned with the limits or points.

Now let us fix the top and lower boundary of the rectangle. How many possible values can they take? Each boundary can take values of $y$-coordinate from one of the $N$ points or the limits $0$ and $W$. Using two nested loops, we can fix them.

Next we need to fix the left and right boundary. Lets us sort the points according to $x$ first and then according to $y$. Next we iterate over the points. Initially set the left boundary of rectangle aligned to $y$-axis, that is aligned with the left boundary of Sand Kingdom.

Now, for each point, if the $y$ coordinate of the point is above or below or on the boundary we fixed then we ignore them, cause they don't cause any problem. But if they fall inside, then we need to process this point. We put the right boundary aligned to the $x$ coordinate of this point and calculate this rectangle.

But it's not over. We shift the left boundary over to current right boundary and continue process remaining points.

This is a classical problem of Kadane's Algorithm. With two nested loops to fix upper and lower boundary, and another loop inside iterating over $N$ points makes the complexity $O(N^3)$.

$O(N^2)$ solution is possible, by constructing a histogram for each row ( $O(N)$ ) and the calculating area of histogram in $O(N)$. But that's not necessary here.

Code: http://ideone.com/FjvTN2

UVa 12831 - Bob the Builder

Complexity: $O(\sqrt{V}E)$
Category: Max Flow, Bipartite Matching, Dinic's Algorithm, Path Cover

Another classical problem, though we had a hard time solving this one. Mainly cause we didn't realize this was a path cover problem which can be solved using Max Flow.

Take each number provided and generate all child. Consider each number a node and if it possible to generate $B$ from $A$, then add a directed edge from $A$ to $B$. When you are done, you will have DAG.

Now split all the nodes in two. We are going to create a bipartite graph now. On the left side, we will have all the original nodes and on the right side we will have the copy we got by splitting the nodes. Now, for each edge between $A$ and $B$, add edge in the bipartite graph from $A$ on the left side to $B$ on the right side.

Find the matching ( or maximum flow ) of this graph by running  Dinic's Algorithm. The answer will be $\text{Total Nodes} - \text{Matching}$.

Code: http://ideone.com/U1VPNB

UVa 12832 - Chicken Lover

Complexity: $O(M)$
Category: Expected Value, Probability, DP,  Combination

If a shop can make $N$ different items, and in a single day prepares $K$ items from those $N$ items, then how many different sets of menus ($total$) can they make? $total = C^N_K$. Now, if they decide to make chicken that day for sure, how many sets of menus ( $chicken$ ) can they make now? $chicken = C^{N-1}_{K-1}$. So what is the probability $P$ that if I visit a shop I will get to eat chicken? $P = \frac{chicken}{total}$. And what is the probability that I will not eat chicken? $Q = 1- P$.

So now I know the probability of eating chicken for each shop. How do we find the expected value? We will find it using dynamic programming.

The states of dp only be the shop number. $dp(x)$ will give me the expected number of chicken that I can eat if I visit all shops starting from $x$. Our result will be $dp(1)$.

At each state, I have $P_i$ probability that I will eat chicken and $Q$ probability that I will not. So result for each state will be:

$dp ( pos ) = P \times ( 1 + dp ( pos + 1 ) + Q \times dp ( pos + 1 )$
$dp ( pos ) = P + P \times dp ( pos + 1 ) + Q \times dp ( pos + 1 ) $
$dp ( pos ) = P + dp ( pos + 1 ) \times ( P + Q )$
$dp ( pos ) = P + dp ( pos + 1 )$.

In order to print the result in $\frac{A}{B}$ form, we need to avoid $double$ and use integer arithmetic in all places. I implemented my own fraction class for that purpose.

Code: http://ideone.com/mGKwuL

UVa 12833 - Daily Potato

Complexity: $O(26 \times N)$
Category: String, Manacher's Algorithm

For each query, we have to count the number of palindromes which are substring of $S$, starts and ends with given $C$ and has exactly $X$ occurrences of $C$ in it. Since it deals with palindrome, perhaps it has something to do with Manacher's Algorithm?

With Manacher's Algorithm, we can find out the maximum length of palindrome in $O(N)$. But what's more, we can actually generate an array $M$ which gives us, for each center in the extended string $E$, ( $aba$ when extended becomes $\text{^#a#b#a#\$} $ ) the maximum length of palindrome with that particular center. How can we use this knowledge to solve our problem?

Let us consider the character $'a'$ only. We can easily extend it for other characters by repeating the whole process $26$ times.

Suppose we are working with center $x$. It has a palindrom from it with length $y$. Therfore, in extended string of manacher, the palindrome starts from $x-y$ and ends in $x+y$. Now, how many times does $'a'$ occurs in this palindrome? Using Cumulative Sum it is possible to answer in $O(1)$. Let that occurance be $f = \text{# of times 'a' occurs in palindrome with center x}$. Let us mark this in another array $freq$. That is we mark it like $\text{freq[f]++}$, meaning we have $freq[f]$ palindromes where $'a'$ occurs $f$ times. But wait, what if the palindrome does not start and end with $'a'$? Simple, we keep on throwing the leading and trailing character until it starts and ends with $'a'$ and it will still have $f$ occurances of $'a'$ in it.

So we repeat this for all possible center. Now, if the query is find number of palindromes that starts and ends with $'a'$ and $'a'$ occurs exactly $X$ times, how do we solve it?

First of all, our result will contain $res = freq[X]$ in it. What's more, our result will also contain $res \text{+=}  freq[X+2] + freq[X+4] + freq[X+6] + ...$. Why is that? Take any palindrome that contains more than $X$ occurances of $'a'$. Since they start and end with $'a'$, we can just throw them out of that palindrome and reduce the occurance of $'a'$ in it by $2$. After that, we keep on trimming down head and tail of that palindrome until we reach $'a'$ again. That is, a palindrome with $Y$ occurrences of $'a'$ can be trimmed down to palindrome with $Y-2$, $Y-4$, $Y-6$, $...$ occurrences of $'a'$.

Instead of getting the result $res = freq[X] + freq[X+2] + freq[X+4] + ... $ we can just use cumulative sum again to find it in $O(1)$ here. Just find cumulative sum of alternate terms.

Code: http://ideone.com/brZKXL

UVa 12834 - Extreme Terror

Complexity: $O(N\: logN )$
Category: Adhoc

This was the easiest problem. Each shop gives me some money ($income$) and then for that shop I have to give Godfather some cut ($expense$). So for each shop I get $profit = income - expense$. So I calculate profit for each shop and then sort them. I can now skip $K$ shops. I will of course skip shops for which profit is negative as long as I am allowed to skip.

Code: http://ideone.com/s3iHGz

UVa 12835 - Fitting Pipes Again

Complexity: $O(N!\: N^2)$
Category: Geometry, Trigonometry, Permutation, Packing Problem

It's a packing problem. At first glance it seems like a tough problem but it's not. Let us first define few variables first.


This is the polygon. Each polygon has two horizontal lines through it. We will call them low and top line. Each side of the polygon has length of $x$. The radius of the polygon is $r = \frac{x}{2} + y$.

We are given height of each polygon. But first we will need to calculate the value of $x$ and $y$. We can find their values using trigonometry.

$y^2 + y^2 = x^2$
$2y^2 = x^2$
$x = \sqrt{2y^2}$

We also know that $h = y + x + y$. From that we can derive:

$y + x + y = h$
$2y+x = h$
$2y + \sqrt{2y^2} = h$
$2y + y\sqrt{2} = h$
$y( 2 + sqrt{2} ) = h$
$y = \frac{h}{2} +\sqrt{2}$

With the above, we can find the value of $y = \frac{h}{2} +\sqrt{2}$ and $x = \sqrt{2y^2}$ But why did we find their values?

Okay, what happens when we put to polygon side by side? In order to solve this problem we need to be able to find the distance between the center of two polygon $A$ and $B$.

Now if two polygons are of same height and they are placed side by side, then the difference between their centers will be $d = r_a + r_b$. What happens when two polygons of arbitrary height come beside each other?

$3$ things can happen when two polygon $A$ and $B$ are placed side by side. $A$ is on left of $B$.

  1. Height of bottom line of $A$ is higher than height of top line of  $B$. In this case, $A$ is so big that $B$ slides inside the radius of $A$.
  2. Height of bottom line of $B$ is higher than height of top line of $A$. In this case $A$ is so small that it slides inside the radius of $B$.
  3. Nobody can slide inside each others radius. So $d = r_a + r_b$.
We need to calculate the value of $d$ for step $1$ and $2$. That can also be easily done using trigonometry. Try to draw some diagram yourself.

So we used $x$ and $y$ to find the value of $d$ between two polygon. How do we use this to find the minimum width of box? 

Suppose we are trying to pack the polygons in some order. Which order? We don't know which order will give us minimum width so we try them all. There will be $N!$ order. 

So for each order, what will be the minimum of width. First lets take the first polygon, and put it inside the empty box such that it touches the left side of the box. We will calculate the center of each polygon relative to the left side of the box. The first box will have center at $r_0$.

Now take the second box. First imagine there is no polygon in the box. There where will be the center of the second polygon? Ar $r_1$. Now, since there is a polygon, we will try to put it beside that one. Where will be the center now? $r_0 + d$. We will take the maximum possible center.
Repeat this for the third polygon. Empty box, beside first polygon, beside second polygon. Take the maximum again. Repeat this for all polygon.

From all polygon, find the one with maximum center position. Add radius of that polygon to it's center to find the width.

Take the minimum width from all permutation. 


UVa 12836 - Gain Battle Power

Complexity: $O(N^2)$
Category: Interval DP, LIS, Knuth Optimization

First we need to calculate the power of each deatheater. We can do that by calculating LIS from both direction and then adding them.

Once we calculate the power, we run an interval dp between $1$ and $N$. The dp will have states $start$ and $end$. Inside the dp a loop between start and end will run, choosing different cut sections. We will take the one with minimum value.

But this results in $O(N^3)$ solution. Using Knuth's optimization we can reduce it to $O(N^2)$. 


UVa 12837 - Hasmot Ali Professor

Complexity: $O(100 \times |S|^2 )$
Category: String, Trie, Data Structure

 We will create two trie trees. The first one will contain all the queries. Take a query and concatanate them in a special way. Take the first string of the query and add a $\text{'#'}$ and then reverse the second string of the query and attach it to result. That is, if we have $abc$ and $pqr$ as query, then special string will be $\text{abc#rqp}$. Insert all special strings for each query in the first tries.

Now, let us process the main string $S$. We will take each of its suffix and insert into the second trie. Now, when inserting the suffixes, each time a new node is created, we can say that we found a unique substring. 

Each time we find a unique substring we will process it further. Take the unique substring, and using brute force generate all possible special strings ( the one we made using query strings ) with the first and last characters of the unique string. We don't need to take more than $10$ characters from each end.

For each of those special string we made from unique substring, we will query the first trie with it and find the node where it ends. We will add one to that node number in a global array.

Once we finish processing all nodes in the second trie, we will simply traverse the first trie according to each query and print the result found in that node.


UVa 12838 - Identity Redemption

Complexity: Unknown
Category: Matching on General Graph

I didn't manage to solve this yet. It looks like matching on general graph.

UVa 12839 - Judge in Queue

Complexity: $O(N\:logN)$
Category: Data Structure, Heap

We want to minimize the waiting time for each person. So first we will sort the people in descending order. Next we will create a priority queue, which will contain all the service center along with the information about when that service center will be free. Initially all service centers are free.

Now, we take each person and take out the service center that will get free at the earliest time. The person had to originally wait  $x$ minutes and it took $y$ minutes for the service to get free again. So the person had to wait $x+y$ minutes. We insert the service again inside the heap and update its free time by the time it takes to serve one person.

Repeat and keep track of the highest time a person had to wait.




I hope the details are clear enough for everyone to understand. Let me know if you find any mistakes.

Monday, August 17, 2015

Modular Exponentiation

Problem

Given three positive integers $B, P$ and $M$, find the value of $B^P \:\%\: M$.

For example, $B=2$, $P=5$ and $M=7$, then $B^P \:\%\: M = 2^5 \: \% \: 7 = 32 \: \% \: 7 = 4$.

This problem is known as [1]Modular Exponentiation. In order to solve this problem, we need to have basic knowledge about [2]Modular Arithmetic

Brute Force - Linear Solution $O(P)$

As usual, we first start off with a naive solution. We can calculate the value of $B^P \:\%\: M$ in $O(P)$ time by running a loop from $1$ to $P$ and multiplying $B$ to result.
int bigmodLinear ( int b, int p, int m ) {
    int res = 1 % m;
    b = b % m;
    for ( int i = 1; i <= p; i++ ) {
        res = ( res * b ) % m;
    }
    return res;
}
A simple solution which will work as long as the value of $P$ is small enough. But for large values of $P$, for example, $P > 10^9$, this code will time out. Also note that in line $2$, I wrote $res = 1 \: \% \: m$. Some people tend to write $res = 1$. This will produce wrong result when the value of $P$ is $0$ and value of $M$ is $1$.

Divide and Conquer Approach - $O(log_2(P) )$

According to [3]Wiki,
A divide and conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same (or related) type (divide), until these become simple enough to be solved directly (conquer).
That is, instead of solving the big problem $B^P \:\%\: M$, we will solve smaller problems of similar kind and merge them to get answer to the big problem.

So how do we apply this D&C (Divide and Conquer) idea?

Suppose we are trying to find the value of $B^P$. Then three thing can happen.
  1. Value of P is 0 ( Base Case ): $B^0$ is $1$. This will be the base case of our recursion function. Once we reach this state, we no longer divide the problem into smaller parts.
  2. Value of P is Even: Since $P$ is even, we can say that $B^P = B^{\frac{P}{2}} \times B^{\frac{P}{2}}$. For example, $2^{32} = 2^{16} \times 2^{16}$, $3^6 = 3^3 \times 3^3$. Therefore, instead of calculating the value of $x = B^P$, if we find the value of $y = B^{\frac{P}{2}}$, then we can get the value of $x$ as $x = y \times y$. 
  3. Value of P is Odd: In this case we can simply say that $B^P = B \times B^{P-1}$.
Using these three states, we are can formulate a D&C code.
int bigmodRecur ( int b, int p, int m ) {
    if ( p == 0 ) return 1%m; ///Base Case

    if ( p % 2 == 0 ) { ///p is even
        int y = bigmodRecur ( b, p / 2, m );
        return ( y * y ) % m; ///b^p = y * y
    }
    else {
        ///b^p = b * b^(p-1)
        return ( b * bigmodRecur ( b, p - 1, m ) ) % m;
    }
}
In line $2$ we have the base case. We return $1\:\%\:m$ in case value of $m$ is $1$. Next on line $4$ we check if $p$ is even or not. If it is even, we calculate $A^{\frac{P}{2}}$ and return after squaring it. In line $8$, we handle the case when $P$ is odd.

At each step we either divide $P$ by $2$ or subtract $1$ from $P$ and then divide it by $2$. Therefore, there can be at most $2 \times log_2(P)$ steps in D&C approach. Hence complexity is $O(log_2(P) )$.

A Better Solution

A better solution for this problem exists. By using Repeated Squaring algorithm, we can find the Modular Exponentiation faster than D&C. Repeated Squaring Algorithm has the same complexity as D&C, but it is iterative, thus does not suffer from recursion overhead.

We need to know about bitwise operations before we learn Repeated Squaring algorithm. Since we did not cover this topic yet, I won't write about this approach now. Hopefully, once we cover bitwise operations I will write another post

Resource

  1. Wikipedia - Modular Exponentiation
  2. forthright48 - Introduction to Modular Arithmetic
  3. Wikipedia - Divide and conquer algorithms
  4. forthright48 - Introduction to Number Systems

Saturday, August 15, 2015

Leading Digits of Factorial

Problem

Given an integer $N$, find the first $K$ leading digits of $N!$.

For example, for $N=10$ and $K=3$, then first $3$ leading digits of $10! = 3628800$ is $362$.

Finding leading digits uses concepts similar to [1]Number of Trailing Zeroes of Factorial.

Brute Force Solution

Finding the value of $N!$ and then printing the first $K$ digits is a simple but slow solution. Using $long\:long$ we can calculate value $N!$ up to $N \leq 20$ and using Big Integer we can calculate arbitrary $N!$ but with complexity worse than $O(N^2)$.

Solution Using Logarithm

In [1], we say that a logarithm of value $x$ is $y$ such that $x = 10^y$. For now let us find out leading digits of a value $x$ instead of $N!$. We will extend it to cover factorials later.

So, we know that $log_{10}(x) = y$, where $y$ will be some fraction. Let us separate $y$ into its integer and decimal part and call them $p,q$. For example, if $y = 123.456$, then $p=123$ and $q=0.456$.

Therefore, we can say that $log_{10}(x) = p + q$. Which means, $x = 10^y = 10^{p+q}=10^p \times 10^q$.

Now expand the values of $10^p$ and $10^q$. If $A=10^p$, then $A$ will simply be a power of $10$ since $p$ is an integer. To be more exact, $A$ will be $1$ with $p$ trailing zeroes. For example, $A=10^3 = 1000$. What about $B=10^q$?

Since $q$ is a fraction which is $0 \leq q < 1$, value of $B$ will be between $10^0 \leq B < 10^1$, that is, $1 \leq B < 10$.

Okay, we got the value of $A$ and $B$, what now? We know that if we multiply $A$ and $B$ we will get $x$. But don't multiply them just yet. Think for a bit what will happen when we multiply a decimal number with $10$. If it is integer, it will get a trailing zero, e.g, $3 \times 10 = 30$. But if it is a fraction, its decimal point will shift to right, e.g $23.65 \times 10 = 236.5$. Actually, decimal points shifts for integer numbers too, since integer numbers are real numbers with $0$ as fraction, e.g $3 = 3.00$. So in either case multiplying $10$ shifts decimal point to the right. 

So what happens if we multiply, $A$, which is just $10^p$ to $B$? Since $A$ has $10$ in it $p$ times, the decimal point of $B$ will shift to right $p$ times. That is all $A$ does to $B$ is change its decimal point. It does not change the digits of $B$ in any way. Thus, $B$ contains all the leading digits of $x$.

For example, $log_{10}(5420) = 3.7339993 = 3 + 0.7339993$. $\therefore B = 10^0.7339993 = 5.4200$. 

So, if we need first $K$ leading digits of $x$, we just need to multiply $B$ with $10^{K-1}$ and then throw away the fraction part. That is $res = \lfloor B \times 10^{K-1} \rfloor$. Why $10^{K-1}$ not just $10^K$? That's because we already have $1$ leading digit present in $10^q$ before shifting it.

Extending to Factorial

It is easy to extend the idea above to $N!$. First we need to find out the value of $y=log_{10}(N!)$.

$y= log_{10}(N!)$
$y= log_{10}(N \times (N-1) \times (N-2) \times...\times 3 \times 2 \times 1 )$
$y= log_{10}(N) + log_{10}(N-1) + log_{10}(N-2) + ... + log_{10}(2) + log_{10}(1) $

So we can simply find out the value of $y$ by running a loop from $1$ to $N$ and taking its log value.

After that we decompose $y$ into $p$, integer part and $q$, fraction part. The answer will be $\lfloor 10^q \times 10^{K-1}\rfloor$.

Code

const double eps = 1e-9;

/// Find the first K digits of N!
int leadingDigitFact ( int n, int k ) {
    double fact = 0;

    ///Find log(N!)
    for ( int i = 1; i <= n; i++ ) {
        fact += log10 ( i );
    }

    ///Find the value of q
    double q = fact - floor ( fact+eps );

    double B = pow ( 10, q );

    ///Shift decimal point k-1 times
    for ( int i = 0; i < k - 1; i++ ) {
        B *= 10;
    }

    ///Don't forget to floor it
    return floor(B+eps);
}
The code does exactly what we discussed before. But note the $eps$ that we added when flooring value in line $12$ and $22$. This due to precision error when dealing with real numbers in C++. For example, due to precision error sometimes a value which is supposed to be $1$, becomes $0.9999999999999$. The difference between these two values is very small, but if we floor them both, the first one becomes $1$ whereas the second one becomes $0$. So in order to avoid this error, when flooring a positive value we add a small number ( epsilon = $0.000000001$ ) to the number.

Summary

We need to execute the following steps to find the first $K$ leading digits of a number $x$ ( in our problem $x=N!$ ):
  1. Find the log value of the number whose leading digits we are seeking. $y=log_{10}(x)$.
  2. Decompose $y$ into two parts. Integer part $p$ and fraction part $q$.
  3. The answer is $\lfloor 10^q \times 10^{K-1} \rfloor$.

Resource

Wednesday, August 12, 2015

Number of Trailing Zeroes of Factorial

Problem

Given a positive integer $N$, find the number of trailing zero $N!$ has.

For example, $5! = 120$ has $1$ trailing zero and $10! = 3628800$ has $2$.

Brute Force Solution

Brute Force solution for this problem involves calculating the value of $N!$, which is overkill in this situation. Read the section on Brute Force on this article before you continue - [1] Number of Digits of Factorial

The brute force solution for this problem faces same limitation as [1]. We cannot calculate $N!$ for $N > 20$ using long long variable and using Big Integer is too slow, $O(N^2)$, if we assume number of digits in $N!$ increase by $1$ in each step.

Solution Using Factorization

Suppose a there is a number $x$, with $y$ trailing zeroes at its end, then can't we write that number as $x = z \times 10^y$?

For example, $x = 12300$ and it has $2$ trailing zeroes. Then we can write $x = 123 \times 10^2$.

Therefore, for every factor of $10$, $x$ gets a trailing zero. If we can find out number of $10$ in $N!$ then we can easily count its number of trailing zeroes.

How do we form a $10$? We form a $10$ by multiplying $2$ and $5$. So we need to find out how many $2$ and $5$ $N!$ has. This can be found using the idea for factorizing $N!$. The idea is discussed in [2]Prime Factorization of Factorial. We will be using $\text{factorialPrimePower}()$ function from that post.

So all we need to do is call $x = \text{factorialPrimePower}(2)$ and $y = \text{factorialPrimePower}(5)$ to find out the frequency of those primes. For every pair of $(2,5)$ we get one $10$ factor. So how many pairs can we make if we have $x$ number of $2$'s and $y$ number of $5$'s? We can make $MIN(x,y)$ pairs.

For example, for $10!$ we have $x = \frac{10}{2} + \frac{10}{4} + \frac{10}{8}= 5 + 2 + 1 = 8$ and $y =  \frac{10}{5} = 2$. Therefore number of trailing zero is $MIN(x,y) = MIN(8,2) = 2$.

Trailing Zeroes in Different Base

We solved the problem for decimal base. Now what if we want to know how many trailing zero does $N!$ has if we convert $N!$ to base $B$?

For example, how many trailing zero does $10!$ has in base $16$? In order to solve this, we need to know how number system works. Read the post [3]Introduction to Number System to learn more.

Previously we said that for decimal numbers, multiplying them with $10$ increase their trailing zeroes. Can something similar be said for the base $16$? Yes. Look at the following values:

$(1)_{16} = 1 \times 16^0$
$(10)_{16} = 1 \times 16^1$
$(100)_{16} = 1 \times 16^2$

Everytime we multiply a number with its base, all its digits shift a place to left and at the end a $0$ is added. So for a number in base $B$, if we multiply it by $B$ it gets a trailing zero at its end. 

So all we need to do is find out how many $B$ does $N!$ has in it. For base $16$, we need to find out how many times $16 = 2^4$ occurs in $N!$. For that we find out how many times $2$ occurs using $x = \text{factorialPrimePower}(2)$ and since we get $16$ for each $2^4$, we conclude that $N!$ has $\frac{x}{4}$ trailing zeroes.

This is extended for any base. Factorize the base $B$ and find out occurances of each prime. Then figure out how many combinations we can make.

For example if base is $60$, then $60 = 2^2 \times 3 \times 5$. So we find out $x = \text{factorialPrimePower}(2)$, $y = \text{factorialPrimePower}(3)$ and $z = \text{factorialPrimePower}(5)$. But then, we see that we need $2^2$ in $60$, so we can't use all the $x$ we calculated, we need to pair them. So it becomes $x = \frac{x}{2}$. Now, using $x,y,z$ we can make $60$ only $MIN(x,y,z)$ times. So this will be number of trailing zero.

Summary

We find number of trailing zero using the following steps:
  1. Factorize the base $B$
  2. If $B = p_1^{a_1} \times p_2^{a_2}... \times p_k^{a_k}$, then find out occurance of $x_i =  \text{factorialPrimePower} ( p_i)$.
  3. But we can't use $x_i$ directly. In order to create $B$ we will need to combine each $p_i$ into $p_i^{a_i}$. So we divide each $x_i$ by $a_i$.
  4. Number of trailing zero is $MIN(x_1,x_2,...,x_k)$.

Resources


  1. forthright48 - Number of Digits of Factorial
  2. forthright48 - Prime Factorization of Factorial
  3. forthright48 - Introduction to Number System 
  4. Wikipedia - Trailing Zero

Monday, August 10, 2015

Prime Factorization of Factorial

Problem

Given a positive integer $N$, find the prime factorization of $N!$.

For example, $5! = 5 \times 4 \times 3 \times 2 \times 1 = 120 =  2^3 \times 3 \times 5$.

Brute Force Solution

A possible solution is to calculate the value of $x = N!$ and then prime factorize $x$. But calculating $N!$ is tedious. We cannot fit $N!$ where $N > 20$ in a long long variable. We will need to use Big Integer class and that would make things slow. I will soon write a blog post on Big Integer, until then know that using Big Integer it would take more than $N^2$ steps to calculate $N!$.

Is there a better way?

Limits on Prime

Before we move on to the solution, let us first decide the limit on prime. In order to factorize $x = N!$, we have to generate prime numbers. But up to which value? Should we generate all primes less than $\sqrt{x}$? 

Even for a small value of $N$ like $100$, $x$ can be huge with over $100$ digits in it, thus, $\sqrt{x}$ will also be huge. Generating so many primes is not feasible. Using Sieve of Eratosthenes we could generate primes around $10^8$, which is nowhere near $\sqrt{100!}$.

Note that $N! = N \times (N-1) \times (N-2) \times ... \times 2 \times 1$. That is, $N!$ is a product of numbers less than $N$ only. Now, can there be any prime greater than $N$ that can divide $N!$? 

Suppose there is a number $A$ and we factorized it. It is trivial to realize that all its prime factors will be less than or equal to $A$. So in $N!$, which is the product of numbers less than $N$, if we decompose all those numbers to their prime factors, then they will reduce to primes less than or equal to $N$.

For example, $6! = 6 \times 5 \times 4 \times 3 \times 2 \times 1 = (2 \times 3) \times 5 \times 2^2  \times 3 \times 2 = 2^4 \times 3^2 \times 5$.

So the prime factors of $N!$ will be less than or equal to $N$. Generating primes till $\sqrt{N!}$ is not necessary. We just need to generate all primes less than or equal to $N$.

Prime Factorization

Now that we know the limit for primes, we are ready to begin factorizing the factorial. There is more than one way to achieve this. We will see three of them and discuss which one is best.

First - Linear Loop From $1$ to $N$

We know that $N! = N \times (N-1) \times (N-2) \times ... \times 2 \times 1$. So we could simply factorize every number from $1$ to $N$ and add to a global array that tracks the frequency of primes. Using the code for $factorize()$ from here, we could write a solution like below.
vector<int> prime;
int primeFactor[SIZE]; ///Size should be as big as N

void factorize( int n ) {
    int sqrtn = sqrt ( n );
    for ( int i = 0; i < prime.size() && prime[i] <= sqrtn; i++ ) {
        if ( n % prime[i] == 0 ) {
            while ( n % prime[i] == 0 ) {
                n /= prime[i];
                primeFactor[ prime[i] ]++; ///Increment global primeFactor array
            }
            sqrtn = sqrt ( n );
        }
    }
    if ( n != 1 ) {
        primeFactor[n]++;
    }
}

void factFactorize ( int n ) {
    for ( int i = 2; i <= n; i++ ) {
        factorize( i );
    }

    ///Now We can print the factorization
    for ( int i = 0; i < prime.size(); i++ ) {
        printf ( "%d^%d\n", prime[i], primeFactor[ prime[i] ] );
    }
}
We pass the value of $N$ to $factFactorize()$ in line $20$, and it calculates the frequency of each prime in $N!$. It starts a loop from $2$ to $N$ and factorizes each of them. In line $4$ we have the $factorize()$ function modified a bit in line $10$ and $16 $to suit our need. When those lines find a prime factor, they increase the frequency of that prime in the $primeFactor$ array.

It is simple and straight forward, but takes $O(N \times factorize() ) $ amount of time. We can do better.

Second - Summation of Each Prime Frequency

Instead of factorizing from $1$ to $N$, how about we just find out how many times each prime occurs in $N!$ and list them. If $p_1$ occurs $a_1$ times, $p_2$ occurs $a_2$ times $...$ $p_x$ occurs $a_x$ times, then $N! = p_1^{a_1} \times p_2^{a_2}\times ... \times p_x^{a_x}$.

That sounds nice, but how do we find the frequency of prime factors in $N!$. Let us just focus on one prime factor, for example $2$, and find out how many times it occurs in $N!$. We will extend this idea to other primes.

Let $N = 12$. How many times does $2$ occur in $12!$? We know that $12! = 12 \times 10 \times 9 \times ... \times 1$. How many numbers from $1$ to $12$ has $2$ as their prime factors? $\frac{12}{2} =  6$ numbers do and they are $\{ 2, 4, 6, 8, 10, 12 \}$. So we can say that at least $2^6$ is a factor of $12!$. But is there more?

Yes. Notice that $4 = 2^2$, so it has an extra $2$ in it that we did not count. That means for each number that has $2^2$ in them as a factor, we need to add $1$ to our result. How many number are there which has $2^2$ as their factor? $\frac{12}{4} = 3$ numbers, which are $\{ 4, 8, 12 \}$. So we increase our frequency to $6 + 3 = 9$ and say we have at least $2^9$ in $12!$. But is that it?

No. $8 =  2^3$ and for each number with $2^3$ as factor we add $1$ to result. So our result is $9 + \frac{12}{8} = 9 + 1 = 10$. 

Do we try with $16 = 2^4$ now? No. $12!$ cannot have any number with factor $2^4$ since $\frac{12}{16} = 0$. So we conclude that $12!$ has $2^{10}$ as its factor and no more.

Now, we extend this idea to other primes. What is the frequency of prime factor $3$ in $12!$? $\frac{12}{3} + \frac{12}{9} + \frac{12}{27} = 4 + 1 + 0 = 5$. We repeat this for all primes less than equal to $12$.

Therefore, we can say that for a given prime $p$, $N!$ will have $p^x$ as its prime factor where $x = \frac{N}{p} + \frac{N}{p^2} + \frac{N}{p^3} + ... \text{ Until it becomes 0 }$.

So, using this idea our code will look as the following.
void factFactorize ( int n ) {
    for ( int i = 0; i < prime.size() && prime[i] <= n; i++ ) {
        int p = prime[i];
        int freq = 0;

        while ( n / p ) {
            freq += n / p;
            p *= prime[i];
        }

        printf ( "%d^%d\n", prime[i], freq ); ///Printing prime^freq which is factor of N!
    }
}
This code factorizes $N!$ as long as we can generate all primes less than or equal to $N!$. The loop in line $6$ runs until $\frac{n}{p}$ becomes 0.

This code has 3 advantages over the "First" code. 
  1. We don't have to write $factorize()$ code.
  2. Using this code, we can find how many times a specific prime $p$ occurs in $N!$ in $O(log_p (N))$ time. In the "First" code, we will need to run $O(N)$ loop and add occurrences of $p$ in each number.
  3. It has a better complexity for Factorization. Assuming the loop in line $6$ runs $log_2 (N)$ times, this code has a complexity of $O(N log_2 (N))$. The code runs faster than this since we only loop over primes less than $N$ and at each prime the loop runs only $O(log_p (N))$ times. The "First" code ran with $O(N \times factorize() )$ complexity, where $factorize()$ has complexity of $O(\frac{ \sqrt{N} }{ ln ( \sqrt{N} ) } + log_2(N) \: )$.  
This idea still has a small flaw. So the next one is better than this one.

Three - Better Code than Two

Suppose, we want to find out how many times $1009$ occurs in $9 \times 10^{18}!$. Let us modify the "Second" code to write another function that will count the result for us.
long long factorialPrimePower ( long long n, long long p ) {
    long long freq = 0;
    long long cur = p;

    while ( n / cur ) {
        freq += n / cur;
        cur *= p;
    }

    return freq;
}
If we pass $n = 9 \times 10^{18}$ and $p = 1009$, it will return us $8928571428571439$. But this is wrong. The line $7$ in the code overflows resulting in wrong answer. Try it yourself. Print out the value of $cur$ in each step and see when it overflows.

We could change the condition in line $5$ into something like this to solve the situation:
while ( n / cur > 0 )
But this remedy won't work if $cur \times p$ overflows into a positive number. If we want we could use techniques that avoids multiplying two numbers if it crosses a limit, but there is an simpler way.

Note that $\frac{N}{p^3}$ is same as $\frac{ \frac{N}{p^2} }{p}$. So instead of saying that $res = \frac{N}{p} + \frac{N}{p^2}...$, we could rewrite it as

$x = N: \: res = res + \frac{x}{p}$.
$x = \frac{N}{p} = \frac{x}{p}: \: res = res + \frac{x}{p}$.
$x = \frac{N}{p^2} = \frac{ \frac{N}{p} } {p} = \frac{x}{p}: \: res = res + \frac{x}{p}$.
$...$
$x = 0$

Instead of raising the power of $p$, we divide the value of $N$ by $p$ at each step. This has the same effect.

So our code for finding frequency of specific prime should look like following:
long long factorialPrimePower ( long long n, long long p ) {
    long long freq = 0;
    long long x = n;

    while ( x ) {
        freq += x / p;
        x = x / p;
    }

    return freq;
}
There might still be inputs for which this code will overflow, but chances for that is now lower.. Now if we send in $n = 9 \times 10^{18}$ and $p = 1009$, then this time we get $8928571428571425$ as our result.

If we apply this improvement in our $factFactorize()$ function, then it will become:
void factFactorize ( int n ) {
    for ( int i = 0; i &lt; prime.size() &amp;&amp; prime[i] &lt;= n; i++ ) {
        int x = n;
        int freq = 0;

        while ( x / prime[i] ) {
            freq += x / prime[i];
            x = x / prime[i];
        }

        printf ( "%d^%d\n", prime[i], freq );
    }
}
This code has less chance to overflow so it is better.

Resource

Saturday, August 8, 2015

Number of Digits of Factorial

Problem

Given an integer $N$, find number of digits in $N!$.

For example, for $N=3$, number of digits in $N! = 3! = 3\times 2\times 1 = 6$ is $1$. For $N=5$, number of digits in $5! = 120$ is $3$.

Brute Force Solution

The first solution that pops into mind is to calculate $N!$ and count how many digits it has. A possible solution will look like the following:
int factorialDigit ( int n ) {
    long long fact = 1;
    for ( int i = 2; i <= n; i++ ) {
        fact *= i;
    }

    int res = 0; ///Number of digit of n!
    while ( fact ) { /// Loop until fact becomes 0
        res++;
        fact /= 10; ///Remove last digit
    }

    return res;
}
This code works, but only for $N \leq 20$. Once $N$ crosses $20$, it no longer fits in a "long long" variable.

Since factorial of $N > 20$ overflows $long\: long$, how about we use "Big Integer" to store the value?

Brute Force Solution with Big Integer

If you don't know what Big Integer is, then know that it is a class for arbitrary large integer. C++ does not support this class so we will have to manually implement it if we want to use C++ to solve problems involving large integers. Or you could use a programming language that supports this class, such as Java.

I will write a post on Big Integer someday, but for now you could just use the Big Int class written by +Anudeep Nekkanti  from here

Multiplication of a Big Integer and an integer takes $O(\text{number of digits of Big Integer})$. Assuming that when calculating $N!$, the number of digits of $N!$ increases by $1$ at each step, it will take $O(N^2)$ time to compute $N!$. But obviously $N!$ does not increase by $1$ digit at each step ( for e.g, multiply by $100$ increases it by $2$ digits ), so worst time complexity is worse than $O(N^2)$.

Solution Using Logarithm

Logarithm of a number is connected to its number of digits, which might not be apparent. What is logarithm? Logarithm of a number $x$, in base $b$, is a real number $y$ such that $x = b^y$.  For example: 
$$log_{10}1234 = 3.0913151597 \text{ and } 10^{3.0913151597} = 1234$$
In logarithms, base of the number is important. Since we want number of digits of $N!$ in decimal, we will work with base $10$.

Number of Digit of an Integer

First, we will apply the logarithm idea on an integer. So where is the connection of logarithm with digit number? Look at the following logarithm values:

$log_{10}(x) = y$
$log_{10}(1) = 0$
$log_{10}(10) = 1$
$log_{10}(100 )= 2$
$log_{10}(1000) = 3$
$log_{10}(10000) = 4$

As the value of $x$ increases, value of $y$ also increases. Every time we multiple $x$ by $10$, value of $y$ increases by $1$. That is, every time number of digit increases, value of $y$ increases. From this table, we can infer few things about log of other values.

If $log_{10}(100)$ is 2, and $log_{10}(1000)$ is 3, then for all value of $x$ where $100 < x < 1000$ , value of $y$ will lie between $2 < y < 3$. Let us try this out.

$log_{10}(100) = 2$
$log_{10}(150) = 2.17609125906$
$log_{10}(500) = 2.69897000434$
$log_{10}(999) = 2.99956548823$

Now note that, for every $100 \leq x < 1000$, value of $y$ is $2 \leq y < 3$. Can you see some relation between value of $y$ and number of digits of $x$?

Yes. If the value of $y$ is of form $2.XXX$, then $x$ has $2+1=3$ digits.  
$$\therefore \text{number of digits of}\: x = \lfloor log_{10} (x) \rfloor + 1$$
Be careful, $\lfloor log_{10} (x) \rfloor + 1$ is not same as $\lfloor log_{10} (x) + 1  \rfloor$. Try it out with $100,1000,10000$. We need to floor the log value before we add 1. 
int numberDigit ( int n ) {
    int wrongAnswer = log10(n) + 1; ///This is wrong.
    int rightAnswer = ( (int) log10(n) ) + 1; ///This is right.
    return rightAnswer;
}
In line $3$, we type cast $log10(n)$ to integer. This has same action as $floor()$ function. Also note that we used $log10()$ function instead of $log()$ function. Unlike our calculators, in C++ $log()$ has base $2$.

Extending To Factorial

So how do we extend this idea to $N!$?

Let $x = log_{10}(N!)$. Then our answer will be $res = \lfloor x \rfloor + 1$. So all we need to do is find value of $x$.

$x = log_{10}(N!)$
$x = log_{10}(1 \times 2 \times 3 \times ... \times N)$
$\therefore x = log_{10}(1) + log_{10}(2) + log_{10}(3) + ... + log_{10}(N)$ This is using the law $ log_{10}(ab) = log_{10}(a)+log_{10}(b) $

So in order to calculate $x = log_{10}(N!)$, we don't have to calculate value of $N!$. We can simply add log value of all numbers from $1$ to $N$. This can be achieved in $O(N)$.
int factorialDigit ( int n ) {
    double x = 0;
    for ( int i = 1; i <= n; i++ ) {
        x += log10 ( i );
    }
    int res = ( (int) x ) + 1;
    return res;
}

Digits of $N!$ in Different Base

Now what if we want to find how many digits $N!$ has if we convert $N!$ to some other base. 

For example, how many digits $3!$ has in binary number system with base $2$? We know that $(6)_{10} = (110)_2$. So $3!$ has $3$ digits in base $2$ number system.

Can we use logarithms to solve this problem too? Yes. 
$$\text{number of digits of x in base B} = log_B(x)$$
All we need to do is change the base of our $log$ and it will find number of digits in that base.

But, how do we change base in our code? We can only use log with base $2$ and $10$ in C++. Fear not, we can use the following law to change base of logartihm from $B$ to $C$.
$$log_B(x) = \frac{log_C(x)}{log_C(B)}$$
So in C++, we will use $C = 2$ or $C=10$ to find value of $log_B(x)$.
int factorialDigitExtended ( int n, int base ) {
    double x = 0;
    for ( int i = 1; i <= n; i++ ) {
        x += log10 ( i ) / log10(base); ///Base Conversion
    }
    int res = ( (int) x ) + 1;
    return res;
}

Tuesday, August 4, 2015

UVa 10407 - Simple division

Problem 


Given an array of numbers, find the largest number $d$ such that, when elements of the array are divided by $d$, they leave the same remainder

Solution

We will be using our knowledge about Congruence Relation and GCD to solve this problem. But first, we need to make sure that we understand the problem properly.

Wrong Approach

At first, one might think that $d$ is the $gcd$ of array elements. Surely, dividing elements of the array with $d$ will leave same remainder $0$, but that doesn't necessarily mean it is the largest possible answer. 

For example, suppose the array is $A = \{2,8,14,26\}$. Then what will be the $gcd$ of array A? $gcd(2,8,14,26) = 2$. Dividing elements of $A$ with $2$ leaves us $0$. So is this our answer? No.

For array $A$, there exists a number greater than $2$ that leaves the same remainder. And that number is $3$. When we divide each element with $3$, it leaves us $2$ as the remainder.

The problem did not ask us to find the number that will leave $0$ as remainder. It asked us to find the largest number $d$ that leaves the same remainder. So for array $A$ the $gcd(A)$ is not the answer. Is $3$ our answer? No. The answer is given below in Example.

Using Congruence Relation 

Let us rephrase the problem using congruence relation. Suppose there is an array with elements, $A=\{a,b,c\}$. We need to find the largest number $d$ that leaves the same remainder for each of its element.

Let us consider only $\{a,b\}$ for now. We need to find $d$ such that it leaves the same remainder when it divides $a$ and $b$. Meaning
$$a \: \equiv \: b \: \text{(mod d)} \\ a - b \: \equiv \: 0 \: \text{(mod d)}$$
What does this mean? It means, if $d$ leaves the same remainder when it divides $a$ and $b$, then it will leave no remainder when dividing $a-b$.

Using same logic, we can say that $b - c \: \equiv \: 0 \: \text{(mod d)}$. Therefore, if we find a number that divides $a-b$ and $b-c$,  then that number will leave the same remainder when dividing $\{a,b,c\}$. 

This idea can be extended for any number of elements in the array. So $d$ is such a number that leaves no remainder when it divides the difference of adjacent elements in the array.

But wait, there are multiple numbers that can divide the difference of adjacent elements. Which one should we take? Since we want the largest value of $d$, we will take the largest divisor that can divide all the difference of adjacent numbers. There is only one divisor that can divide all the adjacent differences, and that is $gcd( (a-b), (b-c) )$.

Therefore, if we have an array $A=\{a_1,a_2,a_3...a_n\}$, then $d = gcd( a_2 - a_1, a_3- a_2,...a_n - a_{n_1})$.

Careful about negative value of $gcd()$. Make sure you take the absolute value of $gcd()$.

Example

Let us get back to the example we were looking into before. Finding $d$ for the array $A = \{2,8,14,26\}$. We know that $d$ will divide the difference of adjacent elements completely. So let us make another array that will contain the difference of adjacent elements., $B = \{8-2, 14-8, 26-14\} = \{6,6,12\}$. Therefore, $d = gcd ( 6,6,12 ) = 6$.

Summary

  1. Let the array of elements be $A = \{a,b,c,d...\}$.
  2. $res \neq gcd(a,b,c,d...)$.
  3. $res = gcd ( a-b,b-c,c-d,...)$.

Code

#include <bits/stdc++.h>
using namespace std;

typedef long long vlong;

vlong gcd ( vlong a, vlong b ) {
    while ( b ) {
        a = a % b;
        swap ( a, b );
    }
    return a;
}

vlong arr[1010];

int main () {

    while ( scanf ( "%d", &arr[0] ) != EOF ) {
        if ( arr[0] == 0 ) break; //End of test case

        //A new test case has started
        int cur = 1;

        //Take input
        while ( 1 ) {
            scanf ( "%lld", &arr[cur] );
            if ( arr[cur] == 0 ) break;
            else cur++;
        }

        vlong g = 0; //Start with 0 since gcd(0,x) = x.
        for ( int i = 1; i < cur; i++ ) {
            int dif = arr[i] - arr[i-1]; //Calculate difference
            g = gcd ( g, dif ); //Find gcd() of differences
        }

        if ( g < 0 ) g *= -1; //In case gcd() comes out negative
        printf ( "%lld\n", g );
    }

    return 0;
}

Monday, August 3, 2015

UVa 11388 - GCD LCM

Problem

Problem Link - UVa 11388 - GCD LCM

Given two positive integers $(G, L)$, we have to find a pair of integers $(a,b)$ such that $gcd(a, b)=G$ and $\text{lcm}(a, b)=L$. If there are multiple such pairs, we have to find the pair where $a$ is minimum. Also, both $a$ and $b$ needs to be positive.

Solution

We don't know the value of $(a,b)$ yet. Here is how I approached the problem.

Value of $a$

What we know that $gcd(a,b) = G$. So, $G$ divides $a$ and $b$. Therefore, $a$ is a multiple of $G$. We also need to make sure that $a$ is as small as possible. So, what is the smallest positive number that can be divided by $G$? The answer is $G$ itself.
$$\therefore a = G$$

Existence of Solution

Next, we know that $lcm(a,b)$ is the smallest positive number which is divisible by both $a$ and $b$. Since $a=G$ and $a \: | \: lcm(a,b)$, it follows that $a = G$ should divide $lcm(a,b) = L$. If $L$ is not divisible by $G$, then no solution exists. 
$$\therefore \text{if } (G \not| L), \text{no solution exists}$$

Value of $b$

The value of $b$ can be derived in the following way. We know that:

$gcd(a,b) \times lcm(a,b) = a \times b$
$G \times L = G \times b$
$b = \frac{G\times L}{G}$
$\therefore b = L$.

Summary 

  1. $a = G$
  2. If $G \not| L$, no solution exists
  3. $b = L$
  4. $\therefore (a,b) = (G,L)$

Code

Here is the code in C++
#include <bits/stdc++.h>

int main () {
    int kase;
    scanf ( "%d", &kase ); //Input number of case

    while ( kase-- ) {
        int G, L;
        scanf ( "%d %d", &G, &L ); //Take input

        int a, b; //Need to find their value
        
        a = G;
        
        if ( L % G != 0 ) {
            printf ( "-1\n" ); //No Solution
            continue;
        }
        
        b = L;
        
        printf ( "%d %d\n", a, b );
    }

    return 0;
}

Saturday, August 1, 2015

Introduction to Number Systems

Number System is a consistent way of expressing a number. It consists of three parts:
  1. The Symbol - It can be digits, letters or any other symbol.
  2. The Position - The position of symbols determine their weight.
  3. The Base - The total number of different symbols supported.
Number system is often named after the size of its base. For example, the "Decimal Number System", with symbols $(0,1,2,3,4,5,6,7,8,9)$. Since there are $10$ different symbols, the base is $10$.

The Decimal Number System is the standard number system that we use in our everyday life. We will use this system as our reference for other systems. Since we will be looking into lots of different number systems in this article, from now on I will write numbers in decimal number system as $(number)_{10}$. In general, $(x)_{y}$ means $x$ is a number with base $y$.

How Number System Works

As mentioned before, Number System consists of three parts.

Symbols in Number System

In decimal number system, the first few numbers we encounter are $0, 1, 2, 3, 4, 5, 6, 7, 8, 9$. Then we run out of symbols. We add a new position at the front of the number and start placing number in those to continue counting: $8, 9, 10, 11, 12, 13...$

The same concept is used with number system of different base. For example, let us consider a number system with 3 different symbols: $(0,1,2)$. The first few numbers in this system are: $0,1,2$. Once we run out of symbols we introduce new positions and get: $10,11,12,20,21,22,100,101,102,110$.

Position of Symbol

In a number, the position of symbols is $0$ indexed and ranked from right to left. For example, the number $54711$ will have the following index for its positions:
Index$4$$3$$2$$1$$0$
Symbols$5$$4$$7$$1$$1$
The last digit of the number has position index $0$. The index increases as we go left.

The position in a number represents the weight of the symbol in that position. A symbol in a higher position values more than one in a lower position. For example in the decimal number system, we know that $2 > 1$. But if we consider $1$ in a higher position then $100 > 20$. How is the weight of each position calculated exactly? The answer is in next section.

Base of Number System

The "Base" of number system determine the weight of each position.

For example, $(1234)_{10} = 1000 + 200 + 30 + 4 = 1 \times 10^3 + 2 \times 10^2 + 3 \times 10^1 + 4 \times 10^0$. 

Here the weight of the symbol in $0_{th}$ position is $10^0$, $1_{st}$ position is $10^1$, $2_{nd}$ position is $10^2$ and $3_{rd}$ position is $10^3$. As the position increases, the weight of position increases by $10$. A symbol in $n_{th}$ position carries $10^n$ weight.

But this is true only for the decimal number system. The weight is a power of $10$ for decimal system cause it has a base of $10$. A number system with base $B$ will have weight $B^n$ for $n_{th}$ position.

Binary Number System

Binary Number System is a Number System with Base $2$. It has only two symbols: $(0,1)$. This number system is used by computers to store information. As programmers, we will frequently work with binary numbers, so we need to understand this system properly.

Since we are used to the Decimal Number System, it might be awkward to work with the binary system at first. But we will get used to it pretty soon.

Here are the first few numbers in Binary System.
Decimal$0$$1$$2$$3$$4$$5$$6$$7$$8$$9$
Binary$0$$1$$10$$11$$100$$101$$110$$111$$1000$$1001$

Binary to Decimal

We will first look into how we can convert a number from binary to the decimal system.

Suppose we want to find the value of $(11001)_2$ in decimal. How do we find it? Remember that we learned that a symbol in $n_{th}$ position has $Base^n$ weight. What is the base in the binary system? $2$. So this number can be written as:

$(11001)_2 = 1\times 2^4 + 1 \times 2^3 + 0 \times 2^2 + 0\times 2^1 + 1 \times 2^0 = 16 + 8 + 0 + 0 + 1 = (25)_{10}$

Decimal to Binary

Solve $(19)_{10} = (?)_2$. We need to convert $19$ to binary.

A number $x$ in binary system is of form: $x = s_n2^n +...+s_32^3+s_22^2+s_12^1+s_02^0$, where $s_i$ is the symbol in $i_{th}$ position. What happens when we find $x \: \% \: 2$? Since all position except for $0_{th}$ position is multiple of $2$, $x \: \% \: 2 = s_0$.

That is, finding the value of $x \: \% \: 2$ gives us the last digit of $x$ in the binary system. 

Okay, we got the digit in $0_{th}$ position, how do we find the rest? In order to find the digit in next position, we divide $x$ by $2$. What happens if we divide $x$ by $2$? 

$\frac{x}{2} = s_n2^{n-1} +...+s_32^2+s_22^1+s_12^0+\frac{s_0}{2}$
$\therefore \lfloor \frac{x}{2} \rfloor = s_n2^{n-1} +...+s_32^2+s_22^1+s_12^0$

That is, when we divide $x$ by $2$, the last digit of $x$ disappears and all digits of $x$ shifts one place to right. For example, if $x$ is $(11011)_2$ then $\frac{x}{2}$ is $(1101)_2$.

In C++, we can easily perform this floor division by simply performing integer division. Once we divide it, we then find the value of $\lfloor \frac{x}{2} \rfloor \: \% \: 2$. By repeating this until $x$ becomes $0$, we can find all digits.

Let us try to solve the problem $(19)_{10} = (?)_2$.

$x=19: x \: \% \: 2 = 1$
$x = \lfloor \frac{19}{2} \rfloor = 9: x \: \% \: 2 = 1$
$x = \lfloor \frac{9}{2} \rfloor = 4: x \: \% \: 2 = 0$
$x = \lfloor \frac{4}{2} \rfloor = 2: x \: \% \: 2 = 0$
$x = \lfloor \frac{2}{2} \rfloor = 1: x \: \% \: 2 = 1$
$x = \lfloor \frac{1}{2} \rfloor = 0: \text{end}$
$\therefore (19)_{10} = (10011)_2$

Hexadecimal Number System

Hexadecimal Number System is a Number System with Base $16$. It has 16 symbols, $(0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F)$. We already saw Decimal to Binary conversion and vice versa. Looking into Decimal-Hexadecimal conversion and vice verse will help us understand the base conversions better.

Here are the first few numbers in Hexadecimal System.

$Decimal$$0$$1$$2$$3$$4$$5$$6$$7$$8$$9$
$Hexadecimal$$0$$1$$2$$3$$4$$5$$6$$7$$8$$9$
$Decimal$$10$$11$$12$$13$$14$$15$$16$$17$$18$$19$
$Hexadecimal$$A$$B$$C$$D$$E$$F$$10$$11$$12$$13$

In hexadecimal system, $(A,B,C,D,E,F)$ corresponds to the decimal value $(10,11,12,13,14,15)$.

Hexadecimal to Decimal

Suppose we want to find the value of $(1A3F2B)_{16}$ in decimal. Using the same logic as Binary to Decimal conversion, we can say that:

$(1A3F2B)_{16} = 1\times 16^5 + A \times 16^4 + 3 \times 16^3 + F\times 16^2 + 2 \times 16^1 + B \times 16^0$
$ (1A3F2B)_{16} = 1\times 16^5 + (10) \times 16^4 + 3 \times 16^3 + (15)\times 16^2 + 2 \times 16^1 + (11) \times 16^0$
$ (1A3F2B)_{16} = 1048576 + 10 \times 65536 + 3 \times 4096 + 15 \times 256 + 2 \times 16 + 11 \times 1$
$ (1A3F2B)_{16} = 1048576 + 655360 + 12288 + 3840 + 32 + 11$
$ (1A3F2B)_{16} = (1720107)_{10}$

Decimal to Hexadecimal

Solve $(123456789)_{10} = (?)_{16}$.

A number $x$ in hexadecimal system is of form: $x = s_n16^n +...+s_316^3+s_216^2+s_116^1+s_016^0$, where $s_i$ is the symbol in $i_{th}$ position. 

Just like Binary System, finding the value of $x \: \% \: 16$ gives us the last digit of $x$ in hexadecimal system. To find the rest, we divide it by $16$ and find the last digit again.

$\frac{x}{16} = s_n16^{n-1} +...+s_316^2+s_216^1+s_116^0+\frac{s_0}{16}$
$\therefore \lfloor \frac{x}{16} \rfloor = s_n16^{n-1} +...+s_316^2+s_216^1+s_116^0$

Let us try to solve the problem $(123456789)_{10} = (?)_{16}$.

$x=123456789: x \: \% \: 16 = 5$
$x = \lfloor \frac{123456789}{16} \rfloor = 7716049: x \: \% \: 16 = 1$
$x = \lfloor \frac{7716049}{16} \rfloor = 482253: x \: \% \: 16 = 13$
$x = \lfloor \frac{482253}{16} \rfloor = 30140: x \: \% \: 16 = 12$
$x = \lfloor \frac{30140}{16} \rfloor = 1883: x \: \% \: 16 = 11$
$x = \lfloor \frac{1883}{16} \rfloor = 117: x \: \% \: 16 = 5 $
$x = \lfloor \frac{117}{16} \rfloor = 7: x \: \% \: 16 = 7 $
$x = \lfloor \frac{7}{16} \rfloor = 0: \text{end} $
$\therefore (123456789)_{10} = (75BCD15)_{16}$

Notice how we replaced the remainder value $11,12,13$ with $B,C,D$. Since the value $(10,11,12,13,14,15)$ represents $(A,B,C,D,E,F)$ in hexadecimal, we use the proper symbols in the number.

Number System with Base $B$

We saw how to work with Binary and Hexadecimal number system. We will now generalize the approach.

Suppose there is number $x$ in base$B$, then $x = d_nd_{n-1}..d_3d_2d_1d_0$, where $0 \leq d_i < B$. 

Base to Decimal

The number $x$ in decimal will be $d_n \times B^n + d_{n-1} \times B^{n-1} + ... + d_1 \times B^1 + d_0 \times B^0$.

Here is a code that can convert a number in base $B$ into decimal.
int baseToDecimal ( string x, int base ) {
    int res = 0;
    int len = x.length();

    int coef = 1; ///initially base^0
    for ( int i = len - 1; i >= 0; i-- ) { ///Start from reverse
        res += (x[i]-'0') * coef;
        coef *= base; //increase power of base
    }
    return res;
}
The number $x$ here is a string. That is because $x$ can have symbols that are not digits, for example, $(AB12)_{16}$. We find out the length of the number in line $3$. Then we iterate over the number from last digit to the first digit. We start from back where the coefficient of the digit is $base^0 = 1$. Every time we change position, we multiply the coefficient by $base$. This ensures we multiply $d_n$ with $base^n$.

If we want we can convert the number to decimal from the front of the number, that is from first digit to last digit.

Suppose we want to make the number $d_1d_2d_3$ in base $B$. We can start with the digit $d_1$ only. Then we can move it to left of its position by multiply it by $B$, $d_1B^0 \times B = d_1B^1$. Then if we add $d_2$ with it, it becomes $d_1B^1 + d_2 = d_1d_2$. We multiply it by $B$ again and then add $d_3$. It finally becomes $d_1d_2d_3$.

For example, $(123)_{10} = (1 \times 10 + 2 ) \times 10 + 3$.

Using this idea, we can write the following code:
int baseToDecimalAlternate ( string x, int base ) {
    int res = 0;
    int len = x.length();

    for ( int i = 0; i < len; i++ ) {
        res = ( res * base ) + (x[i]-'0');
    }
    return res;
}
Here we iterate the number $x$ from $0$ to $len-1$ in line $5$. Line $6$ is what I explained above.

The alternate method is shorter and sometimes makes a problem easier to solve. Knowing both method helps us approach problems from different directions.

Decimal To Base

$x \: \% \: B$ will give us the last digit of $x$ in base $B$. We can get the remaining digits by repeatedly dividing $x$ by $B$ and taking its last digit until $x$ becomes 0.

$x = x: d_0 = x \: \% \: B$
$x = \lfloor \frac{x}{B} \rfloor: d_1 = x \: \% \: B$
$...$

Here is how we will do it in code:
//A list of symbol. Depending on base and number system, this list can be different.
char symbol[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
string decimalToBase ( int x, int base ) {
    string res = "";

    while ( x ) {
        int r = x % base; //Find the last digit
        res = res + symbol[r];//Change the integer value to symbol and append to res
        x /= base;//Remove the last digit
    }
    if ( res == "" ) res = symbol[0];//If res is empty, that means x is 0.
    reverse ( res.begin(), res.end());//We found the digits in reverse order.
    return res;
}
In line $2$ we have a symbol list. Since we are converting to a different base, it will have symbols for each value from $0$ to $B-1$. We keep on finding the last digit of $x$ until it becomes $0$ in the loop at line $6$. The last digit is found in line $7$. We append it to the result in line $8$ and we divide $x$ by $B$ in line $9$. In line $11$ we check if $res$ is empty. If it is then $x$ was $0$ from the beginning, so we append $0$ to $res$. In line $12$ we reverse the whole string $res$ since we generated the digits in reverse order, i.e, from last digit to the first digit.

Resource