結果

問題 No.2046 Ans Mod? Mod Ans!
ユーザー 👑 箱
提出日時 2021-07-18 21:51:24
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,904 ms / 4,000 ms
コード長 1,906 bytes
コンパイル時間 15,901 ms
コンパイル使用メモリ 433,488 KB
実行使用メモリ 65,424 KB
最終ジャッジ日時 2023-08-22 11:35:47
合計ジャッジ時間 31,990 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 297 ms
55,880 KB
testcase_01 AC 297 ms
55,852 KB
testcase_02 AC 296 ms
56,040 KB
testcase_03 AC 323 ms
56,656 KB
testcase_04 AC 320 ms
56,620 KB
testcase_05 AC 317 ms
56,620 KB
testcase_06 AC 318 ms
56,540 KB
testcase_07 AC 323 ms
56,772 KB
testcase_08 AC 390 ms
59,092 KB
testcase_09 AC 395 ms
59,184 KB
testcase_10 AC 401 ms
59,264 KB
testcase_11 AC 456 ms
59,504 KB
testcase_12 AC 450 ms
59,320 KB
testcase_13 AC 1,192 ms
63,360 KB
testcase_14 AC 951 ms
62,776 KB
testcase_15 AC 1,299 ms
63,560 KB
testcase_16 AC 1,304 ms
63,048 KB
testcase_17 AC 1,184 ms
62,772 KB
testcase_18 AC 1,113 ms
62,880 KB
testcase_19 AC 1,292 ms
63,376 KB
testcase_20 AC 1,904 ms
65,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

// sum of A_j * (A_i / A_j)
fun helper(a: Array<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 PrintWriter.solve() {
    val n = nextInt()
    val a = Array(n) { nextInt() }
    a.sortDescending()
    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 main() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0