Fast Multiplication Modulo Algorithm

principle:

32+16+4=52

 

1 LL qmul(LL x, LL y, LL mod) { // Multiplication prevents overflow, if p * p does not explode LL, it can be multiplied directly; O(1) multiplication or converted into binary addition
 2  // Fast multiplication modulo algorithm 
3  
4      LL ret = 0 ;
 5      while (y) {
 6          if (y & 1 )
 7              ret = (ret + x) % mod;
 8          x = x * 2 % mod;
 9          y >>= 1 ;
 10      }
 11      return ret;
 12 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324722587&siteId=291194637