For the SPOONS problem I pre computed an array of values n choose floor(n/2).However I’m getting overflow as i noticed negative numbers after some n = 20 .I have used unsigned long long datatype,shoudn’t it be enough for numbers till 10^18?? Perhaps my multiplication of ints or something must be the problem.
Heres my code(i.e. the exact function:
void precompute(ull choose[65])
{
int r = 0; // ull is unsigned long long
bool alt = 1;
choose[1] = 1;
for ( int n = 2 ; n < 65 ; ++n)
{
if (alt)
{
r += 1;
choose[n] = choose[n-1] * (n / r); // am calculating n choose [n/2]
alt = 0;
}
else {
choose[n] = choose[n-1] / (n -r) * (n);
alt = 1;
}
}
}
whats the problem ??