modint by cjl
struct m{
int x;m(int o=0){x=o;}m(lll o){x=o%mod;}m&operator+=(m a){return(x+=a.x)%=mod,*this;}m&operator-=(m a){return(x+=mod-a.x)%=mod,*this;}
m&operator*=(m a){return(x=1ll*x*a.x%mod),*this;}m&operator^=( int b){m a=*this;x=1;while(b)(b&1)&&(*this*=a,1),a*=a,b>>=1;return*this;}
m&operator/=(m a){return*this*=(a^=mod-2);}friend m operator+(m a,m b){return a+=b;}friend m operator-(m a,m b){return a-=b;}
friend m operator*(m a,m b){return a*=b;}friend m operator/(m a,m b){return a/=b;}friend m operator^(m a, int b){return a^=b;}
m operator-(){return 0-*this;}bool operator==(const m b)const{return x==b.x;}
};
仿照 atcoder library 写的
typedef long long LL;
template<unsigned P> struct modint{
unsigned v; using ull=unsigned long long;
template<class T> modint(T x=0):v((x%int(P)+int(P))%int(P)){}
modint operator-()const{return modint(P-v);}
modint inv()const{return qpow(*this,LL(P)-2);}
modint&operator+=(const modint&rhs){if(v+=rhs.v,v>=P) v-=P; return *this;}
modint&operator-=(const modint&rhs){return *this+=-rhs;}
modint&operator*=(const modint&rhs){v=ull(v)*rhs.v%P; return *this;}
modint&operator/=(const modint&rhs){return *this*=rhs.inv();}
friend int raw(const modint&self){return self.v;}
friend modint qpow(modint a,LL b){modint r=1;for(;b;b>>=1,a*=a) if(b&1) r*=a; return r;}
friend modint operator+(modint lhs,const modint&rhs){return lhs+=rhs;}
friend modint operator-(modint lhs,const modint&rhs){return lhs-=rhs;}
friend modint operator*(modint lhs,const modint&rhs){return lhs*=rhs;}
friend modint operator/(modint lhs,const modint&rhs){return lhs/=rhs;}
};
using Z=modint<998244353>;
注意那个 int(P)
非常重要。。。
精简版:但好像并没有
template<unsigned P> struct modint{
unsigned v; using ull=unsigned long long; using Z=modint;
template<class T> modint(T x=0):v((x%int(P)+int(P))%int(P)){}
Z operator-()const{return Z(P-v);}
Z inv()const{return qpow(*this,LL(P)-2);}
Z&operator+=(Z b){if(v+=b.v,v>=P) v-=P; return *this;}
Z&operator-=(Z b){return *this+=-b;}
Z&operator*=(Z b){v=ull(v)*b.v%P; return *this;}
Z&operator/=(Z b){return *this*=b.inv();}
friend int raw(const Z&self){return self.v;}
friend Z qpow(Z a,LL b){Z r=1;for(;b;b>>=1,a*=a) if(b&1) r*=a; return r;}
friend Z operator+(Z a,Z b){return a+=b;}
friend Z operator-(Z a,Z b){return a-=b;}
friend Z operator*(Z a,Z b){return a*=b;}
friend Z operator/(Z a,Z b){return a/=b;}
};
使用例子:
int main(){
// #ifdef LOCAL
// freopen("input.in","r",stdin);
// #endif
Z a=-1,b=-2;
printf("%d %d\n",raw(a),raw(b));
printf("%d\n",raw(a+b));
printf("%d\n",raw(a-b));
printf("%d\n",raw(a*b));
printf("%d\n",raw(a/b));
printf("%d\n",raw(qpow(a,2)));
return 0;
}
标签:return,rhs,模板,operator,modint,friend,const
From: https://www.cnblogs.com/caijianhong/p/template-modint.html