結果

問題 No.117 組み合わせの数
ユーザー chocoruskchocorusk
提出日時 2020-09-13 10:55:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,887 ms / 5,000 ms
コード長 737 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 10,812 KB
実行使用メモリ 165,892 KB
最終ジャッジ日時 2023-09-02 12:41:35
合計ジャッジ時間 4,573 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
readlines=sys.stdin.buffer.readlines

t=int(readline())
MOD=10**9+7
MAX=2*(10**6)
f=[1]*(MAX+1)
invf=[1]*(MAX+1)
for i in range(2, MAX+1):
    f[i]=f[i-1]*i%MOD
invf[MAX]=pow(f[MAX], MOD-2, MOD)
for i in range(MAX-1, -1, -1):
    invf[i]=invf[i+1]*(i+1)%MOD
def comb(x, y):
    if not (0<=y and y<=x):
        return 0
    return f[x]*invf[y]*invf[x-y]%MOD
import re
for _ in range(t):
    s=readline().decode()
    v=re.findall(r'\d+', s)
    x, y=int(v[0]), int(v[1])
    if s[0]=='C':
        print(comb(x, y))
    elif s[0]=='P':
        print(comb(x, y)*f[y]%MOD)
    else:
        if y==0:
            print(1)
        else:
            print(comb(x+y-1, y))
0