結果

問題 No.2383 Naphthol
ユーザー 👑 箱
提出日時 2023-03-02 18:08:25
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 361 ms / 2,000 ms
コード長 1,971 bytes
コンパイル時間 18,025 ms
コンパイル使用メモリ 447,600 KB
実行使用メモリ 57,304 KB
最終ジャッジ日時 2023-10-17 14:27:40
合計ジャッジ時間 26,275 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 319 ms
54,428 KB
testcase_01 AC 320 ms
54,440 KB
testcase_02 AC 324 ms
57,088 KB
testcase_03 AC 315 ms
54,436 KB
testcase_04 AC 319 ms
54,420 KB
testcase_05 AC 319 ms
54,436 KB
testcase_06 AC 319 ms
54,436 KB
testcase_07 AC 318 ms
54,420 KB
testcase_08 AC 320 ms
54,420 KB
testcase_09 AC 319 ms
54,416 KB
testcase_10 AC 356 ms
57,288 KB
testcase_11 AC 361 ms
57,304 KB
testcase_12 AC 337 ms
57,092 KB
testcase_13 AC 351 ms
57,304 KB
testcase_14 AC 356 ms
57,304 KB
testcase_15 AC 325 ms
57,088 KB
testcase_16 AC 330 ms
57,096 KB
testcase_17 AC 322 ms
54,444 KB
testcase_18 AC 357 ms
57,292 KB
testcase_19 AC 319 ms
56,492 KB
testcase_20 AC 360 ms
57,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fun main() {
    val (n, k) = readln().split(" ").map { it.toInt() }
    if (n == 1) {
        println(arrayOf(1, 1, 3, 3, 3, 1, 1)[k])
        return
    }
    var ans = binom(2 * n + 4L, k.toLong())
    if (n % 2 == 0 && k % 2 == 0) {
        ans += ModInt(3) * binom(n + 2L, k / 2L)
    } else if (n % 2 != 0 && k % 2 == 0) {
        ans += binom(n + 1L, k / 2L) + binom(n + 1L, (k - 2) / 2L) + ModInt(2) * binom(n + 2L, k / 2L)
    } else if (n % 2 != 0 && k % 2 != 0) {
        ans += ModInt(2) * binom(n + 1L, (k - 1) / 2L)
    }
    ans /= ModInt(4)
    println(ans)
}

// region ModInt
class ModInt(x: Long) {
    companion object {
        const val MOD = 998244353L
    }

    constructor(y: Int) : this(y.toLong())

    val x = (x % MOD + MOD) % MOD

    operator fun plus(other: ModInt): ModInt {
        return ModInt(x + other.x)
    }

    operator fun minus(other: ModInt): ModInt {
        return ModInt(x - other.x)
    }

    operator fun times(other: ModInt): ModInt {
        return ModInt(x * other.x)
    }

    operator fun div(other: ModInt): ModInt {
        return this * other.inv()
    }

    fun pow(exp: Long): ModInt {
        if (exp == 0L) return ModInt(1L)
        var a = pow(exp shr 1)
        a *= a
        if (exp and 1L == 0L) return a
        return this * a
    }

    fun inv(): ModInt {
        return this.pow(MOD - 2)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as ModInt

        if (x != other.x) return false

        return true
    }

    override fun hashCode(): Int {
        return x.hashCode()
    }

    override fun toString(): String {
        return "$x"
    }
}

fun binom(n: Long, k: Long): ModInt {
    var numer = ModInt(1)
    var denom = ModInt(1)
    for (i in 0 until k) {
        numer *= ModInt(n - i)
        denom *= ModInt(k - i)
    }
    return numer / denom
}
// endregion
0