結果

問題 No.1001 注文の多い順列
ユーザー petite_progpetite_prog
提出日時 2020-03-09 21:33:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 4,839 bytes
コンパイル時間 1,336 ms
コンパイル使用メモリ 117,864 KB
実行使用メモリ 73,612 KB
最終ジャッジ日時 2023-08-09 02:37:42
合計ジャッジ時間 4,579 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 78 ms
61,380 KB
testcase_19 AC 69 ms
55,392 KB
testcase_20 AC 44 ms
36,836 KB
testcase_21 AC 41 ms
33,416 KB
testcase_22 AC 93 ms
73,520 KB
testcase_23 AC 92 ms
73,248 KB
testcase_24 AC 94 ms
72,992 KB
testcase_25 AC 90 ms
71,356 KB
testcase_26 AC 91 ms
70,316 KB
testcase_27 AC 92 ms
71,756 KB
testcase_28 AC 93 ms
72,024 KB
testcase_29 AC 89 ms
72,728 KB
testcase_30 AC 85 ms
69,576 KB
testcase_31 AC 90 ms
72,696 KB
testcase_32 AC 89 ms
73,612 KB
testcase_33 AC 98 ms
73,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
   ∫ ∫ ∫
   ノヽ
  (_  )
 (_    )
(______ )
 ヽ(´・ω・)ノ 
   |  /
   UU
*/
#pragma region macro
#include <iostream>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<cassert>
#include<cmath>
#include<climits>
#include<iomanip>
#include<bitset>
#include<unordered_map>
#include<tuple>
typedef long long int64;
using namespace std;
using P = pair<int64, int64>;
typedef vector<int> vi;
const int MOD = (int)1e9 + 7;
const int64 INF = 1LL << 62;
const int inf = 1<<30;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = s; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end() //コンテナじゃないと使えない!!
#define debug(x) cerr << #x << ": " << x << "\n";
#define mp make_pair
#define bn '\n'
template <typename T>
ostream& operator<<(ostream& os, const vector<T> &V){
    int N = V.size();
    REP(i,N){
        os << V[i];
        if (i!=N-1) os << " ";
    }
    os << "\n";
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, pair<T,S> const&P){
    os << "(";
    os << P.first;
    os << " , ";
    os << P.second;
    os << ")";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> &S){
    auto it=S.begin();
    while(it!=S.end()){
        os << *it;
        os << " ";
        it++;
    }
    os << "\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T> &q){
    for(auto it=q.begin();it<q.end();it++){
        os<<*it;
        os<<" ";
    }
     os<<endl;
    return os;
}
vector<pair<int,int>> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)};
#pragma endregion
//fixed<<setprecision(10)<<ans<<endl;

int64 pow(int a,int b,int mod){
    vector<bool> bit;
    for(b=b;b>0;b>>=1){
        bit.push_back(b&1);
    }
    vector<int64> fac(bit.size()); fac[0] = a;
    int64 res = 1;
    for(int i=1;i<bit.size();i++){
        fac[i] = (fac[i-1] * fac[i-1])%mod;
    }
    for(int i=0;i<bit.size();i++){
        if(bit[i]) res*=fac[i];
        res%=mod;
    }
    return res;
}


//mint
struct mint {
    int64 x;
    mint(int64 x=0):x((x+2*MOD)%MOD){}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(int64 t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime MOD
    mint inv() const {
        return pow(MOD-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};
ostream& operator<<(ostream& os, mint a){
    os << a.x;
    return os;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<pair<bool,int>> BAN_limit;
    int b,l;
    int ban_cnt = 0;
    REP(i,N){
        cin >> b >> l;
        if(b){ l--; ban_cnt++;}
        BAN_limit.emplace_back(b,l);
    }

    sort(ALL(BAN_limit),[](pair<bool,int> a,pair<bool,int> b)->bool{
        return a.second < b.second;
    });

    bool ban;
    int limit;
    vector<vector<mint>> DP(N+1,vector<mint>(N+1,0)); //iまで見て、制約を満たす数がj
    DP[0][0] = 1;
    REP(i,N){
        tie(ban,limit) = BAN_limit[i];
        REP(j,N){
            //それが双対かどうかは別として条件を満たしている推移
            DP[i+1][j+1] += DP[i][j] * max(0, limit-j);
            
            //双対のとき、条件を満たさない推移もカウントする
            if(ban){
                DP[i+1][j] += DP[i][j];
            }
        }
    }
    vector<mint> fact(N+1,1);
    for(int i=1;i<=N;i++){
        fact[i] = fact[i-1] * i;
    }

    REP(j,N){
        DP[N][j] *= fact[N-j];
    }

    mint ans = 0;
    REP(j,N+1){
        if(N-j <= ban_cnt){
            if((ban_cnt-(N-j))&1){
                ans -= DP[N][j];
            }else{
                ans += DP[N][j];
            }
        }
    }
    cout << ans << endl;
}
0