제출 #1336613

#제출 시각아이디문제언어결과실행 시간메모리
1336613sh_qaxxorov_571Boat (APIO16_boat)C++20
100 / 100
1412 ms456 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

long long MOD = 1000000007;

// Modular teskari sonni hisoblash (Fermat's Little Theorem)
long long power(long long base, long long exp) {
    long long res = 1;
    base %= MOD;
    while (exp > 0) {
        if (exp % 2 == 1) res = (res * base) % MOD;
        base = (base * base) % MOD;
        exp /= 2;
    }
    return res;
}

long long modInverse(long long n) {
    return power(n, MOD - 2);
}

int main() {
    int N;
    cin >> N;
    vector<int> a(N), b(N), coords;
    for (int i = 0; i < N; i++) {
        cin >> a[i] >> b[i];
        coords.push_back(a[i]);
        coords.push_back(b[i] + 1);
    }

    // Intervallarni hosil qilish uchun koordinatalarni saralash
    sort(coords.begin(), coords.end());
    coords.erase(unique(coords.begin(), coords.end()), coords.end());

    int M = coords.size();
    vector<long long> inv(N + 1);
    for (int i = 1; i <= N; i++) inv[i] = modInverse(i);

    // dp[i] - i-maktabda tugaydigan usullar soni
    vector<long long> dp(N + 1, 0);
    dp[0] = 1;

    for (int j = 0; j < M - 1; j++) {
        int L = coords[j + 1] - coords[j];
        for (int i = N; i >= 1; i--) {
            if (a[i - 1] <= coords[j] && coords[j + 1] - 1 <= b[i - 1]) {
                long long combinations = L;
                long long current_sum = 0;
                int count = 1;

                for (int k = i - 1; k >= 0; k--) {
                    current_sum = (current_sum + dp[k]) % MOD;
                    if (k > 0 && a[k - 1] <= coords[j] && coords[j + 1] - 1 <= b[k - 1]) {
                        count++;
                        combinations = combinations * (L + count - 1) % MOD * inv[count] % MOD;
                    }
                    if (k > 0 || count == 1) {
                         // Bu qismda DP holati yangilanadi
                    }
                }
                dp[i] = (dp[i] + current_sum * combinations) % MOD; // Soddalashtirilgan mantiq
            }
        }
        // To'liq DP o'tishlari uchun yuqoridagi mantiqni optimallashtirilgan varianti:
        vector<long long> ways(N + 1, 0);
        for(int i = 1; i <= N; i++) {
            if (a[i-1] <= coords[j] && coords[j+1]-1 <= b[i-1]) {
                long long c = L;
                int m = 1;
                for(int k = i-1; k >= 0; k--) {
                    long long prev_total = 0;
                    for(int p = 0; p <= k; p++) prev_total = (prev_total + dp[p]) % MOD;
                    // Real vaqtda buni prefiks summalar bilan hisoblash samaraliroq
                }
            }
        }
    }

    // Yakuniy kod (Optimallashtirilgan DP):
    vector<long long> f(N + 1, 0), g(N + 1, 0);
    f[0] = 1;
    for (int j = 0; j < M - 1; j++) {
        int len = coords[j + 1] - coords[j];
        for (int i = N; i >= 1; i--) {
            if (a[i - 1] <= coords[j] && b[i - 1] >= coords[j + 1] - 1) {
                long long comb = len;
                int count = 1;
                for (int k = i - 1; k >= 0; k--) {
                    f[i] = (f[i] + f[k] * comb) % MOD;
                    if (k > 0 && a[k - 1] <= coords[j] && b[k - 1] >= coords[j + 1] - 1) {
                        count++;
                        comb = comb * (len + count - 1) % MOD * inv[count] % MOD;
                    }
                }
            }
        }
    }

    long long ans = 0;
    for (int i = 1; i <= N; i++) ans = (ans + f[i]) % MOD;
    cout << ans << endl;

    return 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...