結果

問題 No.2705 L to R Graph (Another ver.)
ユーザー hirayuu_ychirayuu_yc
提出日時 2024-02-20 12:21:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,978 ms / 3,000 ms
コード長 1,971 bytes
コンパイル時間 426 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 290,852 KB
最終ジャッジ日時 2024-03-16 02:56:27
合計ジャッジ時間 58,837 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 40 ms
58,944 KB
testcase_03 AC 42 ms
58,944 KB
testcase_04 AC 43 ms
59,712 KB
testcase_05 AC 43 ms
58,944 KB
testcase_06 AC 43 ms
58,944 KB
testcase_07 AC 40 ms
58,944 KB
testcase_08 AC 1,237 ms
246,744 KB
testcase_09 AC 1,469 ms
265,880 KB
testcase_10 AC 1,005 ms
233,176 KB
testcase_11 AC 1,135 ms
255,048 KB
testcase_12 AC 814 ms
206,844 KB
testcase_13 AC 492 ms
155,084 KB
testcase_14 AC 1,383 ms
259,388 KB
testcase_15 AC 223 ms
100,124 KB
testcase_16 AC 824 ms
207,252 KB
testcase_17 AC 391 ms
143,048 KB
testcase_18 AC 1,283 ms
249,244 KB
testcase_19 AC 657 ms
183,064 KB
testcase_20 AC 446 ms
145,508 KB
testcase_21 AC 1,096 ms
247,248 KB
testcase_22 AC 1,075 ms
245,192 KB
testcase_23 AC 1,588 ms
279,924 KB
testcase_24 AC 1,615 ms
281,944 KB
testcase_25 AC 1,564 ms
277,544 KB
testcase_26 AC 1,606 ms
282,340 KB
testcase_27 AC 1,518 ms
271,824 KB
testcase_28 AC 1,483 ms
270,104 KB
testcase_29 AC 1,466 ms
267,576 KB
testcase_30 AC 1,506 ms
271,104 KB
testcase_31 AC 1,464 ms
269,072 KB
testcase_32 AC 1,518 ms
271,820 KB
testcase_33 AC 1,521 ms
270,164 KB
testcase_34 AC 1,594 ms
281,940 KB
testcase_35 AC 1,496 ms
269,108 KB
testcase_36 AC 1,568 ms
280,756 KB
testcase_37 AC 1,612 ms
286,488 KB
testcase_38 AC 1,494 ms
269,116 KB
testcase_39 AC 1,576 ms
282,372 KB
testcase_40 AC 1,554 ms
276,336 KB
testcase_41 AC 1,668 ms
288,900 KB
testcase_42 AC 1,571 ms
280,608 KB
testcase_43 AC 1,677 ms
290,852 KB
testcase_44 AC 1,650 ms
290,848 KB
testcase_45 AC 1,667 ms
290,852 KB
testcase_46 AC 1,643 ms
290,852 KB
testcase_47 AC 1,630 ms
290,848 KB
testcase_48 AC 1,975 ms
290,824 KB
testcase_49 AC 1,978 ms
290,824 KB
testcase_50 AC 1,943 ms
290,824 KB
testcase_51 AC 1,929 ms
290,820 KB
testcase_52 AC 1,930 ms
290,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
	def cumsum(x,d=1):
	    for i in range(d,len(x)):
	        x[i]+=x[i-d]
	
	N,P=map(int,input().split())
	ans=0
	f=[0,0]
	fact=1
	for i in range(2,N+1):
	    f.append((pow(N,N-i,P)*fact)%P)
	    fact*=N-i
	    fact%=P
	dist=[0]*N
	cum=0
	pat=0
	for i in range(N-2,0,-1):
	    cum+=f[i+2]
	    pat+=cum
	    cum%=P
	    pat%=P
	    dist[i]=pat
	backet=100
	pats=[0]*N*2
	det=[[0]*N*2 for i in range(backet+1)]
	for i in range(1,N+1):
	    for j in range(N+1):
	        if i*j>=N:
	            break
	        if j==0:
	            pats[1]+=N-i+1
	            pats[i*(j+1)]-=N-i+1
	            continue
	        if j>backet:
	            cnt=0
	            for k in range(i*j+1,i*(j+1),j):
	                pats[k]+=1
	                cnt+=1
	            pats[i*(j+1)]-=cnt
	        else:
	            det[j][i*j+1]+=1
	            cnt=(i*(j+1)-2)//j-i+1
	            det[j][min(N*2-1,i*j+1+cnt*j)]-=1
	            pats[i*(j+1)]-=cnt
	for i in range(1,backet+1):
	    cumsum(det[i],i)
	    for j in range(N*2):
	        pats[j]+=det[i][j]
	cumsum(pats)
	for i in range(N-1):
	    pats[i]=N*(N+1)//2-pats[i]
	    ans+=pats[i]*dist[i]
	    ans%=P
	cumsum(f)
	div=[[] for i in range(N+1)]
	cum=[[0] for i in range(N+1)]
	for i in range(1,N+1):
	    for j in range(i,N+1,i):
	        div[j].append(i)
	        cum[i].append(cum[i][-1]+f[j])
	for i in range(1,N+1):
	    gcd=[0]*len(div[i])
	    for j in range(len(div[i])-1,-1,-1):
	        gcd[j]+=N//div[i][j]
	        for k in range(j):
	            if div[i][j]%div[i][k]==0:
	                gcd[k]-=gcd[j]
	        pl=cum[div[i][j]][-1]-cum[div[i][j]][i//div[i][j]]
	        plcnt=len(cum[div[i][j]])-1-i//div[i][j]
	        mi=cum[div[i][j]][-1]-cum[div[i][j]][i//div[i][j]-1]+f[i-1]*(i//div[i][j]-1)
	        micnt=plcnt+1+i//div[i][j]-1
	        ans+=(pl+f[-1]*(micnt-plcnt)-mi)*gcd[j]
	        if j==0:
	            ans+=(pl+f[-1]*(micnt-plcnt)-mi)*N*(N-1)//2
	        ans%=P
	print((ans*N*(N-1))%P)
main()
0