結果

問題 No.1750 ラムドスウイルスの感染拡大-hard
ユーザー yudedakoyudedako
提出日時 2022-01-26 22:52:48
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 4,199 bytes
コンパイル時間 15,746 ms
コンパイル使用メモリ 258,152 KB
実行使用メモリ 130,656 KB
最終ジャッジ日時 2023-08-25 11:41:38
合計ジャッジ時間 38,881 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 931 ms
130,656 KB
testcase_01 AC 921 ms
62,544 KB
testcase_02 AC 923 ms
62,436 KB
testcase_03 AC 976 ms
62,816 KB
testcase_04 AC 1,158 ms
62,908 KB
testcase_05 AC 918 ms
62,248 KB
testcase_06 AC 921 ms
62,500 KB
testcase_07 AC 922 ms
62,188 KB
testcase_08 AC 1,539 ms
63,076 KB
testcase_09 AC 1,535 ms
63,352 KB
testcase_10 AC 1,581 ms
62,984 KB
testcase_11 AC 1,587 ms
63,820 KB
testcase_12 AC 1,676 ms
63,224 KB
testcase_13 AC 1,683 ms
62,952 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.annotation.tailrec
import scala.io.StdIn.*
import scala.math.*
import scala.reflect.ClassTag
import scala.util.chaining.*

object ModInt:
  opaque type ModInt = Long
  extension (value: Long)
    inline def asModInt(using inline mod: Int): ModInt =  value % mod
  extension (value: Int)
    inline def asModInt(using inline mod: Int): ModInt = value % mod
  trait Converter[T]:
    inline def convert(value: T)(using inline mod: Int): ModInt
  inline given Converter[Int] with
    override inline def convert(value: Int)(using inline mod: Int) = value.asModInt
  inline given Converter[Long] with
    override inline def convert(value: Long)(using inline mod: Int) = value.asModInt
  inline given Converter[ModInt] with
    override inline def convert(value: ModInt)(using inline mod: Int) = value
  extension (modInt: ModInt)
    inline def asLong(using inline mod: Int): Long = (modInt + mod) % mod
    inline def inverse(using inline mod: Int): ModInt = modInt.powMod(mod - 2)
    inline def powMod(exp: Long)(using inline mod: Int): ModInt =
      var result = 1L
      var base = modInt
      var e = exp
      while e > 0 do
        if (e & 1) == 1 then
          result = result * base % mod
        base = base * base % mod
        e >>= 1
      result
    inline def powMod(exp: Int)(using inline mod: Int): ModInt = powMod(exp.toLong)
  extension [L: Converter, R: Converter] (left: L)
    inline def +(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) + summon[Converter[R]].convert(right)).asModInt
    inline def -(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) - summon[Converter[R]].convert(right)).asModInt
    inline def *(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right)).asModInt
    inline def /(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right).inverse).asModInt

trait SemiRing[T]:
  def plus(left: T, right: T): T
  def times(left: T, right: T): T
  def zero: T
  def one: T
class Matrix[T: SemiRing: ClassTag] private(val row: Int, val column: Int, private val matrix: Array[Array[T]]):
  def this(row: Int, column: Int) =
    this(row, column, Array.fill(row){Array.fill(column){summon[SemiRing[T]].zero}})
  def apply(i: Int, j: Int): T =
    matrix(i)(j)
  def update(i: Int, j: Int, value: T) =
    matrix(i)(j) = value
  def *(right: Matrix[T]): Matrix[T] =
    val newRow = row
    val newColumn = right.column
    val mid = column
    Matrix.tabulate(newRow, newColumn){(i, j) =>
      (0 until mid).foldLeft(summon[SemiRing[T]].zero){(acc, k) => summon[SemiRing[T]].plus(acc, summon[SemiRing[T]].times(matrix(i)(k), right.matrix(k)(j))) }
    }
  def pow(exp: Long): Matrix[T] =
    require(row == column)
    var result = Matrix.e(row, row)
    var base = this
    var e = exp
    while e > 0 do
      if (e & 1) == 1 then
        result *= base
      base *= base
      e >>= 1
    result
object Matrix:
  def tabulate[T: SemiRing: ClassTag](row: Int, column: Int)(generator: (Int, Int) => T): Matrix[T] =
    Matrix(row, column, Array.tabulate(row){i => Array.tabulate(column){j => generator(i, j)}})
  def e[T: SemiRing: ClassTag](row: Int, column: Int): Matrix[T] =
    Matrix.tabulate(row, column){(i, j) =>
      if i == j then summon[SemiRing[T]].one else summon[SemiRing[T]].zero
    }


@main def main =
  import ModInt.*
  inline given mod: Int = 998244353
  inline given SemiRing[ModInt] with
    override def zero = 0.asModInt
    override def one = 1.asModInt
    override def plus(left: ModInt, right: ModInt) = left + right
    override def times(left: ModInt, right: ModInt) = left * right

  val (n, m, t) = readLine().split(' ').pipe{case Array(n, m, t) => (n.toInt, m.toInt, t.toLong)}
  val roads = Array.fill(m){
    val Array(u, v) = readLine().split(' ').map(_.toInt)
    (u, v)
  }
  val matrix = Matrix[ModInt](n, n)
  for (u, v) <- roads do
    matrix(u, v) = 1.asModInt
    matrix(v, u) = 1.asModInt
  val initial = Matrix[ModInt](n, 1).tap(m => m(0, 0) = 1.asModInt)
  val result = matrix.pow(t) * initial
  println(result(0, 0))
0