Advertisement
Matrix_code

mod int

Mar 23rd, 2021
1,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. template<const int &MOD>
  5. struct modint {
  6.     int val;
  7.  
  8.     modint(int64_t v = 0) {
  9.         if (v < 0) v = v % MOD + MOD;
  10.         if (v >= MOD) v %= MOD;
  11.         val = int(v);
  12.     }
  13.  
  14.     modint(uint64_t v) {
  15.         if (v >= MOD) v %= MOD;
  16.         val = int(v);
  17.     }
  18.  
  19.     modint(int v) : modint(int64_t(v)) {}
  20.     modint(unsigned v) : modint(uint64_t(v)) {}
  21.  
  22.     explicit operator int() const { return val; }
  23.     explicit operator unsigned() const { return val; }
  24.     explicit operator int64_t() const { return val; }
  25.     explicit operator uint64_t() const { return val; }
  26.     explicit operator double() const { return val; }
  27.     explicit operator long double() const { return val; }
  28.  
  29.     modint& operator+=(const modint &other) {
  30.         val -= MOD - other.val;
  31.         if (val < 0) val += MOD;
  32.         return *this;
  33.     }
  34.  
  35.     modint& operator-=(const modint &other) {
  36.         val -= other.val;
  37.         if (val < 0) val += MOD;
  38.         return *this;
  39.     }
  40.  
  41.     static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
  42. #if !defined(_WIN32) || defined(_WIN64)
  43.         return unsigned(x % m);
  44. #endif
  45.         // Optimized mod for Codeforces 32-bit machines.
  46.         // x must be less than 2^32 * m for this to work, so that x / m fits in an unsigned 32-bit int.
  47.         unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
  48.         unsigned quot, rem;
  49.         asm("divl %4\n"
  50.             : "=a" (quot), "=d" (rem)
  51.             : "d" (x_high), "a" (x_low), "r" (m));
  52.         return rem;
  53.     }
  54.  
  55.     modint& operator*=(const modint &other) {
  56.         val = fast_mod(uint64_t(val) * other.val);
  57.         return *this;
  58.     }
  59.  
  60.     modint& operator/=(const modint &other) {
  61.         return *this *= other.inv();
  62.     }
  63.  
  64.     friend modint operator+(const modint &a, const modint &b) { return modint(a) += b; }
  65.     friend modint operator-(const modint &a, const modint &b) { return modint(a) -= b; }
  66.     friend modint operator*(const modint &a, const modint &b) { return modint(a) *= b; }
  67.     friend modint operator/(const modint &a, const modint &b) { return modint(a) /= b; }
  68.  
  69.     modint& operator++() {
  70.         val = val == MOD - 1 ? 0 : val + 1;
  71.         return *this;
  72.     }
  73.  
  74.     modint& operator--() {
  75.         val = val == 0 ? MOD - 1 : val - 1;
  76.         return *this;
  77.     }
  78.  
  79.     modint operator++(int) { modint before = *this; ++*this; return before; }
  80.     modint operator--(int) { modint before = *this; --*this; return before; }
  81.  
  82.     modint operator-() const {
  83.         return val == 0 ? 0 : MOD - val;
  84.     }
  85.  
  86.     friend bool operator==(const modint &a, const modint &b) { return a.val == b.val; }
  87.     friend bool operator!=(const modint &a, const modint &b) { return a.val != b.val; }
  88.     friend bool operator<(const modint &a, const modint &b) { return a.val < b.val; }
  89.     friend bool operator>(const modint &a, const modint &b) { return a.val > b.val; }
  90.     friend bool operator<=(const modint &a, const modint &b) { return a.val <= b.val; }
  91.     friend bool operator>=(const modint &a, const modint &b) { return a.val >= b.val; }
  92.  
  93.     static const int SAVE_INV = int(1e6) + 5;
  94.     static modint save_inv[SAVE_INV];
  95.  
  96.     static void prepare_inv() {
  97.         // Make sure MOD is prime, which is necessary for the inverse algorithm below.
  98.         for (int64_t p = 2; p * p <= MOD; p += p % 2 + 1)
  99.             assert(MOD % p != 0);
  100.  
  101.         save_inv[0] = 0;
  102.         save_inv[1] = 1;
  103.  
  104.         for (int i = 2; i < SAVE_INV; i++)
  105.             save_inv[i] = save_inv[MOD % i] * (MOD - MOD / i);
  106.     }
  107.  
  108.     modint inv() const {
  109.         if (save_inv[1] == 0)
  110.             prepare_inv();
  111.  
  112.         if (val < SAVE_INV)
  113.             return save_inv[val];
  114.  
  115.         modint product = 1;
  116.         int v = val;
  117.  
  118.         while (v >= SAVE_INV) {
  119.             product *= MOD - MOD / v;
  120.             v = MOD % v;
  121.         }
  122.  
  123.         return product * save_inv[v];
  124.     }
  125.  
  126.     modint pow(int64_t p) const {
  127.         if (p < 0)
  128.             return inv().pow(-p);
  129.  
  130.         modint a = *this, result = 1;
  131.  
  132.         while (p > 0) {
  133.             if (p & 1)
  134.                 result *= a;
  135.  
  136.             p >>= 1;
  137.  
  138.             if (p > 0)
  139.                 a *= a;
  140.         }
  141.  
  142.         return result;
  143.     }
  144.  
  145.     friend ostream& operator<<(ostream &os, const modint &m) {
  146.         return os << m.val;
  147.     }
  148. };
  149. const int mod = 1e9+7;
  150. using mint = modint<mod>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement