結果
問題 |
No.3187 Mingle
|
ユーザー |
![]() |
提出日時 | 2025-06-23 03:34:36 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,762 ms / 2,500 ms |
コード長 | 1,885 bytes |
コンパイル時間 | 367 ms |
コンパイル使用メモリ | 81,664 KB |
実行使用メモリ | 96,640 KB |
最終ジャッジ日時 | 2025-08-30 07:44:14 |
合計ジャッジ時間 | 39,921 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
import sys input = sys.stdin.readline # エラトステネスの篩を用いた素因数分解・約数列挙 MAX=3*10**5+10 # 使いたい最大値を指定 # 線形篩。エラトステネスの篩を線形にできる。 Sieve=[-1]*MAX Primes=[] for i in range(2,MAX): if Sieve[i]==-1: Primes.append(i) Sieve[i]=i for p in Primes: if p*i>=MAX or p>Sieve[i]: break else: Sieve[p*i]=p # 素因数分解 def fact(x): D=dict() while x!=1: k=Sieve[x] if k in D: D[k]+=1 else: D[k]=1 x//=k return D # 約数列挙 def faclist(x): LIST=[1] while x!=1: k=Sieve[x] count=0 while x%k==0: count+=1 x//=k LIST2=[] for l in LIST: for i in range(1,count+1): LIST2.append(l*k**i) LIST+=LIST2 return LIST N,mod=map(int,input().split()) seg_el=1<<((N*2+1).bit_length()) # Segment treeの台の要素数 SEG=[0]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化 def getvalue(n,seg_el): # 一点の値を取得 i=n+seg_el ANS=0 ANS=SEG[i] i>>=1# 子ノードへ while i!=0: ANS+=SEG[i] i>>=1 return ANS def updates(l,r,x): # 区間[l,r)を更新. L=l+seg_el R=r+seg_el while L<R: if L & 1: SEG[L]=(SEG[L]+x)%mod L+=1 if R & 1: R-=1 SEG[R]=(SEG[R]+x)%mod L>>=1 R>>=1 for i in range(3,N+1): L=faclist(i) INV=pow(i,mod-2,mod) G=getvalue(i,seg_el) ANS=(G*INV+1)*pow(i-len(L),mod-2,mod)*i%mod #print(i,L,G,ANS) for x in L[1:]: updates(i+1,i+x,ANS) #print([getvalue(i,seg_el) for i in range(1,N+1)]) print(ANS)