I am trying to compute the following value.
prod = 1;
for(int i=1;i<N;i++){
prod = prod*i;
}
Since N can be large, I was asked to compute modulo 10^9+7
and I did.
int prod =1;
for(int i=1;i<N;i++)
{
prod = ((prod%1000000007) * (i%1000000007))%1000000007;
}
Some one else did.
long ways=1;
for(int j=1;j<N;j++)
{
ways = (int)(j * ways % 1000000007);
}
The online judge is taking the second as correct. Why is it?
So I ran this
int prod = 1;
long ways = 1;
for(int j=1;j<14;j++){
prod = ((prod%1000000007) * (j%1000000007))%1000000007;
ways = (int)(j * ways % 1000000007);
if(prod!=ways){
System.out.println(prod+" "+ways);
System.exit(0);
}
System.out.println(prod+" "+ways+" "+j);
}
And when prod or ways
is 479001600
and j
is 12
in the next iteration they are not equal. Both of which are less than int max which is 2147483647
So I do this and they are equal
prod = ((479001600%1000000007) * (12%1000000007))%1000000007;
ways = (int)(12 * 479001600 % 1000000007);
if(prod!=ways){
System.out.println(prod+" "+ways);
System.exit(0);
}
System.out.println(prod+" "+ways);
I think it is something something casting. But cannot figure out. Please tell me if I am doing something wrong?