結果

問題 No.1001 注文の多い順列
ユーザー mkawa2mkawa2
提出日時 2020-02-29 10:52:44
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,253 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 523,832 KB
最終ジャッジ日時 2024-04-21 21:30:05
合計ジャッジ時間 11,957 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,096 KB
testcase_01 AC 42 ms
52,480 KB
testcase_02 AC 42 ms
52,608 KB
testcase_03 AC 45 ms
52,864 KB
testcase_04 AC 44 ms
52,608 KB
testcase_05 AC 43 ms
52,352 KB
testcase_06 AC 42 ms
52,224 KB
testcase_07 AC 42 ms
52,608 KB
testcase_08 AC 43 ms
52,480 KB
testcase_09 AC 43 ms
52,608 KB
testcase_10 AC 44 ms
52,480 KB
testcase_11 AC 44 ms
52,736 KB
testcase_12 AC 45 ms
52,608 KB
testcase_13 AC 65 ms
65,408 KB
testcase_14 AC 69 ms
68,096 KB
testcase_15 AC 74 ms
70,656 KB
testcase_16 AC 68 ms
67,072 KB
testcase_17 AC 66 ms
66,432 KB
testcase_18 AC 534 ms
284,160 KB
testcase_19 AC 424 ms
259,612 KB
testcase_20 AC 284 ms
188,800 KB
testcase_21 AC 262 ms
171,828 KB
testcase_22 AC 676 ms
323,584 KB
testcase_23 AC 617 ms
318,208 KB
testcase_24 AC 610 ms
316,856 KB
testcase_25 AC 593 ms
312,212 KB
testcase_26 AC 1,300 ms
501,860 KB
testcase_27 AC 1,281 ms
496,000 KB
testcase_28 AC 1,175 ms
480,384 KB
testcase_29 AC 171 ms
114,480 KB
testcase_30 AC 193 ms
129,664 KB
testcase_31 AC 255 ms
148,256 KB
testcase_32 AC 83 ms
72,836 KB
testcase_33 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

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