結果

問題 No.2046 Ans Mod? Mod Ans!
ユーザー 👑 箱星箱星
提出日時 2021-07-19 00:53:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,008 ms / 4,000 ms
コード長 1,461 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 109,568 KB
最終ジャッジ日時 2024-05-09 17:16:49
合計ジャッジ時間 11,006 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,096 KB
testcase_01 AC 38 ms
52,480 KB
testcase_02 AC 37 ms
52,608 KB
testcase_03 AC 67 ms
73,472 KB
testcase_04 AC 71 ms
72,704 KB
testcase_05 AC 67 ms
74,112 KB
testcase_06 AC 69 ms
73,344 KB
testcase_07 AC 69 ms
74,240 KB
testcase_08 AC 127 ms
78,080 KB
testcase_09 AC 117 ms
77,928 KB
testcase_10 AC 123 ms
78,152 KB
testcase_11 AC 143 ms
77,824 KB
testcase_12 AC 129 ms
77,952 KB
testcase_13 AC 845 ms
88,396 KB
testcase_14 AC 641 ms
85,524 KB
testcase_15 AC 1,112 ms
94,396 KB
testcase_16 AC 1,132 ms
94,108 KB
testcase_17 AC 979 ms
92,224 KB
testcase_18 AC 809 ms
91,008 KB
testcase_19 AC 1,046 ms
92,160 KB
testcase_20 AC 2,008 ms
109,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://shakayami.hatenablog.com/entry/2021/01/01/044946
class fenwick_tree():
    n=1
    data=[0 for i in range(n)]
    def __init__(self,N):
        self.n=N
        self.data=[0 for i in range(N)]
    def add(self,p,x):
        assert 0<=p<self.n,"0<=p<n,p={0},n={1}".format(p,self.n)
        p+=1
        while(p<=self.n):
            self.data[p-1]+=x
            p+=p& -p
    def sum(self,l,r):
        assert (0<=l and l<=r and r<=self.n),"0<=l<=r<=n,l={0},r={1},n={2}".format(l,r,self.n)
        return self.sum0(r)-self.sum0(l)
    def sum0(self,r):
        s=0
        while(r>0):
            s+=self.data[r-1]
            r-=r&-r
        return s

#x^2<=Kとなる最大のxを返す
def sqrt(K):
    low=0
    high=K+10
    while(high-low>1):
        mid=(high+low)//2
        if mid*mid<=K:
            low=mid
        else:
            high=mid
    return low

def solve(A):
    N=len(A)
    maxA=max(A)
    sqrtmaxA=sqrt(maxA)
    L=[0 for i in range(sqrtmaxA)]
    BIT=fenwick_tree(maxA+1)
    ans=0
    for i in range(N):
        if A[i]<sqrtmaxA:
            ans+=L[A[i]]
        else:
            for k in range(0,maxA+1,A[i]):
                ans+=k*BIT.sum(k,min(k+A[i],maxA+1))
        BIT.add(A[i],1)
        for j in range(1,sqrtmaxA):
            L[j]+=(A[i]//j)*j
    return ans

N = int(input())
A = list(map(int, input().split()))
A.sort()
A.reverse()
ans = solve(A)
for i in range(N):
    ans += (2 * i - N + 1) * A[i]
print(ans)
0