class Modulo_Error(Exception): pass class Modulo(): __slots__=["a","n"] def __init__(self,a,n): self.a=a%n self.n=n def __str__(self): return "{} (mod {})".format(self.a,self.n) def __repr__(self): return self.__str__() #+,- def __pos__(self): return self def __neg__(self): return Modulo(-self.a,self.n) #等号,不等号 def __eq__(self,other): if isinstance(other,Modulo): return (self.a==other.a) and (self.n==other.n) elif isinstance(other,int): return (self-other).a==0 def __neq__(self,other): return not(self==other) def __le__(self,other): a,p=self.a,self.n b,q=other.a,other.n return (a-b)%q==0 and p%q==0 def __ge__(self,other): return other<=self def __lt__(self,other): return (self<=other) and (self!=other) def __gt__(self,other): return (self>=other) and (self!=other) def __contains__(self,val): return val%self.n==self.a #加法 def __add__(self,other): if isinstance(other,Modulo): if self.n!=other.n: raise Modulo_Error("異なる法同士の演算です.") return Modulo(self.a+other.a,self.n) elif isinstance(other,int): return Modulo(self.a+other,self.n) def __radd__(self,other): if isinstance(other,int): return Modulo(self.a+other,self.n) def __iadd__(self,other): if isinstance(other,Modulo): if self.n!=other.n: raise Modulo_Error("異なる法同士の演算です.") self.a+=other.a if self.a>=self.n: self.a-=self.n elif isinstance(other,int): self.a+=other if self.a>=self.n: self.a-=self.n return self #減法 def __sub__(self,other): return self+(-other) def __rsub__(self,other): if isinstance(other,int): return -self+other def __isub__(self,other): if isinstance(other,Modulo): if self.n!=other.n: raise Modulo_Error("異なる法同士の演算です.") self.a-=other.a if self.a<0: self.a+=self.n elif isinstance(other,int): self.a-=other if self.a<0: self.a+=self.n return self #乗法 def __mul__(self,other): if isinstance(other,Modulo): if self.n!=other.n: raise Modulo_Error("異なる法同士の演算です.") return Modulo(self.a*other.a,self.n) elif isinstance(other,int): return Modulo(self.a*other,self.n) def __rmul__(self,other): if isinstance(other,int): return Modulo(self.a*other,self.n) def __imul__(self,other): if isinstance(other,Modulo): if self.n!=other.n: raise Modulo_Error("異なる法同士の演算です.") self.a*=other.a elif isinstance(other,int): self.a*=other self.a%=self.n return self #Modulo逆数 def inverse(self): return self.Modulo_Inverse() def Modulo_Inverse(self): s,t=1,0 a,b=self.a,self.n while b: q,a,b=a//b,b,a%b s,t=t,s-q*t if a!=1: raise Modulo_Error("{}の逆数が存在しません".format(self)) else: return Modulo(s,self.n) #除法 def __truediv__(self,other): return self*(other.Modulo_Inverse()) def __rtruediv__(self,other): return other*(self.Modulo_Inverse()) #累乗 def __pow__(self,other): if isinstance(other,int): u=abs(other) r=Modulo(pow(self.a,u,self.n),self.n) if other>=0: return r else: return r.Modulo_Inverse() else: b,n=other.a,other.n if pow(self.a,n,self.n)!=1: raise Modulo_Error("矛盾なく定義できません.") else: return self**b #================================================== def Order(X): """ X の位数を求める. つまり, X^k=[1] を満たす最小の正整数 k を求める. """ phi=1 N=X.n e=(N&(-N)).bit_length()-1 if e>0: phi=1<<(e-1) N>>=e else: phi=1 e=0 while N%3==0: e+=1 N//=3 if e>0: phi*=pow(3,e-1)*2 flag=0 p=5 while p*p<=N: if N%p==0: e=0 while N%p==0: e+=1 N//=p phi*=pow(p,e-1)*(p-1) p+=2+2*flag flag^=1 if N>1: phi*=N-1 a=float("inf") k=1 while k*k<=phi: if phi%k==0: if k