Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template<int mod> struct ModInt{
- int x;
- ModInt () : x(0) {}
- ModInt (int64_t x) : x(x >= 0 ? x % mod : (mod - -x % mod) % mod) {}
- ModInt &operator += (const ModInt &p){
- if ((x += p.x) >= mod) x -= mod;
- return *this;
- }
- ModInt &operator -= (const ModInt &p) {
- if ((x += mod - p.x) >= mod) x -= mod;
- return *this;
- }
- ModInt &operator *= (const ModInt &p) {
- x = (int64_t) x * p.x % mod;
- return *this;
- }
- ModInt &operator /= (const ModInt &p) {
- *this *= p.inverse();
- return *this;
- }
- ModInt &operator ^= (int64_t p) {
- ModInt res = 1;
- for (; p; p >>= 1) {
- if (p & 1) res *= *this;
- *this *= *this;
- }
- return *this = res;
- }
- ModInt operator - () const { return ModInt(-x); }
- ModInt operator + (const ModInt &p) const { return ModInt(*this) += p; }
- ModInt operator - (const ModInt &p) const { return ModInt(*this) -= p; }
- ModInt operator * (const ModInt &p) const { return ModInt(*this) *= p; }
- ModInt operator / (const ModInt &p) const { return ModInt(*this) /= p; }
- ModInt operator ^ (int64_t p) const { return ModInt(*this) ^= p; }
- bool operator == (const ModInt &p) const { return x == p.x; }
- bool operator != (const ModInt &p) const { return x != p.x; }
- explicit operator int() const { return x; }
- ModInt &operator = (const int p) { x = p; return *this;}
- ModInt inverse() const {
- int a = x, b = mod, u = 1, v = 0, t;
- while (b > 0) {
- t = a / b;
- a -= t * b;
- std::swap(a, b);
- u -= t * v;
- std::swap(u, v);
- }
- return ModInt(u);
- }
- friend std::ostream & operator << (std::ostream &stream, const ModInt<mod> &p) {
- return stream << p.x;
- }
- friend std::istream & operator >> (std::istream &stream, ModInt<mod> &a) {
- int64_t x;
- stream >> x;
- a = ModInt<mod>(x);
- return stream;
- }
- };
- int main() {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement