結果

問題 No.1740 Alone 'a'
ユーザー 箱星箱星
提出日時 2021-11-12 22:43:06
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 504 ms / 2,000 ms
コード長 3,028 bytes
コンパイル時間 15,978 ms
コンパイル使用メモリ 451,896 KB
実行使用メモリ 111,420 KB
最終ジャッジ日時 2024-11-25 20:24:52
合計ジャッジ時間 33,994 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 292 ms
49,924 KB
testcase_01 AC 301 ms
53,016 KB
testcase_02 AC 309 ms
56,372 KB
testcase_03 AC 498 ms
110,068 KB
testcase_04 AC 491 ms
110,088 KB
testcase_05 AC 302 ms
60,388 KB
testcase_06 AC 303 ms
60,584 KB
testcase_07 AC 289 ms
58,396 KB
testcase_08 AC 290 ms
58,304 KB
testcase_09 AC 291 ms
58,460 KB
testcase_10 AC 288 ms
58,480 KB
testcase_11 AC 341 ms
72,744 KB
testcase_12 AC 501 ms
111,052 KB
testcase_13 AC 493 ms
110,232 KB
testcase_14 AC 503 ms
111,204 KB
testcase_15 AC 298 ms
58,244 KB
testcase_16 AC 494 ms
109,040 KB
testcase_17 AC 298 ms
58,228 KB
testcase_18 AC 332 ms
67,404 KB
testcase_19 AC 481 ms
111,196 KB
testcase_20 AC 419 ms
96,772 KB
testcase_21 AC 469 ms
102,632 KB
testcase_22 AC 490 ms
107,652 KB
testcase_23 AC 492 ms
107,352 KB
testcase_24 AC 420 ms
97,228 KB
testcase_25 AC 421 ms
96,912 KB
testcase_26 AC 477 ms
102,448 KB
testcase_27 AC 482 ms
104,332 KB
testcase_28 AC 429 ms
99,952 KB
testcase_29 AC 504 ms
111,420 KB
testcase_30 AC 488 ms
103,660 KB
testcase_31 AC 498 ms
108,440 KB
testcase_32 AC 480 ms
100,596 KB
testcase_33 AC 494 ms
108,080 KB
testcase_34 AC 336 ms
69,628 KB
testcase_35 AC 431 ms
99,248 KB
testcase_36 AC 422 ms
96,064 KB
testcase_37 AC 341 ms
69,876 KB
testcase_38 AC 390 ms
84,428 KB
testcase_39 AC 313 ms
62,520 KB
testcase_40 AC 349 ms
75,692 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