Advertisement
Ne-Biolog

Untitled

Jan 26th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. template<int MOD>
  2. struct ModInt {
  3. static const int Mod = MOD;
  4. unsigned x;
  5. ModInt() : x(0) {}
  6. ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
  7. ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
  8. int get() const { return (int)x; }
  9.  
  10. ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }
  11. ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
  12. ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
  13. ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
  14.  
  15. ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
  16. ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
  17. ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
  18. ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
  19.  
  20. ModInt inverse() const {
  21. signed a = x, b = MOD, u = 1, v = 0;
  22. while(b) {
  23. signed t = a / b;
  24. a -= t * b; std::swap(a, b);
  25. u -= t * v; std::swap(u, v);
  26. }
  27. if(u < 0) u += Mod;
  28. ModInt res; res.x = (unsigned)u;
  29. return res;
  30. }
  31.  
  32. bool operator==(ModInt that) const { return x == that.x; }
  33. bool operator!=(ModInt that) const { return x != that.x; }
  34. ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
  35.  
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement