結果
| 問題 |
No.1759 Silver Tour
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-01-10 14:26:16 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,136 bytes |
| コンパイル時間 | 15,967 ms |
| コンパイル使用メモリ | 461,144 KB |
| 実行使用メモリ | 74,360 KB |
| 最終ジャッジ日時 | 2024-11-14 10:53:25 |
| 合計ジャッジ時間 | 33,367 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 36 WA * 4 |
ソースコード
import kotlin.math.absoluteValue
fun solve(height: Int, width: Int, mod: Int): Int {
if (height == 1) {
return if (width == 1) 1 else 0
}
if (height % 2 == 1) {
return 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)
}