Блог пользователя Jeanne_Dark

Автор Jeanne_Dark, история, 8 лет назад, По-английски
//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?

  • Проголосовать: нравится
  • +46
  • Проголосовать: не нравится

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

Auto comment: topic has been updated by Jeanne_Dark (previous revision, new revision, compare).

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Auto comment: topic has been updated by Jeanne_Dark (previous revision, new revision, compare).

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

What does this code do? I can understand it looking at the code.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

Auto comment: topic has been updated by Jeanne_Dark (previous revision, new revision, compare).

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +23 Проголосовать: не нравится

Doesn't work in C++11/C++14/C++17 compiler:

Bug 6375 x=2244993833869413467 y=4161159085823384362 m=4189503386722597813 Mul=4205452683108168173 Correct=15949296385570360
If you add if (tmp >= mo) tmp -= mo; it passes 10 million random tests (with numbers bounded by 2^62).
  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +28 Проголосовать: не нравится

    Thank you! BTW, can you tell me why it works?

    • »
      »
      »
      8 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +59 Проголосовать: не нравится

      Because tmp equals to x*y-something*m by modulo 264. If x*y-something*m is smaller than 263 by absolute value, it means that tmp equals to x*y-something*m and hence tmp equals to x*y by modulo m. So if tmp is small enough relative to m, tmp after single reduction will be equal to x*y%m. The code chooses something approximately equal to round(x*y/m) and so ensures that x*y-something*m is small. If |something-(x*y/m)| < 1, than |x*y-something*m| < m. If we assume that 0 <= x < m and 0 <= y < m, the value x*y/m is no larger than m and we need around 64 bit of relative precision of long double to ensure |something-(x*y/m)| < 1. If x or y can be larger than m the code will not work.

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +13 Проголосовать: не нравится

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.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

This fails on test

a = 786428809343111346 b = 435484497063066236 m = 1045750209045681

This code outputs -7195096558989062423.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +33 Проголосовать: не нравится

Oh my contribution...LOL

  • »
    »
    8 лет назад, скрыть # ^ |
    ← Rev. 3  
    Проголосовать: нравится +35 Проголосовать: не нравится

    If you thought about the appearence of the topic, I think there could be upvotes.

    What I did when I read this topic:

    1. Saw { (a)(code) (with many)brackets & characters; }, without any description.

    2. Saw the questions about it, though I had no idea what it does.

    3. Downvoted the topic.

    Still, Calculate x*y%mo in python if 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) % mo for 64-bit a, b, mo on 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.

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

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.