結果
問題 | No.1740 Alone 'a' |
ユーザー |
|
提出日時 | 2021-11-12 22:43:06 |
言語 | Kotlin (2.1.0) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
import java.io.PrintWriterimport 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 ModIntclass ModInt(x: Long) {companion object {const val MOD = 998244353L//const val MOD = 1000000007L}constructor(y: Int) : this(y.toLong())val x = (x % MOD + MOD) % MODoperator 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 *= aif (exp and 1L == 0L) return areturn this * a}fun inv(): ModInt {return this.pow(MOD - 2)}override fun equals(other: Any?): Boolean {if (this === other) return trueif (javaClass != other?.javaClass) return falseother as ModIntif (x != other.x) return falsereturn 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}// endregionfun 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 Scannerprivate 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