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

Автор Boring_Day, история, 4 года назад, По-английски

hey i want to calculate something i needed some attention so i wrote must read blog.

let's say i want to calculate a*b % P where 0<= a,b <= 1e9 and p = 1e9+7

how to calculate this by using only integer number i don't want to use long long as many stupid people do.

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

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

Weird that you call people that use long long stupid, but you can solve it in $$$O(\log(\min(a, b))$$$ using only addition with binary "exponentiation". The operator here that is to be applied repeatedly is addition instead of multiplication.

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

    I am not the only one many people suggest to not use long long because it causes MLE/TLE in many problems.

    Btw Where is the code?

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

      But don't you think it is unnecessary to use a much more complex algorithm to do such a simple problem and use more time?

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

      MLE happens only when you store too many 64-bit integers in a data structure, or use too many 64 bit-integers in a recursive function (which contributes to stack size). The argument that they cause TLE is more or less outdated since most judges have 64-bit architecture now and the main culprit behind the TLE was slow 64-bit multiplication on 32-bit machines.

      If you use the algorithm I suggested on 32-bit machines, it will be slower than 64-bit multiplication. I would have recommended you to write code for this idea yourself, but if you haven't seen binary exponentiation before, you can go through this.

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

        i am getting too many compilation errors like

        Spoiler
        code
  • »
    »
    4 года назад, скрыть # ^ |
     
    Проголосовать: нравится -13 Проголосовать: не нравится

    On a serious note, how does this perform compared to Barrett reduction/Montgomery reduction?