Thursday, 1 August 2013

Let's Learn Java.

from 20Oct finally I would be learning Java for one week :)

int [] a = new int [10];
Arrays.sort (a);
Arrays.fill (a, 0);
Arrays.fill (a, startRange, endRange, value);
Arrays.sort (a, startIndex, endIndex);
String [] name = String[] {"Praveen", "Dhinwa"};

Queue <Integer> q = new LinkedList <Integer> ();
q.add();
q.remove();

String s;
s.substr(startIndex, length)

String a[] = s.split(token);

String s;
Integer.parseInt (s);




Sunday, 23 June 2013

Digit Dynamic Programming problem

We will solve a DP problem today.

dp state is (index, tight).
where index is the position where we currently are (consider the process of constructing solution from left to right).
where tight is a variable which states the number which we have constructed up to now, is strictly less/greater or equal. Tight is false, means it has become less or greater. but true when equal.

Ex. 1. http://www.spoj.com/problems/SQAMOD/
find no of binary numbers less than a given n digit number S which have sum of squares of digits modulo 3 to be zero,
Solution:
1. dp (index, int sumModulo3, int tight):
      So let us see the transitions to different states. You can understand that by reading the code.

700 problems to understand you complete algorithmic programming.

1. Segment Tree:

To Read :
http://www.topcoder.com/tc?d1=tutorials&d2=lowestCommonAncestor&module=Static
http://ronzii.wordpress.com/2011/07/08/segment-tree-tutorial/
http://se7so.blogspot.in/2012/12/segment-trees-and-lazy-propagation.html
http://olympiad.cs.uct.ac.za/presentations/camp3_2007/interval_trees.pdf
http://codeforces.com/blog/entry/6281
http://apps.topcoder.com/forums/?module=Thread&threadID=651820&start=0&mc=2#1146133
http://www.algorithmist.com/index.php/Segmented_Trees
http://letuskode.blogspot.in/2013/01/segtrees.html
http://wcipeg.com/wiki/Heavy-light_decomposition
http://discuss.codechef.com/questions/5960/rnestescape-from-the-mines
http://ideone.com/dPS5N (Heavy Light implementation).
https://sites.google.com/site/indy256/algo/heavy_light (Heavy Light implementation).

Problems:

