//Calculate x*y%mo in python
long long Mul(long long x,long long y,long long mo)
{
long long tmp=((unsigned long long)x*y-(unsigned long long)((long double)x/mo*y+1.0e-8)*mo);
return (tmp<0)?(tmp+mo):tmp;
}
I learned that from internet, but I'm wondering whether it's safe or not.
And I also want to know that:
If I replace
return (tmp<0)?(tmp+mo):tmp;
by
return tmp;
Will it be wrong?








Auto comment: topic has been updated by Jeanne_Dark (previous revision, new revision, compare).
Auto comment: topic has been updated by Jeanne_Dark (previous revision, new revision, compare).
What does this code do? I can understand it looking at the code.
Calculate x*y%mo in python
Auto comment: topic has been updated by Jeanne_Dark (previous revision, new revision, compare).
Doesn't work in C++11/C++14/C++17 compiler:
If you addif (tmp >= mo) tmp -= mo;it passes 10 million random tests (with numbers bounded by 2^62).Thank you! BTW, can you tell me why it works?
Because
tmpequals tox*y-something*mby modulo 264. Ifx*y-something*mis smaller than 263 by absolute value, it means thattmpequals tox*y-something*mand hencetmpequals tox*yby modulom. So iftmpis small enough relative tom,tmpafter single reduction will be equal tox*y%m. The code choosessomethingapproximately equal toround(x*y/m)and so ensures thatx*y-something*mis small. If|something-(x*y/m)| < 1, than|x*y-something*m| < m. If we assume that0 <= x < mand0 <= y < m, the valuex*y/mis no larger thanmand we need around 64 bit of relative precision oflong doubleto ensure|something-(x*y/m)| < 1. Ifxorycan be larger thanmthe code will not work.Thank you!
I spent some time to read it, I understand now!
I have used this particular snippet in a lot of questions now and I have got an AC in all of them.
There is a wikipedia explanation for the same : Link
Check out the second implementation. Basically the above code snippet works due to the computer architecture, that the multiplications done keep the least significant bits of the product.
Oh great, I start to understand it now.
Thank you!
This fails on test
a = 786428809343111346 b = 435484497063066236 m = 1045750209045681This code outputs
-7195096558989062423.I think we should let 0<=a<m and 0<=b<m.
With
a %= mo,b %= moandtmp %= moin the end it works well on random tests.Yea, and I get ACs by it. But it took me time to understand it.
Oh my contribution...LOL
If you thought about the appearence of the topic, I think there could be upvotes.
What I did when I read this topic:
Saw
{ (a)(code) (with many)brackets & characters; }, without any description.Saw the questions about it, though I had no idea what it does.
Downvoted the topic.
Still,
Calculate x*y%mo in pythonif still a bad description. I don't understand why Python is mentioned when the code is in C++. But now it's easier guess what you meant here.I think, starting the topic with smth like "Hello, I saw the following code for calculating
(a * b) % mofor 64-bita, b, moon the Internet: [code] Is there a test where this code is wrong?" would be much better.Hope this will help you to write your posts better.
Thank u, Sir!
Why do you add 10^-8? Why not just do something like x*y — (x/mod*y)*mod? I am confused on how this improves it.
I think it is an epsilon
How would it help with anything though? I just don't see the purpose in adding an epsilon here.
In c++,
Let a=0.99999
(long long)a == 0
but what we want is:
(long long)(a + eps) == 1