Question:
Output:
Compute and print the value in seperate line. Modulus your output to 109+7. If n
Constraints:
1<=T<=50
1<=n<=1000
1<=r<=800
Example:
Input:
1
3 2
Find nCr for given n and r.
Input:
First line contains no of test cases T, for every test case 2 integers as inputs (n,r).
First line contains no of test cases T, for every test case 2 integers as inputs (n,r).
Output:
Compute and print the value in seperate line. Modulus your output to 109+7. If n
Constraints:
1<=T<=50
1<=n<=1000
1<=r<=800
Example:
Input:
1
3 2
Output:
3
3
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
class GFG {
public static BigInteger binomial(int n, int k){
if(n < k){
return new BigInteger("0");
}
BigInteger comb[][] = new BigInteger[n+1][k+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=Math.min(i,k);j++){
if(j == 0 || i == 0 || i == j){
comb[i][j] = new BigInteger("1");
} else if(i<j){
comb[i][j] = (new BigInteger("0")).subtract(new BigInteger("1"));
} else{
comb[i][j] = comb[i-1][j].add(comb[i-1][j-1]);
}
}
}
return comb[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));
}
}
}
EXECUTION TIME: 1.24s(Ya not good...but better than 3.6+ 😉😉😃😃)
Comments
Post a Comment