|
| 1 | +#include <iostream> |
| 2 | + |
| 3 | +class mint{ |
| 4 | + int n; |
| 5 | + int mod; |
| 6 | +public: |
| 7 | + mint(int a = 0, int b = 1e9+7){ |
| 8 | + this->n = a; |
| 9 | + this->mod = b; |
| 10 | + this->n %= b; |
| 11 | + }; |
| 12 | + friend std::ostream& operator<<(std::ostream& out, const mint& m); |
| 13 | + friend std::istream& operator>>(std::istream& in, mint& m); |
| 14 | + |
| 15 | + friend mint operator+(const mint &a, const mint &b); |
| 16 | + friend mint operator-(const mint &a, const mint &b); |
| 17 | + friend mint operator*(const mint &a, const mint &b); |
| 18 | + friend mint operator/(const mint &a, const mint &b); |
| 19 | + friend mint operator%(const mint &a, const mint &b); |
| 20 | + |
| 21 | + friend mint operator+(const mint &a, int b); |
| 22 | + friend mint operator+(int b, const mint &a); |
| 23 | + friend mint operator-(const mint &a, int b); |
| 24 | + friend mint operator-(int b, const mint &a); |
| 25 | + friend mint operator*(const mint &a, int b); |
| 26 | + friend mint operator*(int b, const mint &a); |
| 27 | + friend mint operator/(const mint &a, int b); |
| 28 | + friend mint operator/(int b, const mint &a); |
| 29 | + friend mint operator%(const mint &a, int b); |
| 30 | + friend mint operator%(int b, const mint &a); |
| 31 | + |
| 32 | + friend mint operator+(const mint &a, long long b); |
| 33 | + friend mint operator+(long long b, const mint &a); |
| 34 | + friend mint operator-(const mint &a, long long b); |
| 35 | + friend mint operator-(long long b, const mint &a); |
| 36 | + friend mint operator*(const mint &a, long long b); |
| 37 | + friend mint operator*(long long b, const mint &a); |
| 38 | + friend mint operator/(const mint &a, long long b); |
| 39 | + friend mint operator/(long long b, const mint &a); |
| 40 | + friend mint operator%(const mint &a, long long b); |
| 41 | + friend mint operator%(long long b, const mint &a); |
| 42 | + |
| 43 | + //comparisons just based on n |
| 44 | + friend bool operator==(const mint &a, const mint &b); |
| 45 | + friend bool operator!=(const mint &a, const mint &b); |
| 46 | + friend bool operator<(const mint &a, const mint &b); |
| 47 | + friend bool operator>(const mint &a, const mint &b); |
| 48 | + friend bool operator<=(const mint &a, const mint &b); |
| 49 | + friend bool operator>=(const mint &a, const mint &b); |
| 50 | + |
| 51 | + friend bool operator==(const mint &a, int b); |
| 52 | + friend bool operator==(int b, const mint &a); |
| 53 | + friend bool operator!=(const mint &a, int b); |
| 54 | + friend bool operator!=(int b, const mint &a); |
| 55 | + friend bool operator<(const mint &a, int b); |
| 56 | + friend bool operator<(int b, const mint &a); |
| 57 | + friend bool operator>(const mint &a, int b); |
| 58 | + friend bool operator>(int b, const mint &a); |
| 59 | + friend bool operator<=(const mint &a, int b); |
| 60 | + friend bool operator<=(int b, const mint &a); |
| 61 | + friend bool operator>=(const mint &a, int b); |
| 62 | + friend bool operator>=(int b, const mint &a); |
| 63 | + |
| 64 | + friend bool operator==(const mint &a, long long b); |
| 65 | + friend bool operator==(long long b, const mint &a); |
| 66 | + friend bool operator!=(const mint &a, long long b); |
| 67 | + friend bool operator!=(long long b, const mint &a); |
| 68 | + friend bool operator<(const mint &a, long long b); |
| 69 | + friend bool operator<(long long b, const mint &a); |
| 70 | + friend bool operator>(const mint &a, long long b); |
| 71 | + friend bool operator>(long long b, const mint &a); |
| 72 | + friend bool operator<=(const mint &a, long long b); |
| 73 | + friend bool operator<=(long long b, const mint &a); |
| 74 | + friend bool operator>=(const mint &a, long long b); |
| 75 | + friend bool operator>=(long long b, const mint &a); |
| 76 | + |
| 77 | + //prefix |
| 78 | + mint& operator++(); |
| 79 | + mint& operator--(); |
| 80 | + //postfix |
| 81 | + mint operator++(int); |
| 82 | + mint operator--(int); |
| 83 | + |
| 84 | + //only adds the n |
| 85 | + mint& operator+=(const mint& a); |
| 86 | + mint& operator-=(const mint& a); |
| 87 | + mint& operator*=(const mint& a); |
| 88 | + mint& operator/=(const mint& a); |
| 89 | + mint& operator%=(const mint& a); |
| 90 | + |
| 91 | + mint& operator+=(int a); |
| 92 | + mint& operator-=(int a); |
| 93 | + mint& operator*=(int a); |
| 94 | + mint& operator/=(int a); |
| 95 | + mint& operator%=(int a); |
| 96 | + |
| 97 | + mint& operator+=(long long a); |
| 98 | + mint& operator-=(long long a); |
| 99 | + mint& operator*=(long long a); |
| 100 | + mint& operator/=(long long a); |
| 101 | + mint& operator%=(long long a); |
| 102 | + |
| 103 | + |
| 104 | + mint operator-() const; |
| 105 | + mint operator!() const; |
| 106 | + |
| 107 | + explicit operator bool() const; |
| 108 | + explicit operator int() const; |
| 109 | + explicit operator long long() const; |
| 110 | +private: |
| 111 | + int _inv(int a) const; |
| 112 | +}; |
| 113 | + |
| 114 | +int mint::_inv(int a) const{ |
| 115 | + int x = 1, y = 0, x1 = 0, y1 = 1, a1 = a, b1 = this->mod; |
| 116 | + while(b1){ |
| 117 | + int q = a1/b1; |
| 118 | + int t = x; |
| 119 | + x = x1, x1 = t-q*x1; |
| 120 | + t = y; |
| 121 | + y = y1, y1 = t-q*y1; |
| 122 | + t = a1; |
| 123 | + a1 = b1, b1 = a1-q*b1; |
| 124 | + } |
| 125 | + return (x%this->mod + mod)%this->mod; |
| 126 | +} |
| 127 | + |
| 128 | +std::ostream& operator<<(std::ostream& out, const mint& m){ |
| 129 | + out << m.n; |
| 130 | + return out; |
| 131 | +} |
| 132 | + |
| 133 | +std::istream& operator>>(std::istream& in, mint& m){ |
| 134 | + in >> m.n; |
| 135 | + m.n %= m.mod; |
| 136 | + return in; |
| 137 | +} |
| 138 | + |
| 139 | +mint operator+(const mint &a, const mint &b){ |
| 140 | + //both mint must have the same modulus |
| 141 | + if(a.mod != b.mod) |
| 142 | + throw 1; |
| 143 | + return mint(((a.n+b.n)%a.mod+a.mod)%a.mod, a.mod); |
| 144 | +} |
| 145 | + |
| 146 | +mint operator-(const mint &a, const mint &b){ |
| 147 | + //both mint must have the same modulus |
| 148 | + if(a.mod != b.mod) |
| 149 | + throw 1; |
| 150 | + return mint(((a.n-b.n)%a.mod+a.mod)%a.mod, a.mod); |
| 151 | +} |
| 152 | + |
| 153 | +mint operator*(const mint &a, const mint &b){ |
| 154 | + //both mint must have the same modulus |
| 155 | + if(a.mod != b.mod) |
| 156 | + throw 1; |
| 157 | + return mint(((a.n*b.n)%a.mod+a.mod)%a.mod, a.mod); |
| 158 | +} |
| 159 | + |
| 160 | +mint operator/(const mint &a, const mint &b){ |
| 161 | + //both mint must have the same modulus |
| 162 | + if(a.mod != b.mod) |
| 163 | + throw 1; |
| 164 | + |
| 165 | + return mint(((a.n*(b._inv(b.n)))%a.mod+a.mod)%a.mod, a.mod); |
| 166 | +} |
| 167 | + |
| 168 | +mint operator%(const mint &a, const mint &b){ |
| 169 | + //no TrUe mathematical meaning |
| 170 | + //just does a blind a.n % b.n |
| 171 | + return mint(a.n%b.n, a.mod); |
| 172 | +} |
| 173 | + |
| 174 | +mint operator+(const mint &a, int b){ |
| 175 | + return mint(((a.n+b)%a.mod + a.mod)%a.mod, a.mod); |
| 176 | +} |
| 177 | +mint operator+(int b, const mint &a){ return a + b; } |
| 178 | +mint operator-(const mint &a, int b){ |
| 179 | + return mint(((a.n-b)%a.mod + a.mod)%a.mod, a.mod); |
| 180 | +} |
| 181 | +mint operator-(int b, const mint &a){ return a - b; } |
| 182 | +mint operator*(const mint &a, int b){ |
| 183 | + return mint(((a.n*b)%a.mod + a.mod)%a.mod, a.mod); |
| 184 | +} |
| 185 | +mint operator*(int b, const mint &a){ return a*b; } |
| 186 | +mint operator/(const mint &a, int b){ |
| 187 | + return mint(((a.n*a._inv(b))%a.mod + a.mod)%a.mod, a.mod); |
| 188 | +} |
| 189 | +mint operator/(int b, const mint &a){ |
| 190 | + return mint(((b*a._inv(a.n))%a.mod+a.mod)%a.mod, a.mod); |
| 191 | +} |
| 192 | +mint operator%(const mint &a, int b){ |
| 193 | + return mint(((a.n%b)%a.mod + a.mod)%a.mod, a.mod); |
| 194 | +} |
| 195 | +mint operator%(int b, const mint &a){ |
| 196 | + return mint(((b%a.n)%a.mod + a.mod)%a.mod, a.mod); |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | +mint operator+(const mint &a, long long b){ |
| 201 | + return mint(((a.n+b)%a.mod + a.mod)%a.mod, a.mod); |
| 202 | +} |
| 203 | +mint operator+(long long b, const mint &a){ return a + b; } |
| 204 | +mint operator-(const mint &a, long long b){ |
| 205 | + return mint(((a.n-b)%a.mod + a.mod)%a.mod, a.mod); |
| 206 | +} |
| 207 | +mint operator-(long long b, const mint &a){ return a - b; } |
| 208 | +mint operator*(const mint &a, long long b){ |
| 209 | + return mint(((a.n*b)%a.mod + a.mod)%a.mod, a.mod); |
| 210 | +} |
| 211 | +mint operator*(long long b, const mint &a){ return a*b; } |
| 212 | +mint operator/(const mint &a, long long b){ |
| 213 | + return mint(((a.n*a._inv(b))%a.mod + a.mod)%a.mod, a.mod); |
| 214 | +} |
| 215 | +mint operator/(long long b, const mint &a){ |
| 216 | + return mint(((b*a._inv(a.n))%a.mod+a.mod)%a.mod, a.mod); |
| 217 | +} |
| 218 | +mint operator%(const mint &a, long long b){ |
| 219 | + return mint(((a.n%b)%a.mod + a.mod)%a.mod, a.mod); |
| 220 | +} |
| 221 | +mint operator%(long long b, const mint &a){ |
| 222 | + return mint(((b%a.n)%a.mod + a.mod)%a.mod, a.mod); |
| 223 | +} |
| 224 | + |
| 225 | +mint mint::operator-() const{ |
| 226 | + return mint(-this->n + this->mod, this->mod); |
| 227 | +} |
| 228 | +mint mint::operator!() const{ |
| 229 | + return mint(!(this->n), this->mod); |
| 230 | +} |
| 231 | + |
| 232 | +mint::operator bool() const{ |
| 233 | + return !!(this->n); |
| 234 | +} |
| 235 | +mint::operator int() const{ |
| 236 | + return this->n; |
| 237 | +} |
| 238 | +mint::operator long long() const{ |
| 239 | + return (long long)this->n; |
| 240 | +} |
| 241 | + |
| 242 | +bool operator==(const mint &a, const mint &b){ return a.n == b.n; } |
| 243 | +bool operator!=(const mint &a, const mint &b){ return a.n != b.n; } |
| 244 | +bool operator<(const mint &a, const mint &b){ return a.n < b.n; } |
| 245 | +bool operator>(const mint &a, const mint &b){return a.n > b.n; } |
| 246 | +bool operator<=(const mint &a, const mint &b){return a.n <= b.n; } |
| 247 | +bool operator>=(const mint &a, const mint &b){return a.n >= b.n; } |
| 248 | + |
| 249 | +bool operator==(const mint &a, int b){ return a.n == b; } |
| 250 | +bool operator==(int b, const mint &a){ return b == a.n; } |
| 251 | +bool operator!=(const mint &a, int b){ return a.n != b; } |
| 252 | +bool operator!=(int b, const mint &a){ return b != a.n; } |
| 253 | +bool operator<(const mint &a, int b){ return a.n < b; } |
| 254 | +bool operator<(int b, const mint &a){ return b < a.n; } |
| 255 | +bool operator>(const mint &a, int b){ return a.n > b; } |
| 256 | +bool operator>(int b, const mint &a){ return b > a.n; } |
| 257 | +bool operator<=(const mint &a, int b){ return a.n <= b; } |
| 258 | +bool operator<=(int b, const mint &a){ return b <= a.n; } |
| 259 | +bool operator>=(const mint &a, int b){ return a.n >= b; } |
| 260 | +bool operator>=(int b, const mint &a){ return b >= a.n; } |
| 261 | + |
| 262 | +bool operator==(const mint &a, long long b){ return a.n == b; } |
| 263 | +bool operator==(long long b, const mint &a){ return b == a.n; } |
| 264 | +bool operator!=(const mint &a, long long b){ return a.n != b; } |
| 265 | +bool operator!=(long long b, const mint &a){ return b != a.n; } |
| 266 | +bool operator<(const mint &a, long long b){ return a.n < b; } |
| 267 | +bool operator<(long long b, const mint &a){ return b < a.n; } |
| 268 | +bool operator>(const mint &a, long long b){ return a.n > b; } |
| 269 | +bool operator>(long long b, const mint &a){ return b > a.n; } |
| 270 | +bool operator<=(const mint &a, long long b){ return a.n <= b; } |
| 271 | +bool operator<=(long long b, const mint &a){ return b <= a.n; } |
| 272 | +bool operator>=(const mint &a, long long b){ return a.n >= b; } |
| 273 | +bool operator>=(long long b, const mint &a){ return b >= a.n; } |
| 274 | + |
| 275 | + |
| 276 | +mint& mint::operator++(){ |
| 277 | + (this->n)++; |
| 278 | + this->n %= this->mod; |
| 279 | + return *this; |
| 280 | +} |
| 281 | +mint mint::operator++(int){ |
| 282 | + mint temp(*this); |
| 283 | + ++(this->n); |
| 284 | + return temp; |
| 285 | +} |
| 286 | +mint& mint::operator--(){ |
| 287 | + (this->n)--; |
| 288 | + this->n %= this->mod; |
| 289 | + return *this; |
| 290 | +} |
| 291 | +mint mint::operator--(int){ |
| 292 | + mint temp(*this); |
| 293 | + --(this->n); |
| 294 | + return temp; |
| 295 | +} |
| 296 | + |
| 297 | + |
| 298 | +mint& mint::operator+=(const mint& a){ |
| 299 | + this->n += a.n; |
| 300 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 301 | + return *this; |
| 302 | +} |
| 303 | +mint& mint::operator-=(const mint& a){ |
| 304 | + this->n -= a.n; |
| 305 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 306 | + return *this; |
| 307 | +} |
| 308 | +mint& mint::operator*=(const mint& a){ |
| 309 | + this->n *= a.n; |
| 310 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 311 | + return *this; |
| 312 | +} |
| 313 | +mint& mint::operator/=(const mint& a){ |
| 314 | + this->n *= this->_inv(a.n); |
| 315 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 316 | + return *this; |
| 317 | +} |
| 318 | +mint& mint::operator%=(const mint& a){ |
| 319 | + this->n %= a.n; |
| 320 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 321 | + return *this; |
| 322 | +} |
| 323 | + |
| 324 | +mint& mint::operator+=(int a){ |
| 325 | + this->n += a; |
| 326 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 327 | + return *this; |
| 328 | +} |
| 329 | +mint& mint::operator-=(int a){ |
| 330 | + this->n -= a; |
| 331 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 332 | + return *this; |
| 333 | +} |
| 334 | +mint& mint::operator*=(int a){ |
| 335 | + this->n *= a; |
| 336 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 337 | + return *this; |
| 338 | +} |
| 339 | +mint& mint::operator/=(int a){ |
| 340 | + this->n *= this->_inv(a); |
| 341 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 342 | + return *this; |
| 343 | +} |
| 344 | +mint& mint::operator%=(int a){ |
| 345 | + this->n %= a; |
| 346 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 347 | + return *this; |
| 348 | +} |
| 349 | + |
| 350 | +mint& mint::operator+=(long long a){ |
| 351 | + this->n += a; |
| 352 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 353 | + return *this; |
| 354 | +} |
| 355 | +mint& mint::operator-=(long long a){ |
| 356 | + this->n -= a; |
| 357 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 358 | + return *this; |
| 359 | +} |
| 360 | +mint& mint::operator*=(long long a){ |
| 361 | + this->n *= a; |
| 362 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 363 | + return *this; |
| 364 | +} |
| 365 | +mint& mint::operator/=(long long a){ |
| 366 | + this->n *= this->_inv(a); |
| 367 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 368 | + return *this; |
| 369 | +} |
| 370 | +mint& mint::operator%=(long long a){ |
| 371 | + this->n %= a; |
| 372 | + this->n = (this->n%this->mod + this->mod)%this->mod; |
| 373 | + return *this; |
| 374 | +} |
0 commit comments