Wednesday, June 13, 2012

Binomial Coefficients - Part 3

This post exploits the recurrence :
C(n,k) = C(n-1, k) + C(n-1, k-1)

If the limits to n are known, then the above recurrence can be used for dynamic programming.

Implementation:

Binomial Coefficients - Part 2

In many problems, it is required to find the answer modulo a large prime, such as 1000000007 (hereby referred to as MAX or m). If the problem involves binomial coefficients, then they have to be calculated modulo MAX too.

It is easy to calculate factorials modulo MAX, because
(a*b) mod m = (a mod m) * (b mod m)
but the same does not hold for division, which makes it tricky to calculate binomial coefficients.

However, the following relation holds :
(a/b) mod m = (a mod m) * (c mod m)
where 'c' is the modular multiplicative inverse[1] of 'b'.

Thus, the relation
C(n, k) = fact(n)/(fact(n-k)*fact(k))
is reduced to
C(n,k) = fact(n)*invfact(n-k)*invfact(k)
where C(a,b) and fact(a) are expressed modulo m, and invfact(a) denotes the multiplicative inverse of fact(a) modulo m. Multiplication is also performed modulo m.

Implementation :

The above implementation employs efficient routines for modular arithmetric.

[1] - http://en.wikipedia.org/wiki/Modular_multiplicative_inverse

Binomial Coefficients - Part 1

Implementation 1:
int factorial(int n){
 if (n <= 0) return 1;
    int f = 1;
    do { f *= n;} while (--n);
    return f;
}

int C(int n, int k){
    if (n < k) return 0;
    return fact(n)/(fact(k)*fact(n-k));
}
It works for n <= 12.

Implementation 2:

int C(int n, int k){
    if (n < k) return 0;
    if (k > n/2) k = n - k;
    int c = 1;
    for (int i = 1; i <= k; i++) c = (c*(n-i+1))/i; 
    return c;
}
It works for n <= 29.

The important thing to note here is that, because of the way the loop is written, the division never results in a non-integer. (Why? Think about it!)

Greatest Common Divisor

template <class T>
T gcd (T a, T b){
   return b != 0 ? gcd(b, a%b) : a;
}

Tuesday, July 6, 2010

OpenGL/GLUT with C++ on Windows 7

Windows 7 comes with a dll for OpenGL, and most C++ compilers and IDE's come with library and header files for OpenGL too. But, if you've just begun learning OpenGL, then you are definitely going to need GLUT. Setting things up is a real pain, and can take quite some time. I had to struggle for a while, but then I found this awesome post on levelbylevel.com. It's like a step-by-step guide to set up GLUT and start working with it on CodeBlocks (with MinGW). It was a real lifesaver. I'm posting a breif summary here:
  • Get a C++ IDE with an OpenGL library and header file. CodeBlocks(with MinGW) or DevC++ will do.
  • Download GLUT and extract the zipped file.
  • Copy the file glut.dll into the system folder C:\Windows\System32 (VERY IMPORTANT: If you are using a 64-bit version of Windows, copy the file into C:\Windows\SysWOW64, otherwise it won't work).
  • Copy the file glut.h into the 'include\GL' folder in the C++ compiler folder. On DevC++, it is in the folder where DevC++ is installed, on CodeBlocks it is the MinGW folder inside the folder where CodeBlocks is installed.
  • Copy the file libglut32.a into the 'lib' folder in the C++ compiler folder.
  • Link the libraries 'opengl32' , 'glu32' and 'glut32' to the project in which you wish to use GLUT (how this is done depends on what IDE you are using).
  • You are now good to go.
 If you're looking for a good book to get started with OpenGL without going too deep into the math, try OpenGL SuperBible.