Hello all,
This time I am posting the solution to an Interview Street Problem BINOMIAL COEFFICIENTS:: https://www.interviewstreet.com/challenges/dashboard/#problem/4fe19c4f35a0e
My solution is not passing all test cases as the error shown is TIME LIMIT EXCEEDED ! Suggestions are welcome !
//Binomial Coefficient Problem
import java.io.*;
import java.math.BigInteger;
import java.util.Scanner;
public class BinomialTry
{
public static BigInteger fact(BigInteger x)
{
BigInteger f=BigInteger.ONE;
for(BigInteger i=x;i.compareTo(BigInteger.ONE)>0;i=i.subtract(BigInteger.ONE))
f=f.multiply(i);
return f;
}
public static BigInteger BinoCoff(BigInteger m,BigInteger k)
{
BigInteger d1=fact(m.subtract(k));
BigInteger d=d1.multiply(fact(k));
BigInteger ans=fact(m).divide(d);
return ans;
}
public static int modulo(BigInteger bc,BigInteger p)
{
if(bc.remainder(p)==BigInteger.ZERO)
return 1;
else return 0;
}
public static void main(String args[])throws IOException
{
int j=0,c=0;
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
if(t<0 || t>100)
System.exit(0);
int arr[]=new int [t];
for(int i=0;i<t;i++)
{
BigInteger n=sc.nextBigInteger(); BigInteger p=sc.nextBigInteger();
BigInteger k;
for(k=BigInteger.ZERO ;k.compareTo(n)<=0;k=k.add(BigInteger.ONE))
{
BigInteger temp=BinoCoff(n,k);
if(modulo(temp,p)==1)
c++;
}
arr[j]=c;
c=0;
j++;
}
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
System.gc(); //Garbage clean !!
}
}
This time I am posting the solution to an Interview Street Problem BINOMIAL COEFFICIENTS:: https://www.interviewstreet.com/challenges/dashboard/#problem/4fe19c4f35a0e
My solution is not passing all test cases as the error shown is TIME LIMIT EXCEEDED ! Suggestions are welcome !
//Binomial Coefficient Problem
import java.io.*;
import java.math.BigInteger;
import java.util.Scanner;
public class BinomialTry
{
public static BigInteger fact(BigInteger x)
{
BigInteger f=BigInteger.ONE;
for(BigInteger i=x;i.compareTo(BigInteger.ONE)>0;i=i.subtract(BigInteger.ONE))
f=f.multiply(i);
return f;
}
public static BigInteger BinoCoff(BigInteger m,BigInteger k)
{
BigInteger d1=fact(m.subtract(k));
BigInteger d=d1.multiply(fact(k));
BigInteger ans=fact(m).divide(d);
return ans;
}
public static int modulo(BigInteger bc,BigInteger p)
{
if(bc.remainder(p)==BigInteger.ZERO)
return 1;
else return 0;
}
public static void main(String args[])throws IOException
{
int j=0,c=0;
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
if(t<0 || t>100)
System.exit(0);
int arr[]=new int [t];
for(int i=0;i<t;i++)
{
BigInteger n=sc.nextBigInteger(); BigInteger p=sc.nextBigInteger();
BigInteger k;
for(k=BigInteger.ZERO ;k.compareTo(n)<=0;k=k.add(BigInteger.ONE))
{
BigInteger temp=BinoCoff(n,k);
if(modulo(temp,p)==1)
c++;
}
arr[j]=c;
c=0;
j++;
}
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
System.gc(); //Garbage clean !!
}
}






0 comments:
Post a Comment