結果

問題 No.117 組み合わせの数
ユーザー titiatitia
提出日時 2022-02-10 01:27:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 616 ms / 5,000 ms
コード長 869 bytes
コンパイル時間 375 ms
コンパイル使用メモリ 86,880 KB
実行使用メモリ 256,964 KB
最終ジャッジ日時 2023-09-07 12:00:35
合計ジャッジ時間 2,170 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

T=int(input())

mod=10**9+7

FACT=[1]
for i in range(1,2*10**6+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(2*10**6,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]%mod*FACT_INV[a-b]%mod
    else:
        return 0

for tests in range(T):
    x=input()

    if x[0]=="C":
        y,z=map(int,x[2:-1].split(","))

        if z>y:
            print(0)
            continue

        print(Combi(y,z))
        
    elif x[0]=="P":
        y,z=map(int,x[2:-1].split(","))

        if z>y:
            print(0)
            continue

        print(FACT[y]*pow(FACT[y-z],mod-2,mod)%mod)

    else:
        y,z=map(int,x[2:-1].split(","))

        if y==z==0:
            print(1)
            continue

        print(Combi(y+z-1,z))
        
        
0