結果

問題 No.1001 注文の多い順列
ユーザー tsutajtsutaj
提出日時 2020-02-23 12:28:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,921 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 48,824 KB
最終ジャッジ日時 2024-04-18 08:32:23
合計ジャッジ時間 993 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:62:1: error: 'ostream' does not name a type
   62 | ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;}
      | ^~~~~~~
main.cpp:64:1: error: 'istream' does not name a type
   64 | istream& operator>> (istream& in, ModInt<mod>& a) {
      | ^~~~~~~

ソースコード

diff #

#include <cstdio>
#include <utility>
#include <algorithm>
#include <vector>
#include <tuple>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;

// ModInt begin

template<ll mod>
struct ModInt {
    ll v;
    ll mod_pow(ll x, ll n) const {
        return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod;
    }
    ModInt(ll a = 0) : v((a %= mod) < 0 ? a + mod : a) {}
    ModInt operator+ ( const ModInt& b ) const {
        return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v));
    }
    ModInt operator- () const {
        return ModInt(-v);
    }
    ModInt operator- ( const ModInt& b ) const {
        return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v));
    }
    ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;}
    ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;}
    
    bool operator== ( const ModInt &b ) const {return v == b.v;}
    bool operator!= ( const ModInt &b ) const {return !(*this == b); }
    ModInt& operator+= ( const ModInt &b ) {
        v += b.v;
        if(v >= mod) v -= mod;
        return *this;
    }
    ModInt& operator-= ( const ModInt &b ) {
        v -= b.v;
        if(v < 0) v += mod;
        return *this;
    }
    ModInt& operator*= ( const ModInt &b ) {
        (v *= b.v) %= mod;
        return *this;
    }
    ModInt& operator/= ( const ModInt &b ) {
        (v *= mod_pow(b.v, mod-2)) %= mod;
        return *this;
    }
    ModInt pow(ll x) { return ModInt(mod_pow(v, x)); }
    // operator int() const { return int(v); }
    // operator long long int() const { return v; }
};

template<ll mod>
ModInt<mod> pow(ModInt<mod> n, ll k) {
    return ModInt<mod>(n.mod_pow(n.v, k));
}

template<ll mod>
ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;}
template<ll mod>
istream& operator>> (istream& in, ModInt<mod>& a) {
    in >> a.v;
    return in;
}

// ModInt end

using mint = ModInt<MOD>;

mint dp[3010][3010];
int main() {
    int N; scanf("%d", &N);
    int K = 0;
    
    vector< pair<int, int> > info;
    for(int i=0; i<N; i++) {
        int t, x;
        scanf("%d%d", &t, &x);
        if(t == 1) x--, K++;
        info.emplace_back(x, t);
    }
    sort(info.begin(), info.end());

    fill(dp[0], dp[N+1], mint(0));
    dp[0][0] = mint(1);
    for(int i=0; i<N; i++) {
        for(int j=0; j<=i; j++) {
            int x, t; tie(x, t) = info[i];
            int free_sp = max(0, x - i + j);
            if(t == 0) {
                dp[i+1][j] += dp[i][j] * free_sp;
            }
            if(t == 1) {
                dp[i+1][j] += dp[i][j] * free_sp;
                dp[i+1][j+1] += dp[i][j];
            }
        }
    }

    mint P(1), ans(0);
    for(int i=0; i<=K; i++) {
        if((K-i) % 2) ans -= dp[N][i] * P;
        else ans += dp[N][i] * P;
        P *= mint(i+1);
    }
    printf("%lld\n", ans.v);
    return 0;
}
0