結果

問題 No.1750 ラムドスウイルスの感染拡大-hard
ユーザー yudedakoyudedako
提出日時 2022-01-26 23:15:44
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,894 ms / 2,000 ms
コード長 3,674 bytes
コンパイル時間 13,088 ms
コンパイル使用メモリ 262,216 KB
実行使用メモリ 67,080 KB
最終ジャッジ日時 2023-08-25 12:10:45
合計ジャッジ時間 58,952 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 907 ms
62,772 KB
testcase_01 AC 911 ms
62,244 KB
testcase_02 AC 919 ms
62,312 KB
testcase_03 AC 944 ms
62,372 KB
testcase_04 AC 1,105 ms
62,688 KB
testcase_05 AC 904 ms
62,448 KB
testcase_06 AC 904 ms
62,108 KB
testcase_07 AC 927 ms
62,048 KB
testcase_08 AC 1,193 ms
62,736 KB
testcase_09 AC 1,188 ms
62,576 KB
testcase_10 AC 1,299 ms
62,612 KB
testcase_11 AC 1,198 ms
62,968 KB
testcase_12 AC 1,307 ms
63,352 KB
testcase_13 AC 1,246 ms
62,812 KB
testcase_14 AC 1,835 ms
67,024 KB
testcase_15 AC 1,888 ms
66,948 KB
testcase_16 AC 1,888 ms
67,048 KB
testcase_17 AC 1,841 ms
62,972 KB
testcase_18 AC 1,894 ms
62,756 KB
testcase_19 AC 1,779 ms
62,932 KB
testcase_20 AC 1,607 ms
62,516 KB
testcase_21 AC 1,787 ms
62,672 KB
testcase_22 AC 1,167 ms
62,768 KB
testcase_23 AC 1,884 ms
67,080 KB
testcase_24 AC 1,126 ms
62,520 KB
testcase_25 AC 1,316 ms
63,380 KB
testcase_26 AC 1,090 ms
62,756 KB
testcase_27 AC 967 ms
62,544 KB
testcase_28 AC 1,004 ms
62,484 KB
testcase_29 AC 959 ms
62,908 KB
testcase_30 AC 1,180 ms
63,036 KB
testcase_31 AC 1,136 ms
62,756 KB
testcase_32 AC 1,148 ms
62,980 KB
testcase_33 AC 1,176 ms
62,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

object ModInt:
  opaque type ModInt = Long
  val zero: ModInt = 0
  val one: ModInt = 1
  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

import ModInt.*
inline given mod: Int = 998244353
class Matrix private(val row: Int, val column: Int, private val matrix: Array[Array[ModInt]]):
  def this(row: Int, column: Int) =
    this(row, column, Array.fill(row){Array.fill(column){ModInt.zero}})
  def apply(i: Int, j: Int): ModInt =
    matrix(i)(j)
  def update(i: Int, j: Int, value: ModInt) =
    matrix(i)(j) = value
  def *(right: Matrix): Matrix =
    val newRow = row
    val newColumn = right.column
    val mid = column
    Matrix.tabulate(newRow, newColumn){(i, j) =>
      (0 until mid).foldLeft(ModInt.zero){(acc, k) => acc + matrix(i)(k) * right.matrix(k)(j) }
    }
  def pow(exp: Long): Matrix =
    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(row: Int, column: Int)(generator: (Int, Int) => ModInt): Matrix =
    Matrix(row, column, Array.tabulate(row){i => Array.tabulate(column){j => generator(i, j)}})
  def e(row: Int, column: Int): Matrix =
    Matrix.tabulate(row, column){(i, j) =>
      if i == j then ModInt.one else ModInt.zero
    }


@main def main =
  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(n, n)
  for (u, v) <- roads do
    matrix(u, v) = ModInt.one
    matrix(v, u) = ModInt.one
  val initial = Matrix(n, 1).tap(m => m(0, 0) = ModInt.one)
  val result = matrix.pow(t) * initial
  println(result(0, 0))
0