結果

問題 No.1810 RGB Biscuits
ユーザー yudedakoyudedako
提出日時 2022-01-25 23:14:14
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,524 ms / 2,000 ms
コード長 3,647 bytes
コンパイル時間 17,755 ms
コンパイル使用メモリ 253,928 KB
実行使用メモリ 67,544 KB
最終ジャッジ日時 2023-08-22 18:52:20
合計ジャッジ時間 47,626 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,003 ms
62,656 KB
testcase_01 AC 1,033 ms
62,784 KB
testcase_02 AC 1,000 ms
62,960 KB
testcase_03 AC 1,077 ms
63,196 KB
testcase_04 AC 1,086 ms
62,980 KB
testcase_05 AC 1,397 ms
67,544 KB
testcase_06 AC 1,505 ms
63,768 KB
testcase_07 AC 1,524 ms
64,308 KB
testcase_08 AC 1,484 ms
63,732 KB
testcase_09 AC 1,503 ms
64,836 KB
testcase_10 AC 1,408 ms
63,500 KB
testcase_11 AC 1,070 ms
62,900 KB
testcase_12 AC 1,101 ms
62,964 KB
testcase_13 AC 1,105 ms
62,652 KB
testcase_14 AC 1,351 ms
63,776 KB
testcase_15 AC 1,125 ms
63,028 KB
testcase_16 AC 1,220 ms
63,360 KB
testcase_17 AC 1,424 ms
64,208 KB
testcase_18 AC 1,410 ms
63,972 KB
testcase_19 AC 1,060 ms
62,924 KB
testcase_20 AC 982 ms
62,592 KB
testcase_21 AC 1,311 ms
63,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.io.StdIn.*
import ModInt.*
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 asInt(using inline mod: Int): Int = asLong.toInt
    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

type Matrix = Array[Array[ModInt]]
extension (left: Matrix)
  inline infix def *(right: Matrix)(using inline mod: Int): Matrix =
    val row = left.length
    val column = right(0).length
    val mid = right.length
    Array.tabulate(row){i => Array.tabulate(column){j =>
      (0 until mid).foldLeft(0.asModInt){(acc, k) => acc + left(i)(k) * right(k)(j)}
    }}

extension (matrix: Matrix)
  inline def pow(exp: Long)(using inline mod: Int): Matrix =
    val size = matrix.length
    var result = Array.tabulate(size){i => Array.tabulate(size){j => if i == j then 1.asModInt else 0.asModInt}}
    var base = matrix
    var e = exp
    while e > 0 do
      if (e & 1) == 1 then
        result = result * base
      base = base * base
      e = e >> 1
    result

@main def main =
  inline given mod: Int = 1000000007
  val Array(a, b) = readLine().split(' ').map(_.toInt.asModInt)
  val n = readLine().toInt
  val time = Array.fill(n){readLine().toLong}
  val initial = Array.tabulate(3){i => if i == 2 then Array(0.asModInt) else Array(1.asModInt)}
  val oddOperation = Array(Array(1.asModInt, 0.asModInt, 0.asModInt), Array(0.asModInt, 1.asModInt, 0.asModInt), Array(a, b, 1.asModInt))
  val evenOperation = Array(Array(0.asModInt, 0.asModInt, 1.asModInt), Array(1.asModInt, 0.asModInt, 0.asModInt), Array(0.asModInt, 0.asModInt, 0.asModInt))
  val result = Array.fill(n){0}
  for (t, i) <- time.zipWithIndex do
    val operation = if t % 2 == 0 then (evenOperation * oddOperation).pow(t / 2) else oddOperation * (evenOperation * oddOperation).pow(t / 2)
    val lastState = operation * initial
    result(i) = lastState.flatten.reduce(_ + _).asInt
  println(result.mkString("\n"))
0