結果

問題 No.1759 Silver Tour
ユーザー yudedakoyudedako
提出日時 2022-01-10 14:27:56
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 636 ms / 2,000 ms
コード長 3,090 bytes
コンパイル時間 15,563 ms
コンパイル使用メモリ 457,540 KB
実行使用メモリ 72,956 KB
最終ジャッジ日時 2024-04-26 20:08:10
合計ジャッジ時間 33,304 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 343 ms
60,288 KB
testcase_01 AC 315 ms
57,004 KB
testcase_02 AC 339 ms
60,568 KB
testcase_03 AC 636 ms
72,956 KB
testcase_04 AC 349 ms
55,396 KB
testcase_05 AC 315 ms
56,912 KB
testcase_06 AC 340 ms
60,252 KB
testcase_07 AC 342 ms
60,228 KB
testcase_08 AC 314 ms
56,972 KB
testcase_09 AC 343 ms
60,380 KB
testcase_10 AC 342 ms
60,312 KB
testcase_11 AC 339 ms
60,380 KB
testcase_12 AC 342 ms
60,288 KB
testcase_13 AC 348 ms
60,392 KB
testcase_14 AC 348 ms
60,396 KB
testcase_15 AC 345 ms
60,476 KB
testcase_16 AC 348 ms
60,608 KB
testcase_17 AC 352 ms
60,332 KB
testcase_18 AC 311 ms
56,908 KB
testcase_19 AC 348 ms
60,344 KB
testcase_20 AC 344 ms
60,252 KB
testcase_21 AC 358 ms
60,472 KB
testcase_22 AC 349 ms
60,380 KB
testcase_23 AC 346 ms
60,388 KB
testcase_24 AC 347 ms
60,384 KB
testcase_25 AC 313 ms
56,780 KB
testcase_26 AC 314 ms
56,932 KB
testcase_27 AC 340 ms
60,380 KB
testcase_28 AC 315 ms
56,964 KB
testcase_29 AC 314 ms
56,980 KB
testcase_30 AC 318 ms
56,988 KB
testcase_31 AC 345 ms
60,328 KB
testcase_32 AC 345 ms
60,368 KB
testcase_33 AC 344 ms
60,392 KB
testcase_34 AC 612 ms
70,268 KB
testcase_35 AC 314 ms
51,804 KB
testcase_36 AC 517 ms
61,920 KB
testcase_37 AC 346 ms
60,408 KB
testcase_38 AC 361 ms
62,656 KB
testcase_39 AC 433 ms
64,052 KB
testcase_40 AC 366 ms
60,444 KB
testcase_41 AC 357 ms
60,424 KB
testcase_42 AC 585 ms
66,004 KB
testcase_43 AC 436 ms
63,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import kotlin.math.absoluteValue

fun solve(height: Int, width: Int, mod: Int): Int {
    if (height % 2 == 1) {
        return if (width == 1) 1 else 0
    }
    val endState = (1L shl width) - 1
    fun Long.lastBit(): Long {
        return (inv() + 1) and this
    }
    fun calcPattern(firstLine: Boolean, column: Int, firstLineState: Long, secondLineState: Long): IntArray? {
        val both = endState xor (firstLineState and secondLineState)
        if (both != 0L && (both.lastBit() + both).let { it.lastBit() != it }) {
            return null
        }
        return if (secondLineState == endState) {
            check(!firstLine && firstLineState == endState)
            val result = IntArray(width).also { it[column] = 1 }
            result
        }else {
            val result = IntArray(width)
            var hasPath = false
            if (firstLine) {
                for (dc in -1 .. 1) {
                    val c = column + dc
                    if (c !in 0 until width) continue
                    if ((secondLineState shr c) and 1 == 1L) continue
                    val next = calcPattern(!firstLine, c, firstLineState, secondLineState or (1L shl c)) ?: continue
                    hasPath = true
                    for (i in 0 until width) {
                        result[i] = (result[i] + next[i]) % mod
                    }
                }
            } else {
                for (dc in -1 .. 1 step 2) {
                    val c = column + dc
                    if (c !in 0 until width) continue
                    if ((firstLineState shr c) and 1 == 1L) continue
                    val next = calcPattern(!firstLine, c, firstLineState or (1L shl c), secondLineState) ?: continue
                    hasPath = true
                    for (i in 0 until width) {
                        result[i] = (result[i] + next[i]) % mod
                    }
                }
            }
            if (hasPath) result else null
        }
    }
    val pattern = List(width){(calcPattern(true, it, 1L shl it, 0L) ?: IntArray(width)).toList() }
    operator fun List<List<Int>>.times(right: List<List<Int>>): List<List<Int>> {
        require(this[0].size == right.size)
        val result = mutableListOf<List<Int>>()
        for (i in indices) {
            val line = mutableListOf<Int>()
            for (j in right[i].indices) {
                var value = 0L
                for (k in right.indices) {
                    value += this[i][k].toLong() * right[k][j] % mod
                }
                line.add((value % mod).toInt())
            }
            result.add(line)
        }
        return result
    }
    val mid = List(width){r -> List(width){c -> if ((r - c).absoluteValue <= 1) 1 else 0} }
    val result = listOf(List(width){1}) * (1 until height / 2).fold(pattern){ left, _ -> left * mid * pattern}
    return result.flatten().fold(0){acc, v -> (acc + v) % mod }
}
fun main() {
    val (height, width, mod) = readLine()!!.trim().split(' ').map(String::toInt)
    val result = solve(height, width, mod)
    println(result)
}
0