結果

問題 No.1049 Zero (Exhaust)
ユーザー yudedakoyudedako
提出日時 2022-01-26 17:39:32
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 983 ms / 2,000 ms
コード長 2,999 bytes
コンパイル時間 15,138 ms
コンパイル使用メモリ 259,460 KB
実行使用メモリ 62,724 KB
最終ジャッジ日時 2023-08-24 23:17:00
合計ジャッジ時間 42,692 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 941 ms
62,320 KB
testcase_01 AC 983 ms
62,532 KB
testcase_02 AC 974 ms
62,444 KB
testcase_03 AC 973 ms
62,036 KB
testcase_04 AC 954 ms
62,548 KB
testcase_05 AC 978 ms
62,332 KB
testcase_06 AC 955 ms
62,708 KB
testcase_07 AC 965 ms
62,104 KB
testcase_08 AC 957 ms
62,536 KB
testcase_09 AC 949 ms
62,524 KB
testcase_10 AC 966 ms
62,724 KB
testcase_11 AC 964 ms
62,316 KB
testcase_12 AC 943 ms
61,988 KB
testcase_13 AC 943 ms
62,460 KB
testcase_14 AC 962 ms
62,592 KB
testcase_15 AC 948 ms
62,372 KB
testcase_16 AC 944 ms
62,512 KB
testcase_17 AC 957 ms
62,476 KB
testcase_18 AC 975 ms
62,464 KB
testcase_19 AC 944 ms
62,576 KB
testcase_20 AC 966 ms
62,020 KB
testcase_21 AC 980 ms
62,272 KB
testcase_22 AC 962 ms
62,500 KB
testcase_23 AC 962 ms
62,384 KB
testcase_24 AC 967 ms
62,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.annotation.tailrec
import scala.io.StdIn.*
import scala.math.*
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

import ModInt.*
type Matrix = Array[Array[ModInt]]

extension (left: Matrix)
  inline 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)}
      }
    }
  inline def pow(exp: Int)(using inline mod: Int): Matrix =
    var result = Array.tabulate(left.length){i => Array.tabulate(left.length){j => if i == j then 1.asModInt else 0.asModInt}}
    var e = exp
    var base = left
    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(p, k) = readLine().split(' ').map(_.toInt)
  val init = Array(Array(1.asModInt), Array(2.asModInt))
  val op = Array(Array((p - 1).asModInt, 1.asModInt), Array(0.asModInt, 2 * p.asModInt))
  val Array(Array(result), _) = op.pow(k) * init
  println(result)
0