結果

問題 No.1001 注文の多い順列
ユーザー mkawa2mkawa2
提出日時 2020-02-29 10:52:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,284 bytes
コンパイル時間 624 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 195,868 KB
最終ジャッジ日時 2024-04-21 21:29:51
合計ジャッジ時間 4,572 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
16,384 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 28 ms
11,008 KB
testcase_03 AC 28 ms
10,880 KB
testcase_04 AC 26 ms
10,880 KB
testcase_05 AC 28 ms
11,008 KB
testcase_06 AC 27 ms
10,880 KB
testcase_07 AC 27 ms
10,880 KB
testcase_08 AC 27 ms
10,880 KB
testcase_09 AC 26 ms
10,880 KB
testcase_10 AC 26 ms
10,880 KB
testcase_11 AC 26 ms
11,008 KB
testcase_12 AC 28 ms
10,880 KB
testcase_13 AC 33 ms
11,264 KB
testcase_14 AC 46 ms
12,032 KB
testcase_15 AC 63 ms
13,440 KB
testcase_16 AC 43 ms
11,904 KB
testcase_17 AC 42 ms
11,776 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class mint:
    def __init__(self, x):
        self.__x = x % md

    def __str__(self):
        return str(self.__x)

    def __add__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x + other)

    def __sub__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x - other)

    def __rsub__(self, other):
        return mint(other - self.__x)

    def __mul__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * other)

    __radd__ = __add__
    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * pow(other, md - 2, md))

    def __rtruediv__(self, other):
        return mint(other * pow(self.__x, md - 2, md))

    def __pow__(self, power, modulo=None):
        return mint(pow(self.__x, power, md))

md = 10**9+7

def main():
    n = II()
    xt = []
    one = 0
    for _ in range(n):
        t, x = MI()
        if t:
            x -= 1
            one += 1
        xt.append((x, t))
    tt = []
    xx = []
    for x, t in sorted(xt):
        tt.append(t)
        xx.append(x)
    #print(tt)
    #print(xx)
    # dp[i][j]...i個まで見たとき、t=1の処理をj個飛ばしたときの場合の数
    dp = [[0] * (one + 1) for _ in range(n + 1)]
    dp[0][0] = mint(1)
    for i in range(n):
        for j in range(min(i, one) + 1):
            pre = dp[i][j]
            if pre == 0: continue
            # 処理する
            dp[i + 1][j] += (xx[i] - i + j) * pre
            # 処理しない
            if tt[i]: dp[i + 1][j + 1] += pre
    #p2D(dp)
    fac=[mint(1)]
    for i in range(1,one+1):fac.append(fac[-1]*i)
    ans=mint(0)
    for j in range(one+1):
        if (one-j)&1:ans-=dp[n][j]*fac[j]
        else:ans+=dp[n][j]*fac[j]
    print(ans)

main()
0