http://www.spoj.com/problems/GSS1
http://www.spoj.com/problems/GSS2
http://www.spoj.com/problems/GSS3
http://www.spoj.com/problems/GSS4
http://www.spoj.com/problems/GSS5
http://www.spoj.com/problems/GSS6
http://www.spoj.com/problems/GSS7
http://www.spoj.com/problems/ANDROUND/
http://www.spoj.com/problems/BRCKTS/
http://www.spoj.com/problems/DQUERY/
http://www.spoj.com/problems/FREQUENT/
http://www.spoj.com/problems/HEAPULM/
http://www.spoj.com/problems/HELPR2D2/
http://www.spoj.com/problems/KGSS/
http://www.spoj.com/problems/MKTHNUM/
http://www.spoj.com/problems/NICEDAY/
http://www.spoj.com/problems/YODANESS/
http://www.spoj.pl/problems/INCSEQ/
http://www.spoj.pl/problems/INCDSEQ/
http://www.spoj.pl/problems/KQUERY/
http://www.spoj.pl/problems/QTREE/
http://www.spoj.pl/problems/QTREE2/
http://www.spoj.pl/problems/QTREE3/
http://www.spoj.com/problems/QTREE4/
http://www.spoj.com/problems/QTREE5/
http://www.spoj.pl/problems/CTRICK/
http://www.spoj.pl/problems/MATSUM/
http://www.spoj.pl/problems/RATING/
http://www.spoj.pl/problems/RRSCHED/
http://www.spoj.pl/problems/SUPPER/
http://www.spoj.pl/problems/ORDERS/
http://www.spoj.com/problems/MULTQ3/
http://www.spoj.com/problems/RPAR/
http://www.spoj.com/problems/PATULJCI/
http://www.spoj.com/problems/DISUBSTR/
http://www.spoj.com/problems/HORRIBLE
http://www.spoj.pl/problems/IOPC1207/
http://www.spoj.com/problems/SEGSQRSS/
http://www.spoj.com/problems/ORDERSET/
http://www.spoj.com/problems/HELPR2D2/
http://www.spoj.com/problems/TEMPLEQ
http://www.codechef.com/problems/QTREE
http://www.codechef.com/problems/LEBOBBLE
http://www.codechef.com/problems/DGCD
http://www.codechef.com/problems/QUERY
http://codeforces.com/problemset/problem/280/D
http://codeforces.com/problemset/problem/117/E
http://codeforces.com/problemset/problem/167/D
http://codeforces.com/problemset/problem/266/E
http://codeforces.com/problemset/problem/145/E
http://codeforces.com/problemset/problem/226/E
http://codeforces.com/problemset/problem/311/C
http://codeforces.com/problemset/problem/276/E
http://codeforces.com/problemset/problem/221/D
http://codeforces.com/problemset/problem/174/C
http://codeforces.com/problemset/problem/301/D
http://codeforces.com/problemset/problem/61/E
http://codeforces.com/problemset/problem/103/D
http://codeforces.com/problemset/problem/165/D
http://codeforces.com/problemset/problem/52/C
http://codeforces.com/problemset/problem/85/D
http://codeforces.com/problemset/problem/242/E
http://codeforces.com/problemset/problem/111/B
http://codeforces.com/problemset/problem/220/B
http://codeforces.com/problemset/problem/195/E
http://codeforces.com/problemset/problem/219/E
http://codeforces.com/problemset/problem/281/D
http://codeforces.com/problemset/problem/121/E
http://codeforces.com/problemset/problem/86/D
http://codeforces.com/problemset/problem/182/C
http://codeforces.com/problemset/problem/19/D
http://codeforces.com/problemset/problem/258/E
http://codeforces.com/problemset/problem/190/E
http://codeforces.com/problemset/problem/295/E
http://codeforces.com/problemset/problem/160/E
http://codeforces.com/problemset/problem/163/E
http://codeforces.com/problemset/problem/192/E
http://codeforces.com/problemset/problem/316/E3
http://codeforces.com/problemset/problem/280/E
http://codeforces.com/problemset/problem/238/D

SRM 310 -> Floating Median
http://acm.pku.edu.cn/JudgeOnline/problem?id=1986
http://acm.pku.edu.cn/JudgeOnline/problem?id=2374
http://acmicpc-live-archive.uva.es/nuevoportal/data/problem.php?p=2045
http://acm.pku.edu.cn/JudgeOnline/problem?id=2763
http://www.spoj.pl/problems/QTREE2/
http://acm.uva.es/p/v109/10938.html
http://acm.sgu.ru/problem.php?contest=0&problem=155

2. BIT (also called Fenwick Tree).
Mostly problems of BIT can also be solved by Segment Tree. But it is shorted and faster to code. Hence it is sometimes very easy.

To Read:

http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees
http://p--np.blogspot.in/2011/04/binary-indexed-tree.html
http://www.algorithmist.com/index.php/Fenwick_tree
http://en.wikipedia.org/wiki/Fenwick_tree
http://pavelsimo.blogspot.in/2012/09/counting-inversions-in-array-using-BIT.html
http://petr-mitrichev.blogspot.in/2013/05/fenwick-tree-range-updates.html

Problems:
http://community.topcoder.com/stat?c=problem_statement&pm=10976
And try some earlier problems which are solvable by this data structure to practice more.
Try mobile problem of IOI. 2D BIT.

3. Dynamic Programming (DP):

To Read:

http://en.wikipedia.org/wiki/Edit_distance
http://www.codechef.com/wiki/tutorial-dynamic-programming

Problems:

