結果

問題 No.1873 Bracket Swapping
ユーザー yudedakoyudedako
提出日時 2022-03-17 01:31:57
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 4,606 bytes
コンパイル時間 15,440 ms
コンパイル使用メモリ 273,576 KB
実行使用メモリ 212,644 KB
最終ジャッジ日時 2023-10-25 09:02:59
合計ジャッジ時間 57,859 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 972 ms
65,576 KB
testcase_01 AC 976 ms
65,484 KB
testcase_02 AC 1,012 ms
65,636 KB
testcase_03 AC 1,308 ms
80,296 KB
testcase_04 AC 1,201 ms
68,128 KB
testcase_05 AC 1,186 ms
68,112 KB
testcase_06 AC 1,916 ms
134,228 KB
testcase_07 AC 1,008 ms
65,592 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 AC 1,093 ms
65,752 KB
testcase_12 AC 1,812 ms
120,352 KB
testcase_13 AC 1,666 ms
108,740 KB
testcase_14 TLE -
testcase_15 AC 1,468 ms
91,020 KB
testcase_16 AC 1,057 ms
65,692 KB
testcase_17 AC 1,949 ms
143,940 KB
testcase_18 AC 1,097 ms
65,756 KB
testcase_19 AC 1,186 ms
68,096 KB
testcase_20 TLE -
testcase_21 AC 1,097 ms
65,732 KB
testcase_22 AC 1,283 ms
71,068 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import scala.collection.mutable.*
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*
import scala.reflect.ClassTag
import scala.util.*
import scala.annotation.tailrec
import scala.collection.mutable

class ModInt(val rawValue: Long) extends AnyVal:
  inline def +(right: ModInt)(using inline mod: Int): ModInt = ModInt((rawValue + right.rawValue) % mod)
  inline def -(right: ModInt)(using inline mod: Int): ModInt = ModInt((rawValue - right.rawValue) % mod)
  inline def *(right: ModInt)(using inline mod: Int): ModInt = ModInt((rawValue * right.rawValue) % mod)
  inline def /(right: ModInt)(using inline mod: Int): ModInt = this * right.inverse
  inline def pow(exp: Long)(using inline mod: Int): ModInt =
    var result = 1L
    var base = rawValue
    var e = exp
    while e > 0 do
      if (e & 1) == 1 then
        result = result * base % mod
      base = base * base % mod
      e >>= 1
    ModInt(result)
  inline def pow(exp: Int)(using inline mod: Int): ModInt = pow(exp.toLong)
  inline def inverse(using inline mod: Int): ModInt =
    var x = rawValue
    var y = mod.toLong
    var a = 1L
    var b = 0L
    var c = 0L
    var d = 1L
    while y != 0 do
      val q = x / y
      val e = a - c * q
      val f = b - d * q
      a = c
      b = d
      c = e
      d = f
      x = y
      y = c * rawValue + d * mod
    require(x.abs == 1L)
    ModInt(if x == 1 then a % mod else -a % mod)
  inline def toLong(using inline mod: Int): Long = (rawValue + mod) % mod
  inline def toInt(using inline mod: Int): Int = ((rawValue + mod) % mod).toInt
  override def toString = rawValue.toString

object ModInt:
  inline def zero: ModInt = ModInt(0)
  inline def one: ModInt = ModInt(1)
  inline given Conversion[Int, ModInt] with
    override def apply(x: Int) = ModInt(x)
  extension (value: Long)
    inline def toModInt(using inline mod: Int): ModInt = ModInt(value % mod)


object Matrix:
  import ModInt.*
  opaque type Matrix = Array[Array[ModInt]]
  extension (array: Array[Array[ModInt]])
    def asMatrix: Matrix = array
  extension (matrix: Matrix)
    def asArray: Array[Array[ModInt]] = matrix
  def E(size: Int): Matrix =
    Array.tabulate(size){i => Array.tabulate(size){j => if i == j then 1 else 0}}
  extension (left: Matrix)
    inline def *(right: Matrix)(using inline mod: Int): Matrix =
      val row = left.length
      val mid = right.length
      val column = right(0).length
      val result = Array.fill(row){Array.fill(column){zero}}
      for
        i <- 0 until row
        j <- 0 until column
      do
        result(i)(j) = (0 until mid).foldLeft(zero){(acc, k) => acc + left(i)(k) * right(k)(j)}
      result
    inline def pow(exp: Int)(using inline mod: Int): Matrix =
      var result = E(left.length)
      var b = left
      var e = exp
      while e > 0 do
        if (e & 1) == 1 then
          result *= b
        b *= b
        e >>= 1
      result
    inline def inspect: String = left.map{_.mkString(", ")}.mkString("\n")

def makeMatrix(size: Int): Matrix.Matrix =
  import Matrix.*
  val half = size / 2
  val result = Array.fill(half + 1){Array.fill(half + 1){ModInt.zero}}
  for
    i <- 0 to half
  do
    if 0 <= i - 1 then
      result(i)(i - 1) = (half - i + 1) * (half - i + 1)
    result(i)(i) = (half - i) * (half - i - 1) + i * (i - 1) + 2 * (size - 2 * i) * i
    if i + 1 <= half then
      result(i)(i + 1) = (i + 1) * (i + 1)
  result.asMatrix

@main def main =
  import ModInt.*
  import Matrix.*
  inline given mod: Int = 998244353
  val str = readLine()
  val k = readLine().toInt
  val size = str.length
  val half = size / 2
  val memo = Array.fill(size + 1){Array.fill(half + 1){Array.fill(half + 1){zero}}}.tap{a => a(0)(0)(0) = one}
  for
    (c, i) <- str.zipWithIndex
    height <- 0 to half
    toRight <- 0 to half
  do
    memo(i + 1)(height)(toRight) = c match
      case '(' =>
        var pattern = zero
        if 0 <= height - 1 then
          pattern += memo(i)(height - 1)(toRight)
        if height + 1 <= half && 0 < toRight then
          pattern += memo(i)(height + 1)(toRight - 1)
        pattern
      case ')' =>
        var pattern = zero
        if height + 1 <= half then
          pattern += memo(i)(height + 1)(toRight)
        if 0 <= height - 1 then
          pattern += memo(i)(height - 1)(toRight)
        pattern
      case _ => ???
  val matrix = makeMatrix(size).pow(k).asArray
  val left = matrix(0)
  val right = memo(size)(0)
  var result = zero
  for toRight <- 0 to half do
    result += left(toRight) * right(toRight)
  println(result.toLong)
0