結果

問題 No.2705 L to R Graph (Another ver.)
ユーザー hirayuu_ychirayuu_yc
提出日時 2024-02-11 09:14:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,561 ms / 3,000 ms
コード長 1,877 bytes
コンパイル時間 242 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 289,472 KB
最終ジャッジ日時 2024-02-12 16:33:49
合計ジャッジ時間 58,107 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 39 ms
59,460 KB
testcase_03 AC 41 ms
59,460 KB
testcase_04 AC 40 ms
59,716 KB
testcase_05 AC 41 ms
59,460 KB
testcase_06 AC 40 ms
59,460 KB
testcase_07 AC 41 ms
59,460 KB
testcase_08 AC 1,132 ms
246,880 KB
testcase_09 AC 1,306 ms
265,964 KB
testcase_10 AC 917 ms
233,792 KB
testcase_11 AC 1,047 ms
255,280 KB
testcase_12 AC 783 ms
207,468 KB
testcase_13 AC 479 ms
156,092 KB
testcase_14 AC 1,250 ms
258,176 KB
testcase_15 AC 217 ms
99,932 KB
testcase_16 AC 761 ms
208,004 KB
testcase_17 AC 366 ms
142,112 KB
testcase_18 AC 1,202 ms
248,564 KB
testcase_19 AC 610 ms
183,304 KB
testcase_20 AC 418 ms
147,152 KB
testcase_21 AC 1,008 ms
247,480 KB
testcase_22 AC 978 ms
245,424 KB
testcase_23 AC 1,442 ms
280,840 KB
testcase_24 AC 1,457 ms
282,520 KB
testcase_25 AC 1,413 ms
278,344 KB
testcase_26 AC 1,449 ms
282,972 KB
testcase_27 AC 1,362 ms
272,696 KB
testcase_28 AC 1,348 ms
270,712 KB
testcase_29 AC 1,318 ms
267,428 KB
testcase_30 AC 1,357 ms
271,832 KB
testcase_31 AC 1,331 ms
269,456 KB
testcase_32 AC 1,361 ms
272,540 KB
testcase_33 AC 1,381 ms
270,700 KB
testcase_34 AC 1,442 ms
282,528 KB
testcase_35 AC 1,345 ms
269,600 KB
testcase_36 AC 1,449 ms
281,288 KB
testcase_37 AC 1,485 ms
286,956 KB
testcase_38 AC 1,361 ms
269,468 KB
testcase_39 AC 1,470 ms
282,988 KB
testcase_40 AC 1,422 ms
277,216 KB
testcase_41 AC 1,522 ms
289,216 KB
testcase_42 AC 1,443 ms
281,268 KB
testcase_43 AC 1,524 ms
289,468 KB
testcase_44 AC 1,522 ms
289,468 KB
testcase_45 AC 1,509 ms
289,468 KB
testcase_46 AC 1,523 ms
289,468 KB
testcase_47 AC 1,525 ms
289,468 KB
testcase_48 AC 1,534 ms
289,468 KB
testcase_49 AC 1,542 ms
289,468 KB
testcase_50 AC 1,516 ms
289,468 KB
testcase_51 AC 1,545 ms
289,468 KB
testcase_52 AC 1,561 ms
289,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0