結果

問題 No.1740 Alone 'a'
ユーザー 👑 箱星箱星
提出日時 2021-11-12 22:43:06
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 425 ms / 2,000 ms
コード長 3,028 bytes
コンパイル時間 13,796 ms
コンパイル使用メモリ 448,884 KB
実行使用メモリ 114,764 KB
最終ジャッジ日時 2024-05-04 10:15:26
合計ジャッジ時間 29,185 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 246 ms
58,584 KB
testcase_01 AC 256 ms
58,448 KB
testcase_02 AC 251 ms
58,328 KB
testcase_03 AC 413 ms
113,844 KB
testcase_04 AC 410 ms
113,404 KB
testcase_05 AC 257 ms
60,620 KB
testcase_06 AC 255 ms
60,388 KB
testcase_07 AC 254 ms
58,672 KB
testcase_08 AC 247 ms
58,432 KB
testcase_09 AC 247 ms
58,484 KB
testcase_10 AC 252 ms
58,552 KB
testcase_11 AC 287 ms
75,088 KB
testcase_12 AC 410 ms
114,764 KB
testcase_13 AC 411 ms
112,668 KB
testcase_14 AC 409 ms
114,676 KB
testcase_15 AC 249 ms
58,340 KB
testcase_16 AC 408 ms
112,744 KB
testcase_17 AC 250 ms
58,320 KB
testcase_18 AC 289 ms
68,836 KB
testcase_19 AC 425 ms
114,676 KB
testcase_20 AC 356 ms
100,160 KB
testcase_21 AC 395 ms
106,412 KB
testcase_22 AC 407 ms
110,712 KB
testcase_23 AC 413 ms
110,700 KB
testcase_24 AC 347 ms
100,244 KB
testcase_25 AC 363 ms
100,408 KB
testcase_26 AC 415 ms
106,512 KB
testcase_27 AC 405 ms
106,596 KB
testcase_28 AC 362 ms
102,240 KB
testcase_29 AC 409 ms
114,716 KB
testcase_30 AC 396 ms
106,312 KB
testcase_31 AC 402 ms
112,484 KB
testcase_32 AC 391 ms
104,420 KB
testcase_33 AC 403 ms
110,720 KB
testcase_34 AC 283 ms
70,880 KB
testcase_35 AC 350 ms
102,204 KB
testcase_36 AC 343 ms
100,252 KB
testcase_37 AC 283 ms
71,020 KB
testcase_38 AC 323 ms
85,016 KB
testcase_39 AC 262 ms
62,736 KB
testcase_40 AC 300 ms
76,956 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