自动取余int板子

template<int mod>
struct ModInt {
    
    
    const static int MD = mod;
    int x;
    ModInt(ll x = 0): x(x % MD) {
    
    }
    int get() {
    
     return x; }

    ModInt operator + (const ModInt &that) const {
    
     int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); }
    ModInt operator - (const ModInt &that) const {
    
     int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); }
    ModInt operator * (const ModInt &that) const {
    
     return ModInt((long long)x * that.x % MD); }
    ModInt operator / (const ModInt &that) const {
    
     return *this * that.inverse(); }

    ModInt operator += (const ModInt &that) {
    
     x += that.x; if (x >= MD) x -= MD; }
    ModInt operator -= (const ModInt &that) {
    
     x -= that.x; if (x < 0) x += MD; }
    ModInt operator *= (const ModInt &that) {
    
     x = (long long)x * that.x % MD; }
    ModInt operator /= (const ModInt &that) {
    
     *this = *this / that; }

    ModInt inverse() const {
    
    
        int a = x, b = MD, u = 1, v = 0;
        while(b) {
    
    
            int t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        if(u < 0) u += MD;
        return u;
    }

};
typedef ModInt<7340033> mint;

猜你喜欢

转载自blog.csdn.net/xxmy7/article/details/113620436