結果

問題 No.2754 Cumulate and Drop
ユーザー titia
提出日時 2024-05-13 01:27:28
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 753 ms / 2,000 ms
コード長 1,272 bytes
コンパイル時間 15,458 ms
コンパイル使用メモリ 440,704 KB
実行使用メモリ 102,876 KB
最終ジャッジ日時 2024-12-20 09:21:19
合計ジャッジ時間 27,047 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toLong() }

    val mod = 998244353L

    val fact = LongArray(500000) { 1L }
    for (i in 1 until fact.size) {
        fact[i] = fact[i - 1] * i % mod
    }

    val factInv = LongArray(fact.size) { i ->
        if (i == fact.lastIndex) fact.last().modPow(mod - 2, mod)
        else 0L
    }
    for (i in fact.lastIndex - 1 downTo 0) {
        factInv[i] = factInv[i + 1] * (i + 1) % mod
    }

    fun combi(a: Int, b: Int): Long {
        return if (b in 0..a) {
            fact[a] * factInv[b] % mod * factInv[a - b] % mod
        } else {
            0L
        }
    }

    fun cat(i: Int, j: Int): Long {
        return (combi(i + j + 1, j) - 2 * combi(i + j, j - 1)% mod + mod) % mod
    }

    var ans = 0L
    a.reversed().forEachIndexed { i, ai ->
        ans = (ans + ai.toLong() * cat(n - 1, i) % mod) % mod
    }

    println(ans)
}

private fun Long.modPow(exponent: Long, modulus: Long): Long {
    var result = 1L
    var exp = exponent
    var base = this % modulus
    while (exp > 0) {
        if (exp and 1 == 1L) {
            result = result * base % modulus
        }
        base = base * base % modulus
        exp = exp shr 1
    }
    return result
}
0