結果

問題 No.391 CODING WAR
ユーザー taq225taq225
提出日時 2020-03-09 00:42:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 827 ms / 2,000 ms
コード長 2,297 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 49,920 KB
最終ジャッジ日時 2024-11-07 20:21:21
合計ジャッジ時間 7,320 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,880 KB
testcase_01 AC 31 ms
10,752 KB
testcase_02 AC 30 ms
10,880 KB
testcase_03 AC 31 ms
10,880 KB
testcase_04 AC 30 ms
10,880 KB
testcase_05 AC 32 ms
10,752 KB
testcase_06 AC 30 ms
10,880 KB
testcase_07 AC 31 ms
10,880 KB
testcase_08 AC 31 ms
10,880 KB
testcase_09 AC 827 ms
49,880 KB
testcase_10 AC 711 ms
49,892 KB
testcase_11 AC 600 ms
49,920 KB
testcase_12 AC 31 ms
10,880 KB
testcase_13 AC 773 ms
49,884 KB
testcase_14 AC 637 ms
35,072 KB
testcase_15 AC 722 ms
36,992 KB
testcase_16 AC 445 ms
30,080 KB
testcase_17 AC 506 ms
32,132 KB
testcase_18 AC 355 ms
22,100 KB
testcase_19 AC 362 ms
22,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
    
class Combinatorics:
    def __init__(self, N, mod):
        '''
        Preprocess for calculating binomial coefficients nCr (0 <= r <= n, 0 <= n <= N)
        over the finite field Z/(mod)Z.
        Input:
            N (int): maximum n
            mod (int): a prime number. The order of the field Z/(mod)Z over which nCr is calculated.
        '''
        self.mod = mod
        self.fact = {i: None for i in range(N+1)}         # n!
        self.inverse = {i: None for i in range(1, N+1)}   # inverse of n in the field Z/(MOD)Z
        self.fact_inverse = {i: None for i in range(N+1)} # inverse of n! in the field Z/(MOD)Z
        
        # preprocess
        self.fact[0] = self.fact[1] = 1
        self.fact_inverse[0] = self.fact_inverse[1] = 1
        self.inverse[1] = 1
        for i in range(2, N+1):
            self.fact[i] = i * self.fact[i-1] % self.mod
            q, r = divmod(self.mod, i)
            self.inverse[i] = (- (q % self.mod) * self.inverse[r]) % self.mod
            self.fact_inverse[i] = self.inverse[i] * self.fact_inverse[i-1] % self.mod
    
    def perm(self, n, r):
        '''
        Calculate nPr = n! / (n-r)! % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return (self.fact[n] * self.fact_inverse[n-r]) % self.mod
    
    def binom(self, n, r):
        '''
        Calculate nCr = n! /(r! (n-r)!) % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return self.fact[n] * (self.fact_inverse[r] * self.fact_inverse[n-r] % self.mod) % self.mod
        
    def hom(self, n, r):
        '''
        Calculate nHr = {n+r-1}Cr % mod.
        Assign r objects to one of n classes.
        Arrangement of r circles and n-1 partitions:
            o o o | o o | | | o | | | o o | | o
        '''
        if n == 0 and r > 0:
            return 0
        if n >= 0 and r == 0:
            return 1
        return self.binom(n + r - 1, r)
    
N, M = map(int, input().split())
MOD = 10**9 + 7
com = Combinatorics(M, MOD)
ans = 0
for i in range(M):
    if i % 2 == 0:
        ans = (ans + com.binom(M, i) * pow(M - i, N, MOD)) % MOD
    else:
        ans = (ans - com.binom(M, i) * pow(M - i, N, MOD)) % MOD
print(ans)
0