結果

問題 No.2046 Ans Mod? Mod Ans!
ユーザー asumo0729asumo0729
提出日時 2022-08-19 22:17:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,141 ms / 4,000 ms
コード長 3,255 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 87,332 KB
実行使用メモリ 111,276 KB
最終ジャッジ日時 2023-08-22 11:43:15
合計ジャッジ時間 10,966 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 164 ms
89,700 KB
testcase_01 AC 179 ms
89,664 KB
testcase_02 AC 229 ms
89,588 KB
testcase_03 AC 420 ms
89,436 KB
testcase_04 AC 344 ms
89,544 KB
testcase_05 AC 417 ms
89,532 KB
testcase_06 AC 348 ms
90,324 KB
testcase_07 AC 403 ms
89,440 KB
testcase_08 AC 210 ms
89,900 KB
testcase_09 AC 212 ms
89,988 KB
testcase_10 AC 211 ms
89,964 KB
testcase_11 AC 225 ms
90,232 KB
testcase_12 AC 222 ms
89,884 KB
testcase_13 AC 563 ms
99,860 KB
testcase_14 AC 485 ms
97,584 KB
testcase_15 AC 713 ms
104,720 KB
testcase_16 AC 738 ms
105,140 KB
testcase_17 AC 642 ms
102,972 KB
testcase_18 AC 927 ms
102,968 KB
testcase_19 AC 635 ms
102,664 KB
testcase_20 AC 1,141 ms
111,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from operator import itemgetter
from collections import defaultdict, deque
import heapq
from heapq import heapify, heappop, _heapify_max, heappush
from bisect import bisect_left, bisect_right
import math
import itertools
import copy

stdin=sys.stdin
#sys.setrecursionlimit(10 ** 7)
## import pypyjit
## pypyjit.set_param('max_unroll_recursion=-1')

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()
Yp=lambda:print('Yes')
Np=lambda:print('No')
inf = 1 << 60
mod = 10 ** 9 + 7
mod = 998244353
eps = 1e-9
sortkey1 = itemgetter(0)
sortkey2 = lambda x: (x[0], x[1])

#####segfunc#####
def segfunc(x, y):
    return x + y
#################

"""
min::min(x,y) float('inf)
max::max(x,y) -float('inf')
区間和::x+y  0
区間積::x*y  1
最大公約数::math.gcd(x,y)  0
"""

#####ide_ele#####
ide_ele = 0
#################

class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        self.n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (self.n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(self.n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

    def all_prod(self):
        return self.query(0,self.n)

###############################################################
NN = 2 * 10 ** 5
N = ip()
A = lp()
ans = 0
seg = SegTree([0 for _ in range(NN + 5)], segfunc,ide_ele)
seg1 = SegTree([0 for _ in range(NN + 5)], segfunc,ide_ele)

for a in A:
    seg.update(a, 1)
    seg1.update(a, a)

for a in A:
    ans += seg.query(a + 1, NN + 2) * a
    ans -= seg1.query(a, NN + 2)
    for k in range(1, NN + 1):
        if a * k > NN:
            break
        r = min((k + 1) * a, NN + 1)
        ans += seg.query(k * a, r) * k * a
print(ans)
0