제출 #1086415

#제출 시각아이디문제언어결과실행 시간메모리
1086415WhisperBoat (APIO16_boat)C++17
100 / 100
866 ms8532 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX             2505
#define LIM             5005
int nArr;
vector<int> seg;
int l[MAX], r[MAX];

int dp[MAX * 2][MAX];
int inv[MAX];
int power(int a, int exp){
    int res = 1;
    for (; exp > 0; exp >>= 1, a = (1LL * a * a) % Mod) if(exp & 1){
        res = (1LL * res * a) % Mod;
    }
    return res;
}
void add(int &x, int y){
    x += y;
    if (x >= Mod) x %= Mod;
    while(x < 0) x += Mod;
}

int mul(int x, int y){
    return 1LL * x * y % Mod;
}

void process(void){
    cin >> nArr;
    for (int i = 1; i <= nArr; ++i){
        cin >> l[i] >> r[i];
        seg.emplace_back(l[i]);
        seg.emplace_back(r[i] + 1);
    }

    seg.emplace_back(0);
    sort(seg.begin(), seg.end());
    seg.resize(unique(seg.begin(), seg.end()) - seg.begin());

    for (int i = 0; i <= nArr; ++i) inv[i] = power(i, Mod - 2);
    for (int i = 0; i <= nArr; ++i) dp[0][i] = 1;
    int n = (int)seg.size();

    for (int i = 1; i < n - 1; ++i){
        int x = seg[i + 1] - seg[i];
        for (int pos = 0; pos <= nArr; ++pos){
            dp[i][pos] = dp[i - 1][pos];
            int nCk = 1, cnt = 0;
            for (int last = pos; last >= 1; --last){
                if (l[last] <= seg[i] && seg[i + 1] - 1 <= r[last]){
                    ++cnt;
                    nCk = mul(nCk, x + cnt - 1) * inv[cnt] % Mod;
                    add(dp[i][pos], mul(dp[i - 1][last - 1], nCk));
//                    add(dp[i][pos], mul(dp[i - 1][last - 1], C[x + cnt - 1][cnt]));
                }
            }
        }
    }
    /*
        C(x + cnt - 1, cnt)

        (x + cnt - 1)! / cnt! . (x - 1)!

        (x - 1 + 1) * (x - 1 + 2) .. * (x - 1 + cnt) / cnt!

    */
    cout << (dp[n - 2][nArr] - 1 + Mod) % Mod;
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    // freopen(name".inp", "r", stdin);
    // freopen(name".out", "w", stdout);
    process();
    return (0 ^ 0);
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...