Submission #54824544


Source Code Expand

import MutableIntMap.Companion.mutableIntMapOf
import java.io.*
import java.util.*
import kotlin.collections.*
import kotlin.math.*

val INPUT: InputStream = System.`in`
val OUTPUT: PrintStream = System.out

val _reader = INPUT.bufferedReader()

var _tokenizer: StringTokenizer = StringTokenizer("")
fun read(): String {
    while (!_tokenizer.hasMoreTokens()) {
        _tokenizer = StringTokenizer(_reader.readLine() ?: return "", " ")
    }
    return _tokenizer.nextToken()
}

fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readInts(n: Int) = List(n) { readInt() }
fun readIntArray(n: Int) = IntArray(n) { readInt() }
fun readDoubles(n: Int) = List(n) { readDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { readDouble() }
fun readLongs(n: Int) = List(n) { readLong() }
fun readLongArray(n: Int) = LongArray(n) { readLong() }

val _writer = PrintWriter(OUTPUT, false)
inline fun output(block: PrintWriter.() -> Unit) {
    _writer.apply(block).flush()
}

class MutableIntMap<K> : LinkedHashMap<K, Int>() {
    override operator fun get(key: K): Int = super.get(key) ?: 0

    fun increment(key: K, value: Int = 1) {
        this[key] = this[key] + value
    }

    companion object {
        fun <K> mutableIntMapOf(vararg pairs: Pair<K, Int>) =
            MutableIntMap<K>().apply { putAll(pairs) }
    }
}

typealias Pii = Pair<Int, Int>

fun readPii() = readInt() to readInt()
fun readPiis(n: Int) = List(n) { readPii() }

infix fun Int.hasBit(i: Int): Boolean = this and (1 shl i) > 0
infix fun Int.xorBit(i: Int): Int = this xor (1 shl i)
infix fun Long.hasBit(i: Int): Boolean = this and (1L shl i) > 0
infix fun Long.xorBit(i: Int): Long = this xor (1L shl i)

fun largerStackSize(stackSizeMegaBytes: Int = 100, action: () -> Unit) {
    Thread(null, action, "", 1024L * 1024 * stackSizeMegaBytes).apply {
        start()
        join()
    }
}

// #################################################################################################

@JvmInline
value class ModInt private constructor(val value: Int) {
    operator fun plus(other: ModInt) = plus(other.value)
    operator fun plus(other: Int) = from(value + other)
    operator fun plus(other: Long) = from(value + other)
    operator fun minus(other: ModInt) = minus(other.value)
    operator fun minus(other: Int) = from(value - other)
    operator fun minus(other: Long) = from(value - other)
    operator fun times(other: ModInt) = times(other.value)
    operator fun times(other: Int) = times(other.toLong())
    operator fun times(other: Long) = from(value * other)
    operator fun div(other: ModInt) = times(other.inv())
    operator fun div(other: Int) = div(from(other))
    operator fun div(other: Long) = div(from(other))

    fun pow(exponent: Int): ModInt {
        var ans = One
        var a = this
        var b = exponent
        while (b > 0) {
            if (b % 2 == 1) ans *= a
            a *= a
            b /= 2
        }
        return ans
    }

    fun inv(): ModInt = pow(MOD - 2)

    override fun toString() = value.toString()

    companion object {
        fun combination(n: Int, k: Int): ModInt {
            check(k in 0..n)
            return (1..k).fold(One) { acc, i -> acc * (n - i + 1) / i }
        }

        fun from(value: Int) = ModInt(value.mod())
        fun from(value: Long) = ModInt(value.mod())
        fun Int.mod() = mod(MOD)
        fun Long.mod() = mod(MOD)
        val Zero = from(0)
        val One = from(1)

        const val MOD = 998244353
    }
}

fun Array<ModInt>.sum(): ModInt {
    var sum = ModInt.Zero
    for (element in this) sum += element
    return sum
}


fun solve() {
    val n = readInt()
    val k = readInt()
    val s = read()
    val h = 1 shl k - 1
    var f = Array(h) { ModInt.Zero }
    f[0] = ModInt.One
    for (i in s.indices) {
        val g = Array(h) { ModInt.Zero }
        for (j in 0 until h) if (f[j].value > 0) {
            fun add(t: Int) {
                if (i >= k - 1) {
                    if ((0 until k).any { t hasBit it != t hasBit k - 1 - it }) g[t % h] += f[j]
                } else {
                    g[t] += f[j]
                }
            }
            when (s[i]) {
                'A' -> add(j * 2)
                'B' -> add(j * 2 + 1)
                else -> {
                    add(j * 2)
                    add(j * 2 + 1)
                }
            }
        }
        f = g
    }
    println(f.sum())
}

fun main() {
//    repeat(readInt()) { solve() }
    solve()
}

Submission Info

Submission Time
Task D - Avoid K Palindrome
User wangchaohui
Language Kotlin (Kotlin/JVM 1.8.20)
Score 450
Code Size 4746 Byte
Status AC
Exec Time 161 ms
Memory 58480 KiB

Compile Error

Main.kt:124:9: warning: variable 'n' is never used
    val n = readInt()
        ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 4
AC × 76
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 01_random_64.txt, 01_random_65.txt, 01_random_66.txt, 01_random_67.txt, 01_random_68.txt, 01_random_69.txt, 01_random_70.txt, 01_random_71.txt, 01_random_72.txt, 01_random_73.txt, 01_random_74.txt, 01_random_75.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 50 ms 38080 KiB
00_sample_01.txt AC 55 ms 38492 KiB
00_sample_02.txt AC 53 ms 38200 KiB
00_sample_03.txt AC 53 ms 38316 KiB
01_random_04.txt AC 159 ms 57784 KiB
01_random_05.txt AC 151 ms 58004 KiB
01_random_06.txt AC 141 ms 57732 KiB
01_random_07.txt AC 154 ms 57836 KiB
01_random_08.txt AC 61 ms 39584 KiB
01_random_09.txt AC 146 ms 58064 KiB
01_random_10.txt AC 67 ms 41724 KiB
01_random_11.txt AC 64 ms 49064 KiB
01_random_12.txt AC 52 ms 38396 KiB
01_random_13.txt AC 107 ms 54900 KiB
01_random_14.txt AC 51 ms 38456 KiB
01_random_15.txt AC 98 ms 49592 KiB
01_random_16.txt AC 64 ms 39964 KiB
01_random_17.txt AC 160 ms 58456 KiB
01_random_18.txt AC 65 ms 41812 KiB
01_random_19.txt AC 66 ms 49064 KiB
01_random_20.txt AC 53 ms 38632 KiB
01_random_21.txt AC 64 ms 43984 KiB
01_random_22.txt AC 52 ms 38328 KiB
01_random_23.txt AC 67 ms 44112 KiB
01_random_24.txt AC 61 ms 39472 KiB
01_random_25.txt AC 161 ms 58480 KiB
01_random_26.txt AC 66 ms 41764 KiB
01_random_27.txt AC 69 ms 49004 KiB
01_random_28.txt AC 58 ms 39004 KiB
01_random_29.txt AC 102 ms 53632 KiB
01_random_30.txt AC 51 ms 38484 KiB
01_random_31.txt AC 96 ms 49936 KiB
01_random_32.txt AC 70 ms 40728 KiB
01_random_33.txt AC 154 ms 57928 KiB
01_random_34.txt AC 56 ms 38612 KiB
01_random_35.txt AC 69 ms 49048 KiB
01_random_36.txt AC 54 ms 38432 KiB
01_random_37.txt AC 101 ms 53492 KiB
01_random_38.txt AC 115 ms 55156 KiB
01_random_39.txt AC 106 ms 55308 KiB
01_random_40.txt AC 96 ms 44572 KiB
01_random_41.txt AC 154 ms 57920 KiB
01_random_42.txt AC 68 ms 43980 KiB
01_random_43.txt AC 64 ms 44080 KiB
01_random_44.txt AC 65 ms 43924 KiB
01_random_45.txt AC 70 ms 49004 KiB
01_random_46.txt AC 66 ms 40316 KiB
01_random_47.txt AC 102 ms 52336 KiB
01_random_48.txt AC 91 ms 47460 KiB
01_random_49.txt AC 119 ms 57792 KiB
01_random_50.txt AC 66 ms 41612 KiB
01_random_51.txt AC 66 ms 49008 KiB
01_random_52.txt AC 52 ms 38428 KiB
01_random_53.txt AC 71 ms 48868 KiB
01_random_54.txt AC 51 ms 38424 KiB
01_random_55.txt AC 108 ms 52816 KiB
01_random_56.txt AC 63 ms 39604 KiB
01_random_57.txt AC 157 ms 57840 KiB
01_random_58.txt AC 56 ms 38464 KiB
01_random_59.txt AC 64 ms 43876 KiB
01_random_60.txt AC 56 ms 38756 KiB
01_random_61.txt AC 67 ms 48784 KiB
01_random_62.txt AC 51 ms 38452 KiB
01_random_63.txt AC 108 ms 54860 KiB
01_random_64.txt AC 157 ms 57744 KiB
01_random_65.txt AC 130 ms 57784 KiB
01_random_66.txt AC 67 ms 48772 KiB
01_random_67.txt AC 69 ms 49028 KiB
01_random_68.txt AC 60 ms 39116 KiB
01_random_69.txt AC 102 ms 53040 KiB
01_random_70.txt AC 56 ms 38508 KiB
01_random_71.txt AC 68 ms 44196 KiB
01_random_72.txt AC 144 ms 58284 KiB
01_random_73.txt AC 138 ms 57912 KiB
01_random_74.txt AC 52 ms 38084 KiB
01_random_75.txt AC 156 ms 57852 KiB