码迷,mamicode.com
首页 > 其他好文 > 详细

【数学】模运算类

时间:2021-06-02 20:32:48      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:运算   typedef   数学   str   mint   template   const   pen   min   

const int MOD = 1e9 + 7;

struct ModularIntegers {
    int num;
    template <typename T>
    ModularIntegers(const T& x) {
        if(x >= 0 && x < MOD) num = x;
        else {
            num = x % MOD;
            if(num < 0) num += MOD;
        }
    }
    ModularIntegers operator+(const ModularIntegers &x) const {
        int res = this->num + x.num;
        if(res >= MOD) res -= MOD;
        return ModularIntegers(res);
    }
    ModularIntegers& operator+=(const ModularIntegers &x) {
        this->num += x.num;
        if(this->num >= MOD) this->num -= MOD;
        return *this;
    }
    ModularIntegers operator-(const ModularIntegers &x) const {
        int res = this->num - x.num;
        if(res < 0) res += MOD;
        return ModularIntegers(res);
    }
    ModularIntegers& operator-=(const ModularIntegers &x) {
        this->num -= x.num;
        if(this->num < 0) this->num += MOD;
        return *this;
    }
    ModularIntegers operator*(const ModularIntegers &x) const {
        int res = 1LL * this->num * x.num % MOD;
        return ModularIntegers(res);
    }
    ModularIntegers& operator*=(const ModularIntegers &x) {
        this->num = 1LL * this->num * x.num % MOD;
        return *this;
    }
    ModularIntegers pow(ll x)const {
        ModularIntegers res(1), cur(this->num);
        while(x) {
            if(x & 1)
                res = res * cur;
            cur *= cur;
            x >>= 1;
        }
        return res;
    }
    ModularIntegers inv()const {
        return pow(MOD - 2);
    }
    ModularIntegers operator/(const ModularIntegers &x) const {
        return (*this) * x.inv();
    }
    ModularIntegers& operator/=(const ModularIntegers &x) {
        this->num = ((*this) * x.inv()).num;
        return *this;
    }
};

void _RD(ModularIntegers &x) {
    scanf("%d", &x.num);
}

void _WT(const ModularIntegers &x) {
    printf("%d", x.num);
}

typedef ModularIntegers mint;

【数学】模运算类

标签:运算   typedef   数学   str   mint   template   const   pen   min   

原文地址:https://www.cnblogs.com/purinliang/p/14838826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!