SGU Problems :  269273304317356396445447458489494
http://www.spoj.com/problems/SAMER08D/
http://acm.sgu.ru/problem.php?contest=0&problem=199
http://www.spoj.com/problems/MDOLLS/
http://www.spoj.com/problems/MSTICK/
http://www.spoj.com/problems/MCARDS/
http://www.spoj.com/problems/MIXTURES/
http://www.spoj.com/problems/SCUBADIV/
http://z-trening.com/tasks.php?show_task=5000000355
http://z-trening.com/tasks.php?show_task=5000000286
http://z-trening.com/tasks.php?show_task=5000000465
http://z-trening.com/tasks.php?show_task=5000000310
http://z-trening.com/tasks.php?show_task=5000000778
http://z-trening.com/tasks.php?show_task=5000000363
http://z-trening.com/tasks.php?show_task=5000001024
http://www.spoj.com/problems/VOCV/
http://www.spoj.com/problems/PT07F/
http://www.spoj.com/problems/PT07X/
http://z-trening.com/tasks.php?show_task=5000000070
http://z-trening.com/tasks.php?show_task=5000000569
http://z-trening.com/tasks.php?show_task=5000000441
http://z-trening.com/tasks.php?show_task=5000000050
http://www.spoj.com/problems/RENT/
http://www.spoj.com/problems/INCSEQ/
http://www.spoj.com/problems/INCDSEQ/
http://z-trening.com/tasks.php?show_task=5000000624
http://z-trening.com/tasks.php?show_task=5000000742
http://z-trening.com/tasks.php?show_task=5000000749
http://z-trening.com/tasks.php?show_task=5000001044
http://www.spoj.com/problems/SEQ/
http://www.spoj.com/problems/SPP/
http://z-trening.com/tasks.php?show_task=5000000078
http://z-trening.com/tasks.php?show_task=5000000543
http://z-trening.com/tasks.php?show_task=5000000718
http://z-trening.com/tasks.php?show_task=5000000237
http://z-trening.com/tasks.php?show_task=5000000311
http://www.spoj.com/problems/MORSE/ (dp + trie) Very Hard.
http://www.spoj.com/problems/MPOLY/
http://www.spoj.com/problems/CVXPOLY/
http://www.spoj.com/problems/MTRIAREA/
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=2222
http://www.lightoj.com/login_main.php?url=volume_showproblem.php?problem=1122
http://www.lightoj.com/login_main.php?url=volume_showproblem.php?problem=1122
http://www.lightoj.com/login_main.php?url=volume_showproblem.php?problem=1125
Game (IOI 2008, Practice session)
http://www.topcoder.com/tc?d1=tutorials&d2=dynProg&module=Static



Graph Theory:
http://community.topcoder.com/stat?c=problem_statement&pm=10736
http://www.spoj.com/problems/TRAFFICN/
http://www.spoj.com/problems/PA06ANT/
http://www.spoj.com/problems/PT07Z/
http://www.spoj.com/problems/EXPLOSN/
http://www.spoj.com/problems/BUGLIFE/
http://www.spoj.com/problems/SSORT/
http://www.spoj.com/problems/ARBITRAG/
http://www.spoj.com/problems/CODE/
http://www.spoj.com/problems/FROGGER/
http://www.spoj.com/problems/GCPC11C/
http://www.spoj.com/problems/GCPC11J/
http://www.spoj.com/problems/GHOSTS/
http://www.spoj.com/problems/MAKETREE/
http://www.spoj.com/problems/PARADOX/
http://www.spoj.com/problems/QTREE/
http://www.spoj.com/problems/QTREE2/
http://www.spoj.com/problems/QUEEN/
http://www.spoj.com/problems/ROBOTGRI/
http://www.spoj.com/problems/ELEVTRBL/
http://www.spoj.com/problems/TRIPINV/
http://www.spoj.com/problems/CAPCITY/
http://www.spoj.com/problems/KOICOST/

Thursday, 23 May 2013

Sieve Methods : Prime, Divisor, Euler Phi etc.

Structure of the article. First I will explain what does sieve mean. Then I will give you some examples with corresponding java codes and finally some exercises :)

   According to  http://www.thefreedictionary.com/sieve , "sieve" means A utensil of wire mesh or closely perforated metal, used for straining, sifting, ricing, or puréeing. Similar to this definition sieve is a method of doing stuff, where you keep rejecting the things that are useless and reducing the space that you are currently looking at.

   So much of thing in air, Let us now take an example.


1. Finding primes upto N.
     You have to print all primes upto N.


