Hello everyone i just came over to this problem somewhere that if i have two very very large numbers (10^9) and i want to multiply under some modulo mod...
then we have snippet like this works :
ll mulmod(ll a, ll b, ll mod)
{
ll res = 0; // Initialize result
a = a % mod;
while (b > 0)
{
// If b is odd, add 'a' to result
if (b % 2 == 1)
res = (res + a) % mod;
// Multiply 'a' with 2
a = (a * 2) % mod;
// Divide b by 2
b /= 2;
}
// Return result
return res % mod;
}
But how to handle this when b is large negative number of the order (-10^9).. plzz help thanks
10^18 is in range of long long
but more over when b is negative a*b < 0 and direct modulo with mod yeilds wrong ans plzz correct me if iam wrong
Well if direct mod is wrong, what is your definition of mod?
i just readed somewhere that
this is the correct way what do u think mate ?
it's good if you always want the positive mod
ya for negative b above statement is valid iff (a*b) is >= -10^18 but when exceeds the range overflow occurs so thats why i want to handle the overflow cases for negative values of b pllz help if anyone know how to modify above code for b <0.
Just use
__int128
Dear DaysGone,
The following is a possible solution when
b < 0
.The test program returns two equal numbers when
b < 0
.Best wishes,
but if i give an input 2 -2 1000000007
your code gives first by normal way (-4) % 1e9+7 which is -4
but if i call your function mulmod(2,-2,1000000007) which is (-4) % 1e9+7 the answer comes out to be -4 again.
but actual ans is 1000000003
sorry bro but i dnt think this is the correct way what do u think ?
Dear DaysGone,
Thanks for your comment.
Please refer to the following www.cplusplus.com Forum blog for more information about the behavior of the binary
a % b
operator in C++, which used to be different from the behavior of the modulus operatora mod b
in discrete mathematics whena < 0
.If you would like to have the
mulmod( a, b, mod )
function behaving like the modulus operator in mathematics, whose range is the positive integer interval[ 0, mod - 1 ]
andmod > 0
, then it is necessary and sufficient to update the return statement of the function in the aforementioned suggested code toBest wishes,
yes ur absoultely right thaankss for ur help
With pleasure.
Please check the latest update to the suggested solution.
This update considers all possible cases for the signs of
a
andb
, so thatnegative
stores the sign ofa * b
, and the sum-and-shift loop uses the absolute values|a|
and|b|
. It also includes an assertion statement to validate thatc > 0
in accordance with the mathematical definition of the modulus operator. The absolute values ofa
andb
are also swapped before thefor
loop when|a| < |b|
so as to reduce the number of iterations.Please note that it is possible in C++ to compute
a % b
without run-time error whenb < 0
, but a division-by-zero exception is thrown by the executable code whenb == 0
. In discrete mathematics, the constraintb > 0
must be satisfied for the remainder functiona mod b
.Best wishes,