結果

問題 No.1785 Inequality Signs
ユーザー yudedakoyudedako
提出日時 2022-01-26 15:24:17
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 948 ms / 2,000 ms
コード長 2,673 bytes
コンパイル時間 12,293 ms
コンパイル使用メモリ 269,212 KB
実行使用メモリ 66,720 KB
最終ジャッジ日時 2024-06-02 09:36:11
合計ジャッジ時間 62,920 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 805 ms
63,320 KB
testcase_01 AC 876 ms
63,356 KB
testcase_02 AC 930 ms
65,580 KB
testcase_03 AC 931 ms
66,412 KB
testcase_04 AC 825 ms
63,368 KB
testcase_05 AC 882 ms
64,824 KB
testcase_06 AC 936 ms
66,288 KB
testcase_07 AC 851 ms
63,364 KB
testcase_08 AC 928 ms
66,012 KB
testcase_09 AC 851 ms
63,480 KB
testcase_10 AC 912 ms
65,556 KB
testcase_11 AC 855 ms
63,896 KB
testcase_12 AC 941 ms
64,444 KB
testcase_13 AC 853 ms
63,428 KB
testcase_14 AC 881 ms
65,000 KB
testcase_15 AC 897 ms
64,660 KB
testcase_16 AC 865 ms
64,168 KB
testcase_17 AC 903 ms
65,256 KB
testcase_18 AC 923 ms
65,200 KB
testcase_19 AC 874 ms
64,888 KB
testcase_20 AC 893 ms
65,096 KB
testcase_21 AC 866 ms
64,888 KB
testcase_22 AC 850 ms
63,768 KB
testcase_23 AC 883 ms
64,152 KB
testcase_24 AC 935 ms
66,496 KB
testcase_25 AC 891 ms
64,684 KB
testcase_26 AC 893 ms
64,408 KB
testcase_27 AC 939 ms
66,504 KB
testcase_28 AC 922 ms
66,272 KB
testcase_29 AC 934 ms
66,720 KB
testcase_30 AC 948 ms
66,584 KB
testcase_31 AC 948 ms
66,584 KB
testcase_32 AC 918 ms
66,380 KB
testcase_33 AC 793 ms
63,052 KB
testcase_34 AC 842 ms
63,308 KB
testcase_35 AC 832 ms
63,256 KB
testcase_36 AC 910 ms
65,988 KB
testcase_37 AC 926 ms
66,404 KB
testcase_38 AC 914 ms
66,112 KB
testcase_39 AC 882 ms
63,744 KB
testcase_40 AC 884 ms
64,504 KB
testcase_41 AC 912 ms
65,184 KB
testcase_42 AC 924 ms
66,648 KB
testcase_43 AC 921 ms
66,312 KB
testcase_44 AC 911 ms
66,184 KB
testcase_45 AC 881 ms
64,304 KB
testcase_46 AC 899 ms
65,964 KB
testcase_47 AC 876 ms
64,940 KB
testcase_48 AC 928 ms
65,300 KB
testcase_49 AC 830 ms
63,372 KB
testcase_50 AC 920 ms
65,852 KB
testcase_51 AC 842 ms
63,320 KB
testcase_52 AC 884 ms
63,108 KB
testcase_53 AC 858 ms
63,328 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

@main def main =
  import ModInt.*
  inline given mod: Int = 1000000007
  val Array(n, k) = readLine().split(' ').map(_.toInt)
  val factorial = Array.fill(n + 1){1.asModInt}
  for i <- 2 to n do
    factorial(i) = i * factorial(i - 1)
  val inverse = factorial.clone()
  inverse(n) = inverse(n).inverse
  for i <- n to 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 current = (k + n - 1 to k by -1).foldLeft(1.asModInt){_ * _} * inverse(n)
  val maxLt = min(n - 1, k - 1)
  var result = current
  for lt <- 1 to maxLt do
    current = current * (k - lt) * (n - lt) / ((k + n - lt).asModInt * lt)
    result += current
  println(result.asLong)

0