Skip to content

Instantly share code, notes, and snippets.

@Kaushal28
Created May 27, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kaushal28/429b7671c76acbea9637b8e734361d26 to your computer and use it in GitHub Desktop.
Save Kaushal28/429b7671c76acbea9637b8e734361d26 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.math.*;
class GFG {
public static BigInteger binomial(int n, int k){
if(n < k){
return new BigInteger("0");
}
BigInteger dp[][] = new BigInteger[n+1][n+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
if(j == 0 || i == 0 || i == j){
dp[i][j] = new BigInteger("1");
} else if(i<j){
dp[i][j] = (new BigInteger("0")).subtract(new BigInteger("1"));
} else{
dp[i][j] = dp[i-1][j].add(dp[i-1][j-1]);
}
}
}
return dp[n][k].mod(new BigInteger("1000000007"));
}
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-->0){
int n = in.nextInt();
int k = in.nextInt();
System.out.println(binomial(n,k));
}
}
}
@yogeshjoshi
Copy link

why did you write "mod(new BigInteger("1000000007")" in return statement?

@sm86
Copy link

sm86 commented Sep 25, 2017

@yogeshjoshi Modulus your output to 109+7 is mentioned in the question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment