标签:运算 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