結果

問題 No.1513 simple 門松列 problem
ユーザー yakamotoyakamoto
提出日時 2021-05-21 23:10:42
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,454 ms / 3,000 ms
コード長 6,186 bytes
コンパイル時間 19,868 ms
コンパイル使用メモリ 471,020 KB
実行使用メモリ 66,632 KB
最終ジャッジ日時 2024-10-10 10:04:26
合計ジャッジ時間 32,508 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 301 ms
57,836 KB
testcase_01 AC 303 ms
57,992 KB
testcase_02 AC 1,413 ms
65,812 KB
testcase_03 AC 300 ms
57,924 KB
testcase_04 AC 297 ms
57,936 KB
testcase_05 AC 332 ms
57,868 KB
testcase_06 AC 301 ms
57,868 KB
testcase_07 AC 308 ms
58,076 KB
testcase_08 AC 321 ms
58,032 KB
testcase_09 AC 296 ms
57,800 KB
testcase_10 AC 297 ms
57,860 KB
testcase_11 AC 296 ms
57,884 KB
testcase_12 AC 302 ms
58,108 KB
testcase_13 AC 293 ms
57,844 KB
testcase_14 AC 1,431 ms
64,828 KB
testcase_15 AC 1,454 ms
64,748 KB
testcase_16 AC 1,376 ms
65,720 KB
testcase_17 AC 1,014 ms
65,712 KB
testcase_18 AC 607 ms
65,848 KB
testcase_19 AC 453 ms
66,328 KB
testcase_20 AC 552 ms
66,632 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:235:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: LongArray) {
          ^
Main.kt:239:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: IntArray) {
          ^
Main.kt:243:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: BooleanArray) {
          ^
Main.kt:247:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}
          ^
Main.kt:249:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<LongArray>) {
          ^
Main.kt:256:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<IntArray>) {
          ^
Main.kt:263:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<BooleanArray>) {
          ^
Main.kt:280:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
          ^

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.AssertionError
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 998244353L

class LinearFunctionCompositionMod(val N: Int, val MOD: Long) {

  private val c0 = LongArray(N)
  private val c1 = LongArray(N)

  /**
   * [l, r)
   * @param s f(l)の値
   */
  fun add(l: Int, r: Int, delta: Long, s: Long) {
    val d1 = if (delta >= 0) delta else delta + MOD
    c1[l] += d1
    if (c1[l] >= MOD) c1[l] -= MOD
    c1[r] -= d1
    if (c1[r] < 0) c1[r] += MOD

    var d0 = (s - l*d1%MOD)
    if (d0 < 0) d0 += MOD
    c0[l] += d0
    if (c0[l] >= MOD) c0[l] -= MOD
    c0[r] -= d0
    if (c0[r] < 0) c0[r] += MOD
  }

  fun build() {
    for (i in 0 until N - 1) {
      c0[i + 1] += c0[i]
      if (c0[i + 1] >= MOD) c0[i + 1] -= MOD
      c1[i + 1] += c1[i]
      if (c1[i + 1] >= MOD) c1[i + 1] -= MOD
    }
  }

  fun calc(x: Int): Long {
    return (c1[x] * x + c0[x])%MOD
  }

  fun reset() {
    Arrays.fill(c0, 0)
    Arrays.fill(c1, 0)
  }

  fun reset(other: LinearFunctionCompositionMod) {
    other.c0.copyInto(c0)
    other.c1.copyInto(c1)
  }

  fun inspect() = Pair(c0, c1)
}
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  private val reader = BufferedReader(InputStreamReader(stream), 32768)

  fun solve() {
    val (N, K) = na(2)
    val dp1_cur = Array(K + 1){LinearFunctionCompositionMod(K + 1, MOD)} // 組み合わせ
    val dp1_next = Array(K + 1){LinearFunctionCompositionMod(K + 1, MOD)} // 組み合わせ
    val dp2_cur = Array(K + 1){LinearFunctionCompositionMod(K + 1, MOD)}
    val dp2_next = Array(K + 1){LinearFunctionCompositionMod(K + 1, MOD)}

    for (k1 in 0 until K) {
      if (k1 > 0) {
        dp1_cur[k1].add(0, k1, 0, 1)
        dp2_cur[k1].add(0, k1, 1, 0)
        dp2_cur[k1].add(0, k1, 0, k1.toLong())
      }
      if (k1 < K - 1) {
        dp1_cur[k1].add(k1 + 1, K, 0, 1)
        dp2_cur[k1].add(k1 + 1, K, 1, (k1+1).toLong())
        dp2_cur[k1].add(k1 + 1, K, 0, k1.toLong())
      }
      dp1_cur[k1].build()
      dp2_cur[k1].build()
    }
//    if (isDebug) {
//      debug{"${(0 until K).map{dp1_cur[0].calc(it)}.joinToString(" ")}"}
//    }

    for (i in 2 until N) {
      for (k1 in 0 until K) {
        for (k2 in 0 until K) {
          if (k1 == k2) continue

          val comb = dp1_cur[k1].calc(k2)
          val c2 = dp2_cur[k1].calc(k2)
          if (k1 > k2) {
            dp1_next[k2].add(k2 + 1, k1, 0, comb)
            dp1_next[k2].add(k1 + 1, K, 0, comb)

            dp2_next[k2].add(k2 + 1, k1, comb, comb*(k2+1)%MOD)
            dp2_next[k2].add(k1 + 1, K, comb, comb*(k1 + 1)%MOD)

            dp2_next[k2].add(k2 + 1, k1, 0, c2)
            dp2_next[k2].add(k1 + 1, K, 0, c2)

          }
          else {
            dp1_next[k2].add(0, k1, 0, comb)
            dp1_next[k2].add(k1 + 1, k2, 0, comb)
            dp2_next[k2].add(0, k1, comb, 0)
            dp2_next[k2].add(k1 + 1, k2, comb, comb*(k1 + 1)%MOD)
            dp2_next[k2].add(0, k1, 0, c2)
            dp2_next[k2].add(k1 + 1, k2, 0, c2)
          }
        }
      }
      for (k1 in 0 until K) {
        dp1_next[k1].build()
        dp1_cur[k1].reset(dp1_next[k1])
        dp1_next[k1].reset()

        dp2_next[k1].build()
        dp2_cur[k1].reset(dp2_next[k1])
        dp2_next[k1].reset()
      }
    }

    if (isDebug) {
      debug{"${(0 until K).map{dp2_cur[2].calc(it)}.joinToString(" ")}"}
    }

    var ans1 = 0L
    var ans2 = 0L
    for (k1 in 0 until K) {
      for (k2 in 0 until K) {
        ans1 += dp1_cur[k1].calc(k2)
        ans2 += dp2_cur[k1].calc(k2)
      }
    }
    ans1 %= MOD
    ans2 %= MOD
    out.println("$ans1 $ans2")
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return IntArray(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  /**
   * コーナーケースでエラー出たりするので、debug(dp[1])のように添え字付きの場合はdebug{}をつかうこと
   */
  private inline fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: BooleanArray) {
    debug { toString(a) }
  }

  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}

  private inline fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<BooleanArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }

  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
  private inline fun assert(b: Boolean, f: () -> String) = run{if (!b) throw AssertionError(f())}
}

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0