Method1 :  For all the numbers i from 1 to N, check if i is prime or not. If it is a prime, then print it.

      Subproblem:

            Checking whether a number K is prime.

      Solution:

             1. For all numbers i from 2 to K-1, check if K is divisible by i (as every number is divisible by 1 and itself). If yes, then not a prime else the number is a prime.

                Complexity of this solution : O(K)

             2. Note that we do not need to check upto K-1, instead we  can very well check upto sqrt(K).
            
              Proof: Let us say a number K = a * b. Note that atleast one of a and b <= sqrt(K) otherwise product of them would exceed K. So check just upto sqrt(K).

             3. Either use some probabilistic method for finding whether a number is prime or not.
     More on this later. For now see link
                                  
                                 http://en.wikipedia.org/wiki/Primality_test
                             http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=primalityTesting

Method 2:
      Now here comes the idea of sieve. So initially assume that all numbers are prime. Then you try to sieve/refine your search range by not looking at the entire numbers but at the reduced space. eg. When you find all the numbers which are divisible by 2, You do not look into those again, as they are already not prime. So those numbers are sieved out. Now try for 3,4, upto n.
      In other terms, You first try all the numbers which are divisible are 2 (except 2 itself),
Note that all those wont be primes. So you can remove those out of your consideration now. Now try the same for 3,4,...... N. Finally you will end up with only prime numbers.
      For understanding the actual thing going on, see the code.
     So the code basically sets all the numbers upto N to be prime. Then for every number that is still prime, we set all of its multiples upto N to be non-prime.
   
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    static boolean[] isPrime;

    public static void sieveOptimized(int N) {
        isPrime = new boolean[N + 1];
        
        for (int i = 2; i <= N; i++)
            isPrime[i] = true;
        for (int i = 2; i * i <= N; i++) {
                if (isPrime[i]) {
                // For further optimization, You can do instead of j += i, j += (2 * i).
                // Proof is left to reader :)
                for (int j = i * i; j <= N; j += i) 
                    isPrime[j] = false;
            }
        }
    }
    

    public static void sieve(int N) {
        isPrime = new boolean[N + 1];
        
        for (int i = 2; i <= N; i++)
            isPrime[i] = true;
        for (int i = 2; i <= N; i++) {
            if (isPrime[i]) {
                for (int j = i + i; j <= N; j += i) 
                    isPrime[j] = false;
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int limit = sc.nextInt();

        // call the sieve method
        sieveOptimized(limit);

        for (int i = 1; i <= limit; i++) {
            if (isPrime[i]) 
                System.out.printf("%d ",i);
        }
    }
}

     Now comes the complexity part: Complexity of this code is O(n * logn).
     Proof: (This proof comes a lot of times in various algorithms, So pay attention).
     For all numbers i going from 2 to n, you need to check all the multiples of i. Note that number of multiples of i upto n are n / i. Hence Expression for the complexity will be written as n / 2 + n / 3 + .. + 1. Take n common out of expression. Now it becomes n * (1 / 2 + ..... + 1/n). 
     Now as the expression is definitely greater than n. So adding an n to the expression won't have any effect on the complexity, So add n to the expression and now it becomes n * (1 + 1 / 2 + ... + 1/ n).  The expression (1 + 1 / 2 + 1 / 3 + ... 1 / n) is harmonic sum and it's bounded by ln(n). Hence overall complexity is O(n * logn)
     Proof of harmonic sum:
           A simple Proof: Let us integrate 1 / x from 1 to n. (Note that we are doing integration, which means sum of area under the curve 1/x, which is greater than (1 + 1 / 2 + ... + 1 / n). Value of the integral can be found easily. In fact integration of 1/x dx is ln(x).
2. Finding Sum of divisors of numbers upto N.
     Now you have to find sum of divisors of all numbers upto N. Here we are not just considering proper divisors(numbers other 1 and itself), we are considering all the divisors. Here you can do something like this.
    Here let us say divisorSum[i] denotes sum of divisors of i. Intially value of divisorSum[i] is equal to zero. Then for all the numbers i from 1 to n, We check all the multiples of i (let us say j) and add i to divisorSum[j].
    In other words, Start from 1 and for all the numbers which are multiples of 1, increment their sumDiviors by 1.
    Now do the same for 2,3, ... N. Note that for a number i, you are doing this adding operation upto N/i times. So the complexity calculation is same as before.
   Look the beautifully commented code.

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        
        while (T-- > 0) {
            int n = sc.nextInt();
            int divisorSum[] = new int [n + 1];
            
            // For every number i, You know that 2*i,3*i,4*i   upto k*i such that k*i<=n, will have i 
            // as one of it's divisors, so add that to divisorSum[j]

            for (int i = 1; i <= n; i++) {
                for (int j = i; j <= n; j += i) {
                    divisorSum[j] += i;
                }
            }
            
            // Complexity of this code is O(n * logn)
            // Proof: Expression for the complexity can be written as n / 1 + n / 2 + ... + n / n
            // take n common
            // n * (1 + 1 / 2 + ..... + 1/n)
            // (1 + 1 / 2 + 1 / 3 + ... 1 / n) is harmonic sum and it's bounded by logn.
            // A simple Proof: Let us integrate 1 / x from 1 to n. 
            // (Note that we are doing integration, which means sum of area under the curve 1/x
            // which is greater than (1 + 1 / 2 + ... + 1 / n)
            // value of integration can be found easily
            // as integration of 1/x dx is ln(x)

            for (int i = 1; i <= n; i++) 
                System.out.printf("%d ", divisorSum[i]);
            
            System.out.printf("\n");
        }
    }
}

