結果

問題 No.1740 Alone 'a'
ユーザー 👑 箱
提出日時 2021-11-12 22:43:06
言語 Kotlin
(1.9.10)
結果
AC  
実行時間 562 ms / 2,000 ms
コード長 3,028 bytes
コンパイル時間 19,818 ms
コンパイル使用メモリ 434,052 KB
実行使用メモリ 105,912 KB
最終ジャッジ日時 2023-08-17 03:02:08
合計ジャッジ時間 34,967 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 281 ms
52,780 KB
testcase_01 AC 274 ms
54,428 KB
testcase_02 AC 267 ms
52,600 KB
testcase_03 AC 548 ms
104,840 KB
testcase_04 AC 540 ms
104,596 KB
testcase_05 AC 287 ms
57,304 KB
testcase_06 AC 283 ms
56,884 KB
testcase_07 AC 270 ms
52,600 KB
testcase_08 AC 272 ms
54,528 KB
testcase_09 AC 279 ms
52,552 KB
testcase_10 AC 275 ms
53,012 KB
testcase_11 AC 335 ms
61,572 KB
testcase_12 AC 553 ms
105,124 KB
testcase_13 AC 519 ms
91,488 KB
testcase_14 AC 562 ms
105,912 KB
testcase_15 AC 281 ms
52,628 KB
testcase_16 AC 517 ms
88,692 KB
testcase_17 AC 276 ms
54,364 KB
testcase_18 AC 318 ms
57,620 KB
testcase_19 AC 548 ms
105,204 KB
testcase_20 AC 402 ms
74,096 KB
testcase_21 AC 453 ms
80,092 KB
testcase_22 AC 513 ms
83,544 KB
testcase_23 AC 512 ms
83,668 KB
testcase_24 AC 403 ms
73,412 KB
testcase_25 AC 411 ms
75,888 KB
testcase_26 AC 451 ms
76,320 KB
testcase_27 AC 506 ms
81,184 KB
testcase_28 AC 432 ms
77,772 KB
testcase_29 AC 543 ms
103,820 KB
testcase_30 AC 505 ms
77,288 KB
testcase_31 AC 523 ms
86,128 KB
testcase_32 AC 444 ms
77,932 KB
testcase_33 AC 515 ms
88,036 KB
testcase_34 AC 323 ms
59,436 KB
testcase_35 AC 408 ms
75,752 KB
testcase_36 AC 400 ms
75,480 KB
testcase_37 AC 327 ms
59,672 KB
testcase_38 AC 392 ms
71,320 KB
testcase_39 AC 294 ms
55,772 KB
testcase_40 AC 338 ms
61,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val s = nextLine()
    val dp = Array(n + 1) { Array(2) { Array(2) { ModInt(0) } } }
    dp[0][0][0] = ModInt(1)
    for (i in 0 until n) {
        val c = s[i]
        dp[i + 1][0][0] = if (c == 'a') ModInt(0) else dp[i][0][0]
        dp[i + 1][0][1] = if (c == 'a') dp[i][0][0] else dp[i][0][1]
        dp[i + 1][1][0] = if (c == 'a') dp[i][1][0] * ModInt(25)
                            else dp[i][1][0] * ModInt(25) + dp[i][0][0] * ModInt(c - 'b')
        dp[i + 1][1][1] = if (c == 'a') dp[i][1][1] * ModInt(25) + dp[i][1][0]
                            else dp[i][1][1] * ModInt(25) + dp[i][1][0] + dp[i][0][0] + dp[i][0][1] * ModInt(c - 'b')
    }
    println(dp[n][1][1])
}

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

    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 Long.toModInt(): ModInt {
    return ModInt(this)
}

fun Int.toModInt(): ModInt {
    return ModInt(this)
}

fun binomSimple(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

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", abs(1L.shl(26)))
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()
}

// 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