結果

問題 No.2046 Ans Mod? Mod Ans!
ユーザー 👑 箱星箱星
提出日時 2022-05-04 00:22:08
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 2,008 ms / 4,000 ms
コード長 1,451 bytes
コンパイル時間 15,851 ms
コンパイル使用メモリ 450,488 KB
実行使用メモリ 97,348 KB
最終ジャッジ日時 2024-05-09 17:18:21
合計ジャッジ時間 30,303 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 318 ms
55,752 KB
testcase_01 AC 322 ms
55,836 KB
testcase_02 AC 319 ms
55,588 KB
testcase_03 AC 345 ms
56,868 KB
testcase_04 AC 347 ms
56,800 KB
testcase_05 AC 346 ms
56,852 KB
testcase_06 AC 333 ms
56,708 KB
testcase_07 AC 345 ms
56,676 KB
testcase_08 AC 451 ms
85,088 KB
testcase_09 AC 471 ms
85,324 KB
testcase_10 AC 471 ms
85,344 KB
testcase_11 AC 501 ms
88,072 KB
testcase_12 AC 491 ms
87,940 KB
testcase_13 AC 1,189 ms
93,116 KB
testcase_14 AC 1,056 ms
92,244 KB
testcase_15 AC 1,400 ms
94,116 KB
testcase_16 AC 1,482 ms
93,884 KB
testcase_17 AC 1,298 ms
93,772 KB
testcase_18 AC 1,209 ms
92,396 KB
testcase_19 AC 1,275 ms
93,928 KB
testcase_20 AC 2,008 ms
97,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import kotlin.math.*

// sum of A_j * (A_i / A_j)
fun helper(a: List<Int>): Long {
    val n = a.size
    val max = a.maxOrNull()!!
    val sqrt = sqrt(max.toDouble()).roundToInt()
    val l = Array(sqrt) { 0L }
    val bit = BIT(max + 1)
    var ans = 0L
    for (i in 0 until n) {
        if (a[i] < sqrt) {
            ans += l[a[i]]
        } else {
            for (k in 0..max step a[i]) {
                ans += k * bit.sum(k until minOf(k + a[i], max + 1))
            }
        }
        bit.add(a[i], 1)
        for (j in 1 until sqrt) {
            l[j] += a[i] / j * j.toLong()
        }
    }
    return ans
}

fun main() {
    val n = readInt()
    val a = readInts().sortedDescending()
    var ans = helper(a)
    for (i in 0 until n) {
        ans += (2 * i - n + 1L) * a[i]
    }
    println(ans)
}

class BIT(private val n: Int) {
    private val data = LongArray(n) { 0 }

    fun add(i: Int, x: Long) {
        var p = i + 1
        while (p <= n) {
            data[p - 1] += x
            p += p and -p
        }
    }

    fun sum(range: IntRange): Long {
        val l = range.first
        val r = range.last + 1
        return sum(r) - sum(l)
    }

    private fun sum(i: Int): Long {
        var r = i
        var s = 0L
        while(r > 0) {
            s += data[r - 1]
            r -= r and -r
        }
        return s
    }
}

fun readInt() = readln().toInt()
fun readInts() = readln().split(" ").map { it.toInt() }
0