結果

問題 No.1832 NAND Reversible
ユーザー yudedakoyudedako
提出日時 2022-02-18 13:18:44
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,060 ms / 2,000 ms
コード長 3,381 bytes
コンパイル時間 11,331 ms
コンパイル使用メモリ 262,836 KB
実行使用メモリ 64,352 KB
最終ジャッジ日時 2023-09-11 18:08:56
合計ジャッジ時間 39,003 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 891 ms
61,660 KB
testcase_01 AC 920 ms
62,440 KB
testcase_02 AC 914 ms
61,636 KB
testcase_03 AC 1,024 ms
64,084 KB
testcase_04 AC 930 ms
61,260 KB
testcase_05 AC 923 ms
61,812 KB
testcase_06 AC 1,033 ms
63,712 KB
testcase_07 AC 1,034 ms
64,352 KB
testcase_08 AC 1,045 ms
63,780 KB
testcase_09 AC 914 ms
61,788 KB
testcase_10 AC 1,028 ms
64,156 KB
testcase_11 AC 901 ms
61,748 KB
testcase_12 AC 909 ms
61,576 KB
testcase_13 AC 936 ms
61,852 KB
testcase_14 AC 937 ms
62,036 KB
testcase_15 AC 1,004 ms
61,984 KB
testcase_16 AC 1,060 ms
64,152 KB
testcase_17 AC 1,042 ms
64,176 KB
testcase_18 AC 1,037 ms
64,316 KB
testcase_19 AC 1,056 ms
64,160 KB
testcase_20 AC 913 ms
61,844 KB
testcase_21 AC 1,036 ms
64,164 KB
testcase_22 AC 1,039 ms
64,200 KB
testcase_23 AC 920 ms
61,776 KB
testcase_24 AC 936 ms
62,064 KB
testcase_25 AC 1,038 ms
64,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import javax.print.attribute.standard.MediaSize.Other
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*
import scala.util.*

object ModInt:
  opaque type ModInt = Long
  inline def zero: ModInt = 0
  inline def 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 =
      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 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)
    inline def equals(other: ModInt)(using inline mod: Int): Boolean = modInt.asLong == other.asLong
  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 = 998244353
  val Array(n, k) = readLine.split(' ').map(_.toInt)
  if k == 0 || k == n then
    println(1)
  else if k == 1 then
    println(if n % 2 == 0 then 2 else n - 2)
  else
    val factorial = Array.fill(n + 1){ModInt.one}
    for i <- 1 to n do
      factorial(i) = i * factorial(i - 1)
    val inverse = Array.fill(n + 1){ModInt.one}.tap(_(n) = factorial(n).inverse)
    for i <- n to 1 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 = ModInt.zero
    for end <- 0 to n - k by 2 do
      val pattern = combination(n - k - end + k - 2, k - 2) * (end + 1)
      result += pattern
    println(result.asLong)
0