結果

問題 No.2046 Ans Mod? Mod Ans!
ユーザー 👑 箱星箱星
提出日時 2021-07-19 00:53:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,461 bytes
コンパイル時間 411 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 25,312 KB
最終ジャッジ日時 2024-05-09 17:17:09
合計ジャッジ時間 9,017 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,880 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 44 ms
11,136 KB
testcase_04 AC 41 ms
11,008 KB
testcase_05 AC 44 ms
11,136 KB
testcase_06 AC 41 ms
11,008 KB
testcase_07 AC 43 ms
11,008 KB
testcase_08 AC 374 ms
12,928 KB
testcase_09 AC 446 ms
12,928 KB
testcase_10 AC 499 ms
13,056 KB
testcase_11 AC 674 ms
13,184 KB
testcase_12 AC 565 ms
13,184 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

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