数学模运算类
Posted purinliang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数学模运算类相关的知识,希望对你有一定的参考价值。
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;
以上是关于数学模运算类的主要内容,如果未能解决你的问题,请参考以下文章