Ambiguity for input A : 71045970 B : 41535484 C : 64735492


#1

I have two codes snippets.
This does not work

int Solution::Mod(int A, int B, int C) {

if(A == 0)
return 0;
if (B == 0)
return 1;
long x;
if(B&1)//odd
{
    x = ((A%C)*(Mod(A,B-1,C)%C))%C;
}else{
    x = Mod(A,B/2,C);
    x = (x*x)%C;
}

return (int)(x+C)%C;

}

But this works

int Solution::Mod(int A, int B, int C) {
if(A == 0)
return 0;
if (B == 0)
return 1;
long x;
if(B&1)//odd
{
    x = A%C;
    x = (x*(Mod(A,B-1,C)%C))%C;
}else{
    x = Mod(A,B/2,C);
    x = (x*x)%C;
}

return (int)(x+C)%C;

}

Why?


#2

Cause A,B and C are int, x is long, x gets converted to int.