3. Finding No of divisors of numbers upto N.
        This is also same as the previous example. Here instead of the storing sum in the array, store the number of divisors and for every multiple of i (say j), In the previous example, you were adding value i to divisorSum[j] , Here just increment the count of noOfDivisior[j] by one.
        Code is very easy and hence omitted. Complexity is also same.

4. Sieve for finding euler phi function.
       I will denote the euler phi function for a number n by phi(n). phi(n) is defined as follows.
It is count of how many number from 1 to n are coprime(having gcd value 1) to n.
For example phi(6) is 2 as 1,5  are coprime to 6.
      Few properties of phi function :
        a). phi(p) = p - 1. Where p is prime. All the numbers from 1 to p - 1 are coprime to p.
        b). phi(a * b) = phi(a) * phi(b) where a and b are coprime.
        c). phi(p^k) = p^k - p^(k - 1). Note that here ^ denotes power. Here all the numbers from 1 to p^k are coprime to p^k except all the multiples of p, which are exactly p^(k -1).

     Method for finding:
      1. Simple : For all numbers from 1 to n, check if it is coprime to n or not, If yes add that to your answer.
      2.  Let us say your number is n, which can be denoted as p1^k1 * p2^k2 ..... *p_m*k_m. Note that here p1, p2.... pm are prime. Basically n is written in it's prime representation.
           Then phi(n) would be [ p1^k1 - (p1^(k1-1) ) ] * ....... [p_m^k_m - (p_m^(k_m-1) )] . The expression for n can also be written as p1^k1 * p2^k2 * .... * p_m^k_m * (1 - 1/p1) * (1 - 1/p2) .... * (1 - 1/pm).
which is equal to n * (1 - 1/p1) * (1 - 1/p2) .... * (1 - 1/p_m).
     See the code for details.
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    
    public static boolean isPrime (int n) {
        if (n < 2)
            return false;
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0)
                return false;
        return true;
    }
    
    public static int eulerPhiDirect (int n) {
        int result = n;
        for (int i = 2; i <= n; i++) {
            if (isPrime(i))
                result -= result / i;
        }
        
        return result;
    }
    
    public static int eulerPhi (int n) {
        int result = n;
        // think that it like this, initially all numbers have gcd with n to be equal to 1.
        // Hence value of result is n
        // according to formulla  n * (1 - 1/p1) * (1 - 1/p2) .... * (1 - 1/pm). We will be calculating value 
        // of the product upto i. that is n * (1 - 1/p1) * ... (1 - 1/p_i)
        // So let us take example of p1. value of result after one iteration will be n - n / p1, which is precisly
        // n * (1 - 1/p1). 
        // Similarily by induction hypthesis we can say finally the result will be as required.

        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                result -= result / i;
                
                // By using while loop here, we are ensuing that all the numbers i will be prime.
                // because for every i, all it's multiples are gets removed.
                while (n % i == 0) {
                    n /= i;
                }
            }
        }
        
        if (n > 1) {
            result -= result / n;
        }
        
        return result;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        
        while (T-- > 0) {
            int n = sc.nextInt();

            // call the eulerPhiDirect or eulerPhi method. 
            // culerPhi is more faster as it does not take sqrt(n) for checking for prime

            int ans = eulerPhiDirect (n);
            System.out.println(ans);
        }
    }
}
 
