FCTRL2: Getting wa

Here’s my code in C for Small Factorials problem in the Easy section of Practice problems. The code runs correctly for the sample test cases given, but when I submit it, I get wrong answer. Why is this happening? Please help me.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int i, n, *p, *q;
    scanf("%d", &n);
    p = (int *)malloc(n*sizeof(int));
    q=p;
    for(i=0; i<n; i++)
    {
        scanf("%d", &p[i]);
    }
    for(i=0; i<n; i++)
    {
        long fact=1;
        int m = p[i];
        while(m != 1)
        {
            fact = fact * m;
            m--;
        }
        printf("%ld\n", fact);
    }
    free (q);
    return 0;
}

You have to realize that factorials are big numbers. 100! is number ending with 24 zeros, so it’s greater than long long max.

1 Like

You should check out the tutorial
SMALL FACTORIALS

1 Like

thanks… I got it.

If you want to do like this, you can download bignum library, but I’m not sure is this working on codechef. Check the tutorial :slight_smile: