結果

問題 No.1846 Good Binary Matrix
ユーザー yudedakoyudedako
提出日時 2022-02-28 23:35:09
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,328 ms / 2,000 ms
コード長 3,499 bytes
コンパイル時間 14,713 ms
コンパイル使用メモリ 257,152 KB
実行使用メモリ 87,048 KB
最終ジャッジ日時 2023-09-21 08:34:07
合計ジャッジ時間 53,836 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 933 ms
62,572 KB
testcase_01 AC 935 ms
62,440 KB
testcase_02 AC 932 ms
62,096 KB
testcase_03 AC 943 ms
62,220 KB
testcase_04 AC 933 ms
62,204 KB
testcase_05 AC 930 ms
62,272 KB
testcase_06 AC 930 ms
62,224 KB
testcase_07 AC 931 ms
62,208 KB
testcase_08 AC 932 ms
62,260 KB
testcase_09 AC 934 ms
62,000 KB
testcase_10 AC 930 ms
62,444 KB
testcase_11 AC 924 ms
61,988 KB
testcase_12 AC 928 ms
62,628 KB
testcase_13 AC 919 ms
62,556 KB
testcase_14 AC 931 ms
61,944 KB
testcase_15 AC 930 ms
62,180 KB
testcase_16 AC 1,327 ms
81,056 KB
testcase_17 AC 1,323 ms
80,708 KB
testcase_18 AC 1,324 ms
86,360 KB
testcase_19 AC 1,309 ms
86,684 KB
testcase_20 AC 1,321 ms
80,544 KB
testcase_21 AC 1,123 ms
69,212 KB
testcase_22 AC 1,127 ms
70,064 KB
testcase_23 AC 1,040 ms
69,992 KB
testcase_24 AC 1,027 ms
66,512 KB
testcase_25 AC 1,257 ms
78,084 KB
testcase_26 AC 1,160 ms
70,424 KB
testcase_27 AC 1,073 ms
79,392 KB
testcase_28 AC 1,070 ms
72,896 KB
testcase_29 AC 1,122 ms
70,740 KB
testcase_30 AC 1,265 ms
77,084 KB
testcase_31 AC 1,233 ms
74,348 KB
testcase_32 AC 1,328 ms
77,544 KB
testcase_33 AC 1,311 ms
87,048 KB
testcase_34 AC 973 ms
62,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


object ModInt:
  opaque type ModInt = Long
  inline def zero: ModInt = 0
  inline def one: ModInt = 1
  extension (value: Long)
    inline def toModInt(using inline mod: Int): ModInt =  value % mod
    inline def unsafeAsModInt: ModInt = value
  extension (value: Int)
    inline def toModInt(using inline mod: Int): ModInt = value % mod
    inline def unsafeAsModInt: ModInt = value
  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.toModInt
  inline given Converter[Long] with
    override inline def convert(value: Long)(using inline mod: Int) = value.toModInt
  inline given Converter[ModInt] with
    override inline def convert(value: ModInt)(using inline mod: Int) = value
  extension (modInt: ModInt)
    inline def value(using inline mod: Int): Long = (modInt + mod) % mod
    inline def rawValue: Long = modInt
    inline def inverse(using inline mod: Int): ModInt =
      var x = modInt
      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 * modInt + d * mod
      require(x.abs == 1L)
      if x == 1 then a % mod else -a % mod
    inline def pow(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 pow(exp: Int)(using inline mod: Int): ModInt = pow(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)).toModInt
    inline def -(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) - summon[Converter[R]].convert(right)).toModInt
    inline def *(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right)).toModInt
    inline def /(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right).inverse).toModInt
  inline def powMod(base: Long, exp: Long)(using inline mod: Int): ModInt = base.toModInt.pow(exp)

inline given mod: Int = 1000000007
import ModInt.*

@main def main =
  val Array(height, width) = readLine.split(' ').map(_.toInt)
  val factorial = Array.fill(max(height, width) + 1){one}
  for i <- 2 until factorial.length do
    factorial(i) = i * factorial(i - 1)
  val inverse = Array.fill(factorial.length){one}.tap(_(factorial.length - 1) = factorial.last.inverse)
  for i <- inverse.length - 1 until 2 by -1 do
    inverse(i - 1) = i * inverse(i)
  def combination(n: Int, r: Int): ModInt = factorial(n) * inverse(r) * inverse(n - r)
  var result = zero
  for i <- 0 to height do
    val value = combination(height, i) * (1 - powMod(2, height - i)).pow(width)
    if (i + width) % 2 == 0 then
      result += value
    else
      result -= value
  println(result.value)
0