The program I have posted here is problem of marbles from spoj, and I know it has been discussed here, and I know the logic.
But when I am calculating the factorial, it overflows even on calculation of 29 factorial. What can I do?
long long is not supported
package marbles_spoj;
import java.util.Scanner;
public class combinations_with_repetition {
public static long calc(long n,long k)
{
System.out.println(n+" "+k);
long res=0;
res=factorial(n)/(factorial(k)*factorial(n-k));
return res;
}
static long factorial(long n)
{ long result=1;
if (n==1||n==0)return 1;
else
for(long i=2;i<n;i++)result=result*i;
//System.out.println("result is :"+result);
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of marbles to b picked up");
long n=sc.nextLong();
System.out.println("enter no. of colors of marbles availables");
long r=sc.nextLong();
System.out.println("Number of combinations possible "+calc(n-1,r-1));
sc.close();
}
}