結果

問題 No.1001 注文の多い順列
ユーザー rniyarniya
提出日時 2020-03-03 09:58:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 3,690 bytes
コンパイル時間 1,900 ms
コンパイル使用メモリ 175,196 KB
実行使用メモリ 73,728 KB
最終ジャッジ日時 2024-10-13 21:49:48
合計ジャッジ時間 4,417 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 1 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 3 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 94 ms
61,440 KB
testcase_19 AC 90 ms
55,552 KB
testcase_20 AC 57 ms
36,864 KB
testcase_21 AC 50 ms
33,536 KB
testcase_22 AC 112 ms
73,728 KB
testcase_23 AC 113 ms
73,344 KB
testcase_24 AC 116 ms
72,960 KB
testcase_25 AC 112 ms
71,552 KB
testcase_26 AC 118 ms
70,528 KB
testcase_27 AC 119 ms
71,936 KB
testcase_28 AC 122 ms
72,064 KB
testcase_29 AC 108 ms
72,704 KB
testcase_30 AC 104 ms
69,760 KB
testcase_31 AC 109 ms
72,960 KB
testcase_32 AC 107 ms
73,728 KB
testcase_33 AC 120 ms
73,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD=1e9+7;

template<uint_fast64_t Modulus> class modint{
    using u64=uint_fast64_t;
    public:
    u64 a;
    constexpr modint(const u64 x=0) noexcept:a(((x%Modulus)+Modulus)%Modulus){}
    constexpr u64 &value() noexcept{return a;}
    constexpr const u64 &value() const noexcept{return a;}
    constexpr modint &operator+=(const modint &rhs) noexcept{
        a+=rhs.a;
        if (a>=Modulus) a-=Modulus;
        return *this;
    }
    constexpr modint operator+(const modint &rhs) const noexcept{
        return modint(*this)+=rhs;
    }
    constexpr modint &operator++() noexcept{
        return ++a,*this;
    }
    constexpr modint operator++(int) noexcept{
        modint t=*this; return ++a,t;
    }
    constexpr modint &operator-=(const modint &rhs) noexcept{
        if (a<rhs.a) a+=Modulus;
        a-=rhs.a;
        return *this;
    }
    constexpr modint operator-(const modint &rhs) const noexcept{
        return modint(*this)-=rhs;
    }
    constexpr modint &operator--() noexcept{
        return --a,*this;
    }
    constexpr modint operator--(int) noexcept{
        modint t=*this; return --a,t;
    }
    constexpr modint &operator*=(const modint &rhs) noexcept{
        a=a*rhs.a%Modulus;
        return *this;
    }
    constexpr modint operator*(const modint &rhs) const noexcept{
        return modint(*this)*=rhs;
    }
    constexpr modint &operator/=(modint rhs) noexcept{
        u64 exp=Modulus-2;
        while(exp){
            if (exp&1) *this*=rhs;
            rhs*=rhs; exp>>=1;
        }
        return *this;
    }
    constexpr modint operator/(const modint &rhs) const noexcept{
        return modint(*this)/=rhs;
    }
    constexpr modint operator-() const noexcept{
        return modint(Modulus-a);
    }
    constexpr bool operator==(const modint &rhs) const noexcept{
        return a==rhs.a;
    }
    constexpr bool operator!=(const modint &rhs) const noexcept{
        return a!=rhs.a;
    }
    constexpr bool operator!() const noexcept{return !a;}
    friend constexpr modint pow(modint rhs,long long exp) noexcept{
        modint res{1};
        while(exp){
            if (exp&1) res*=rhs;
            rhs*=rhs; exp>>=1;
        }
        return res;
    }
    template<class T> friend constexpr modint operator+(T x,modint y) noexcept{
        return modint(x)+y;
    }
    template<class T> friend constexpr modint operator-(T x,modint y) noexcept{
        return modint(x)-y;
    }
    template<class T> friend constexpr modint operator*(T x,modint y) noexcept{
        return modint(x)*y;
    }
    template<class T> friend constexpr modint operator/(T x,modint y) noexcept{
        return modint(x)/y;
    }
    friend ostream &operator<<(ostream &s,const modint &rhs) noexcept {
        return s << rhs.a;
    }
    friend istream &operator>>(istream &s,modint &rhs) noexcept {
        u64 a; rhs=modint{(s >> a,a)}; return s;
    }
};

using mint=modint<MOD>;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    vector<vector<int>> Xs(2);
    for (int i=0;i<N;++i){
        int t,X; cin >> t >> X;
        Xs[t].emplace_back(--X);
    }
    vector<int> cnt0(N,0),cnt1(N,0);
    for (int i=0;i<N;++i){
        for (int X:Xs[0]) cnt0[i]+=(i<=X);
        for (int X:Xs[1]) cnt1[i]+=(X<=i);
    }
    int s=Xs[0].size(),t=Xs[1].size();
    vector<vector<mint>> dp(N+1,vector<mint>(N+1,0));
    dp[0][0]=1;
    for (int i=0;i<N;++i){
        for (int j=0;j<N;++j){
            if (i<s) dp[i+1][j]+=dp[i][j]*(cnt0[i+j]-s+i+1);
            if (j<t) dp[i][j+1]+=dp[i][j]*(cnt1[i+j]-j);
        }
    }
    cout << dp[s][t] << '\n';
}
0