結果

問題 No.117 組み合わせの数
ユーザー takakintakakin
提出日時 2020-06-11 00:44:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,084 ms / 5,000 ms
コード長 807 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 10,880 KB
実行使用メモリ 164,856 KB
最終ジャッジ日時 2023-09-06 01:57:37
合計ジャッジ時間 4,774 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,084 ms
164,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
mod=10**9+7
n_max=2*(10**6+1)
F,FI=[0]*(n_max+1),[0]*(n_max+1)
F[0],FI[0]=1,1
for i in range(n_max):
  F[i+1]=(F[i]*(i+1))%mod
FI[n_max-1]=pow(F[n_max-1],mod-2,mod)
for i in reversed(range(n_max-1)):
  FI[i]=(FI[i+1]*(i+1))%mod
def comb(x,y):
  return (F[x]*FI[x-y]*FI[y])%mod
t=int(input())
N=set(str(i) for i in range(10))
for _ in range(t):
  S=input()
  a,b="",""
  st=0
  for s in S:
    if s==",":
      st=1
    if s in N:
      if st==0:
        a+=s
      else:
        b+=s
  a,b=int(a),int(b)
  if S[0]=="C":
    if a<b:
      print(0)
    else:
      print(comb(a,b))
  elif S[0]=="P":
    if a<b:
      print(0)
    else:
      print((F[a]*FI[a-b])%mod)
  else:
    if a==0 and b==0:
      print(1)
    else:
      print(comb(a+b-1,a-1))

0