結果

問題 No.2705 L to R Graph (Another ver.)
ユーザー hirayuu_ychirayuu_yc
提出日時 2024-02-11 15:56:56
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,877 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 607,204 KB
最終ジャッジ日時 2024-02-12 16:35:11
合計ジャッジ時間 80,614 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
59,716 KB
testcase_01 AC 39 ms
59,460 KB
testcase_02 AC 40 ms
59,844 KB
testcase_03 AC 43 ms
61,976 KB
testcase_04 AC 40 ms
59,716 KB
testcase_05 AC 43 ms
61,972 KB
testcase_06 AC 43 ms
61,980 KB
testcase_07 AC 44 ms
59,844 KB
testcase_08 AC 1,712 ms
488,584 KB
testcase_09 MLE -
testcase_10 AC 1,336 ms
416,760 KB
testcase_11 AC 1,497 ms
474,504 KB
testcase_12 AC 1,085 ms
357,212 KB
testcase_13 AC 642 ms
260,328 KB
testcase_14 MLE -
testcase_15 AC 283 ms
137,564 KB
testcase_16 AC 1,089 ms
359,504 KB
testcase_17 AC 535 ms
256,492 KB
testcase_18 AC 1,627 ms
506,524 KB
testcase_19 AC 859 ms
318,680 KB
testcase_20 AC 588 ms
260,940 KB
testcase_21 AC 1,391 ms
451,712 KB
testcase_22 AC 1,377 ms
446,288 KB
testcase_23 MLE -
testcase_24 TLE -
testcase_25 MLE -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 MLE -
testcase_29 MLE -
testcase_30 MLE -
testcase_31 MLE -
testcase_32 MLE -
testcase_33 MLE -
testcase_34 TLE -
testcase_35 MLE -
testcase_36 MLE -
testcase_37 TLE -
testcase_38 MLE -
testcase_39 MLE -
testcase_40 MLE -
testcase_41 TLE -
testcase_42 MLE -
testcase_43 TLE -
testcase_44 TLE -
testcase_45 TLE -
testcase_46 TLE -
testcase_47 TLE -
testcase_48 TLE -
testcase_49 TLE -
testcase_50 TLE -
testcase_51 TLE -
testcase_52 MLE -
権限があれば一括ダウンロードができます

ソースコード

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=300
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