結果

問題 No.117 組み合わせの数
ユーザー gigurururugigurururu
提出日時 2015-01-11 00:45:54
言語 Scala(Beta)
(3.4.0)
結果
WA  
実行時間 -
コード長 1,414 bytes
コンパイル時間 11,811 ms
コンパイル使用メモリ 254,304 KB
実行使用メモリ 239,436 KB
最終ジャッジ日時 2023-09-11 07:42:17
合計ジャッジ時間 18,585 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.mutable
import scala.io.StdIn

object Main extends App{
  def memo[argT2,retT](f:(Int,argT2)=>retT):(Int,argT2)=>retT = {
    val m = mutable.ArrayBuffer.empty[retT]
    (arg1,arg2) => {
      (m.size to arg1).foreach(i=>m+=f(i,arg2))
      if(arg1<0) f(arg1,arg2) else m(arg1)
    }
  }
  var inv: (Int,Int)=>Long = memo{
    case (m: Int, mod: Int) if m > 1 => ((mod - mod / m) * inv(mod % m, mod)) % mod
    case _ => 1
  }
  var fact: (Int,Int)=>Long = memo {
    case (m: Int, mod: Int) if m > 0 => (m * fact(m - 1, mod)) % mod
    case _ => 1
  }
  var factr: (Int,Int)=>Long = memo {
    case (m: Int, mod: Int) if m > 0 => (inv(m, mod) * factr(m - 1, mod)) % mod
    case (0, _) => 1
    case _ => 0
  }
  def combination_len(n:Int,r:Int,mod:Int): Int           = (fact(n,mod)*factr(r,mod)%mod*factr(n-r,mod)%mod).toInt
  def repeated_combination_len(n:Int,r:Int,mod:Int): Int  = combination_len(n+r-1,r,mod)
  def permutation_len(n:Int,r:Int,mod:Int): Int           = (fact(n,mod)*factr(n-r,mod)%mod).toInt
  val mod = 1000000007
  val n = StdIn.readInt()
  val ans = Array.fill(n){
    val a = StdIn.readLine().split("\\(|\\)|,")
    a(0) match {
      case "C" => combination_len(a(1).toInt,a(2).toInt,mod)
      case "P" => permutation_len(a(1).toInt,a(2).toInt,mod)
      case "H" => repeated_combination_len(a(1).toInt,a(2).toInt,mod)
    }
  }
  println(ans.mkString("\n"))
}
0