I need to calculate factorial of a number mod a large prime number. My program works up to some values but, it fails (segmentation fault) when I reach a higher value like the one mentioned in the program.
How can I make this program work?
Thanks a lot
#define LL unsigned long long
#define ull unsigned long long
const LL mod=1000000009;
LL arr[1048580];
inline ull mulMod(ull a,ull b,ull c)
{
if(a<=1000000000ULL && b<=1000000000ULL)
{
//cout<<((a%c)*(b%c))%c<<endl;
ull ret = ((a%c)*(b%c))%c;
return ret;
}
ull ret = 0ULL; a=a%c;
while(b > 0ULL)
{
if(b&1ULL) ret = ((ret%c)+(a%c))%c;
a = (a<<1ULL)%c;
b>>=1ULL;
}
return ret%c;
}
LL fact(LL num)
{
if(arr[num]==0)
{
arr[num]=mulMod(num,fact(num-1),mod);
return arr[num];
}
return arr[num];
}
int main()
{
arr[0]=1;
cout<<fact(325720);
}