I was performing (a*b)%m but all a,b and m are of order 10^18.
It is not possible to multiply a*b or even multiply (a%m * b%m) because m is also of order 10^18.
I believe it would be unnecessary to convert them into strings and then multiply them as strings,take the mod and then return back as long long int.
I got this question and there the accepted solution was to split my operands. (Please see the linked post). However I didn't understand the explanation of bit shifting that he mentioned.
I wrote a function to calculate the product of a and b modulo mod.
/*
a=a1+k*a2
b=b1+k*b2
(a1+k*a2)*(b1+k*b2) % c = a1*b1 % c + k*a1*b2 % c + k*a2*b1 % c + k*k*a2*b2 % c
*/
ull MAM(ull a,ull b,ull mod)//multiply and mod;ull: unsigned long long
{
ull a1,a2,b1,b2;
ull k=4294967296; //2^32
a1=a%k;
a2=a/k;
b1=b%k;
b2=b/k;
ull ans = (a1*b1)%mod + (((a1*k)%mod)*b2) %mod + (((k*a2)%mod)*b1)%mod + (((k*k)%mod)*((a2*b2)%mod))%mod;
return ans;
}
But this wouldn't work without the shifts.Someone please explain the bit shifts that answer is talking about.