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:
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define C_MAX 250 | |
int C[C_MAX][C_MAX]; | |
C[0][0] = 1; | |
for (int i = 1; i < C_MAX; i++){ | |
C[i][0] = 1; | |
for (int j = 1; j <= i; j++){ | |
/* Actual Binomial Coefficient */ | |
//C[i][j] = C[i-1][j] + C[i-1][j-1]; | |
/* Binomial Coefficient modulo MAX */ | |
C[i][j] = (C[i-1][j] + C[i-1][j-1]) % MAX; | |
} | |
} |
may be you should un-comment the commented part.For C_MAX with value 250, It exceeds the bounds of an integer.
ReplyDeleteDone! Thanks!
ReplyDeleteyou could also have used http://en.wikipedia.org/wiki/Lucas'_theorem for this problem
ReplyDelete