Now Let us calculate value of sieve of all numbers from 1 to N.
       Let us say eulerPhi[i] be the value of phi(i). Assign initially all the values of eulerPhi[i] to be i. Then for every prime p, for all multiples of p, we will multiply value of eulerPhi[i] by (1 - 1/p) as per the formula. multiplying eulerPhi[i] by (1 - 1/p) is exactly equal to eulerPhi[i] -= (eulerPhi[i] / p).      
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    
    public static boolean isPrime (int n) {
        if (n < 2)
            return false;
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0)
                return false;
        return true;
    }
    
    private static int[] eulerPhi;
    
    public static void eulerSieve (int N) {
        eulerPhi = new int[N + 1];
        
        // set initial value of phi(i) = i
        for (int i = 1; i <= N; i++)
            eulerPhi[i] = i;
        
        // for every prime i, do as described in blog.
        // Note that we are using isPrime(i) function that takes sqrt(n) time. You are advised to write 
        // a seperate sieve of finding primes as described by me.
        // which will reduce the compleixty of this to n * log(n)
        // Proof of this is similar to previous ones. Left to reader.
        
        for (int i = 1; i <= N; i++) {
            if (isPrime(i))
                for (int j = i; j <= N; j += i)
                    eulerPhi[j] -= eulerPhi[j] / i;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        
        eulerSieve(N);
        
        for (int i = 1; i <= N; i++)
            System.out.printf("%d ",eulerPhi[i]);
    }
}

 5. Sieve for finding inverse modulo m.
       Inverse of a number a with respect to m is defined as b * a = 1 mod m. Then b is called inverse of a modulo m.
      So first of all do you realize that it is not necessary to exist the inverse? Example for a = 4, b = 6.
      Let us play around the expressions. a * b = 1 mod m can be written as a * b = 1 + m * k.
      which  can be written as a * b - m * k = 1. If the left side has a common factor of both a and m, means gcd(a,m) != 1, then note that right side won't be divisible by that number, Hence no solution of
the equation when a and m has gcd non zero. Hence inverse will exist when a and m have non zero gcd.
     
      Now solving a * b + m * (-k) = 1. write the same as a * b + m * k1 = 1.
      So let us try to find solution of a * b + k * m = 1. This k is not equal to previous k, infact it is -k. It is equal to k1.
      So let us try to solve generic problem a * x + b * y = 1. where a and b can also be negative and gcd(a, b) = 1.
      Let us try a simpler version of the same problem which is solving b * x1 + (a  % b) * y = 1;
      Now try to relate these equations.
      a % b = a - [ a / b] * b. where [] denotes floor function. This is same as removing all multiples of b from a, which is exactly equal to a % b.
     Now the equation turns into b * x1 + (a - [a/b] * b) * y1 = 1
     which is a * y1 + b * (x1 - [a/b] * y1) = 1.
     So this is recursive version of a * x + b * y = 1, where x is replaced with y1 and y is replaced with x1 - [a/b]  * y1.
    Things getting really complex.
    (Note that this method is exactly similar to finding gcd of two numbers). Seeing the code will help you to understand more.)
    Complexity of this code is same as gcd as it has exactly the same recurrence relation as of that. Time complexity of gcd(m, n) is log (max(m , n)). Hence we can consider time complexity of this method around O(logn).

import java.io.*;
import java.util.*;
import java.math.*;

class pair {
    public int x, y;
    
    pair (int x,int y) {
        this.x = x;
        this.y = y;
    }   
    
    boolean isEquals (pair p) {
        if (this.x == p.x && this.y == p.y) 
            return true;
        else 
            return false;
    }
}

