I've tried the Marbles pkroblem on spoj - (link: http://www.spoj.com/problems/MARBLES/ )
However I'm getting Runtime Error(SIGSEGV) after multiple tries.
Here's the code I submitted -
#include <stdio.h>
int comb(long int n, long int k)
{
if (n==k || k==0)
return 1;
else
return (comb(n-1, k) + comb(n-1, k-1));
}
int main() {
int t;
long int n, k;
scanf("%d", &t);
while(t--)
{
scanf("%ld %ld", &n, &k);
printf("%d\n", comb(n-1, k-1));
}
return 0;
}
I cant understand why I'm getting the error. Other solutions on the internet have all used iterative way to calculate the nCr combination. I've tried using a recursive approach. Any help would be highly appreciated.