結果
| 問題 |
No.117 組み合わせの数
|
| ユーザー |
|
| 提出日時 | 2015-01-11 00:45:54 |
| 言語 | Scala(Beta) (3.6.2) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,414 bytes |
| コンパイル時間 | 9,320 ms |
| コンパイル使用メモリ | 264,696 KB |
| 実行使用メモリ | 243,592 KB |
| 最終ジャッジ日時 | 2024-06-28 22:10:24 |
| 合計ジャッジ時間 | 16,095 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 1 |
ソースコード
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"))
}