public class Main {
    public static int gcd (int a, int b) {
        if (b == 0)
            return a;   
        return gcd (b, a % b);
    }
    
    public static pair solve (int a,int b) {
        if (b == 0) {
            // a * x + b * y = 1
            // here b = 0
            // hence a * x = 1
            // if a is not 1, then error else x = 1 and y = 0
            // Note that error wont be here, we will always find a which is not 1
            // as error case is already handle in solveThis function
            return new pair (1, 0);
        } else {
            // do the recursive call
            pair p = solve (b, a % b);
            int x1 = p.x;
            int y1 = p.y;
            
            int x = y1;
            int y = x1 - (a / b) * y1;
            
            return new pair (x, y);
        }
    }

    public static pair solveThis (int a, int b) {
        if (gcd (a, b) != 1)
            // (-1, -1) corresponds to error, that means no inverse exists in this case
            return new pair (-1, -1);
        else 
            return solve (a, b);
    }

    public static int modpow (long a, long b, long c) {
        long res = 1;
        while (b > 0) {
            if (b % 2 == 1) {
                res = (res * a) % c;
            }
            a = (a * a) % c;
            b >>= 1;
        }
        
        return (int) res;
    }

    public static int findInverseModuloPrime (int a, int p) {
        return modpow (a, p - 2, p);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int m = sc.nextInt();
        
        pair p = solveThis (a, m);

        if (p.isEquals(new pair(-1, -1)))
            System.out.printf("Error, inverse does not exist");
        else 
            System.out.printf("%d %d\n", p.x, p.y);
    }
}

 
Now I will tell you another easier method . Generalized version of Fermat's theorem says that a ^ phi(m) = 1 mod m where gcd(a, m) = 1. Fir finding inverse a ^ (phi(m) - 1) = 1 / a (mod m) = (inverse(a)) mod m.
  Hence for finding inverse(a) mod m, You can just find a ^ (phi(m) - 1) by modular exponention method. In case of m being prime, As phi(m) = m - 1. So just find a ^ (m - 2) % m. This is what precisely computed by modpow function in the above function.

Complexity:  

Now Sieve for finding inverse modulo m:
    You have to find inverse(i) mod m for i ranging from 1 to n.
    as complexity of modpow is log (n). and we are doing this for n numbers. Hence total complexity will be O(n * logn). Here I am going to describe a better method using sieve   
    Just use the identitiy inverse(i) = (-(m/i) * inverse(m % i) ) % m + m;
   Proof:  It is good one. I do not want reveal it now. Try yourself. If you come up with it, post it in the comments, I would check whether it is correct or not?

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
                
        int n = sc.nextInt(); // denotes the range upto which you want to find the value of modInverse[i]
        int m = sc.nextInt();

        int modInverse[] = new int[n + 1];
        
        modInverse[1] = 1; // this is you know 1 * 1 mod m = 1

        for (int i = 2; i <= n; i++) {
            modInverse[i] = (-(m/i) * modInverse[m % i]) % m + m;
        }
        
        for (int i = 2; i <= n; i++) 
            System.out.printf("%d ", modInverse[i]);
    }
}

Finally, Here are some few exercises for you to try
     I will keep adding the list if I found some problems.

Tuesday, 21 May 2013

Solving some dynamic programming problem

     Today I am going to explain solution of this problem : http://www.spoj.com/problems/BEHAPPY/
Before going to read this article, I would recommend you to read
1. http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=alg_index

2. http://apps.topcoder.com/forums/?module=Thread&threadID=700080&start=0

3. http://apps.topcoder.com/forums/?module=Thread&threadID=697369&start=0
4. http://apps.topcoder.com/forums/?module=Thread&threadID=697925&start=0

     Now let us see the problem. It says that no of ways of arraning a_1 + a_2 + ... a_m  = n.
where A[i] <= a_i <= B[i].
Note the ranges : 1<=M<=20, 1<=N<=100, 0<=Ai,Bi,<=100

    If you try to check every possible, then it is going to surely going to time out. Finding time complexity of naive approach is left to reader. For solving this problem, you can use maximal matching. But here I would describe only dp algorithm.
  1.    Let dp[i][j] denotes the  no of ways of arranging when you have assigned upto i - 1 places and trying to assign i th place.  
  2.    From state dp[I][j] by matching I and j, you can go to state dp[I + 1][j + 1]. By not matching, you can go to dp[I][j + 1]. Coding this is very easy and complexity is O(n*n). 

Saturday, 18 May 2013

Using Constructors and Comparators in C++

First I will describe using constructors in C++.

struct point {
    int x, y;
    point() {} // default constructor
    point (int _x, int _y) {
        x = _x;
        y = _y;
    }
};
// Note that in C++, You do not need to use typedef as it is already typedefed
to point, So you do need to use
// typedef for that.
Another way
struct point {
    int x, y;
    point() {} // default constructor
    point (int x, int y): x(x), y(y) {}
};

Now using comparators in C++
struct point {
    int x, y;
    point() {}
    point (int x, int y) : x(x), y(y) {}
    
    // overloading of < operator
    bool operator<(const point &rhs) const{
        // your main logic for the comparator goes here
        return make_pair(y,x) < make_pair(rhs.y, rhs.x);
    }
For more information about using operator overloading you can visit this link
http://www.cprogramming.com/tutorial/operator_overloading.html
Final Code:
// Comment
struct point {
    int x, y;
    point() {}
    point (int _x, int _y) {
        x = _x;
        y = _y;
    }
    bool operator<(const point &rhs) const{
        return make_pair(y,x) < make_pair(rhs.y, rhs.x);
    }
    bool operator==(const point &rhs) const{
        return make_pair(y,x) == make_pair(rhs.y, rhs.x);
    }
};
Sample uses:
 sorting an array in reverse order
 Method 1:
vector a;
sort(a.rbegin(), a.rend());
Method 2:
sort(a.begin(), a.end());
For an vector
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

bool cmp(int a,int b) {
    return a >= b;
}

int main() {
    int n;
    cin >> n;

    vector a;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        a.push_back(x);
    }

    sort (a.begin(), a.end(), cmp);

    for (int i = 0; i < a.size(); i++)
        cout << a[i] << " ";


    return 0;
}

For an array now.
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

bool cmp(int a,int b) {
    return a >= b;
}

int a[100];

int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
       cin >> a[i];
    }

    sort (a, a + n, cmp);

    for (int i = 0; i < a.size(); i++)
        cout << a[i] << " ";


    return 0;
}

Deleting redundent points out of n points given. Two points are considered to be equal if both their x and y coordinates are equal.
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

struct point {
    int x, y;
    point() {}
    point (int x, int y) : x(x), y(y) {}
    bool operator<(const point &rhs) const{
        return make_pair(y,x) < make_pair(rhs.y, rhs.x);
    }
    bool operator==(const point &rhs) const{
        return make_pair(y,x) == make_pair(rhs.y, rhs.x);
    }
};

int main() {
    int n;
    cin >> n;

    vector a;

    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        a.push_back(point(x, y));
    }

    set st;

    for (int i = 0; i < n; i++) {
        st.insert(a[i]);
    }

    vector res;
    set :: iterator it;
    for (it = st.begin(); it != st.end(); it++) {
        res.push_back(*it);
    }

    for (int i = 0; i < res.size(); i++)
        cout << res[i].x << "  " << res[i].y << endl;

    return 0;
}

Monday, 29 April 2013

Important things to do in summers 2013


1. Getting good in programming contests. Start practicing at topcoder, codeforces, spoj and codechef.
Also solve some old problems of different - different contests.
2. Read mathematics problem for putnam competition.
3. Operating system and networks revision.
4. Puzzle solving for job perspective.
5. Writing something in English for improvement of thought flowing and generation of ideas.
6. Writing short stories in both Hindi and English, I will also try poems.
7.  http://www.imomath.com/index.php?options=525&lmm=0
8. Revise algorithms
9. Doing some courses in algorithms
10. Reading some interesting book about algorithms and recent research areas in algorithms.
11.http://www.artofproblemsolving.com/Forum/resources.phpc=2&cid=23&sid=7a1e4924e7b49fad01070aa057d3e6d9
12.