답안 #172397

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
172397 2020-01-01T13:00:04 Z rama_pang Boat (APIO16_boat) C++14
9 / 100
466 ms 4472 KB
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;

int power(int n, int x) {
    if (x == 0) {
        return 1;
    }
    int res = power(n, x / 2);
    res = (1ll * res * res) % MOD;
    if (x & 1) {
        res = (1ll * res * n) % MOD;
    }
    return res;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int N;
    cin >> N;

    vector<int> A(N + 1);
    vector<int> B(N + 1);
    vector<int> X;
    vector<int> L;
    vector<int> inv(N + 1); // inc[i] = inverse of integer i
    
    for (int i = 1; i <= N; i++) {
        cin >> A[i] >> B[i];
        X.emplace_back(A[i]);
        X.emplace_back(B[i] + 1);
        inv[i] = power(i, MOD - 2); // Fermatt's Little Theorem
    }

    sort(begin(X), end(X));
    X.resize(unique(begin(X), end(X)) - begin(X));

    for (int i = 1; i <= N; i++) {
        A[i] = upper_bound(begin(X), end(X), A[i]) - begin(X);
        B[i] = upper_bound(begin(X), end(X), B[i]) - begin(X);
    }

    L.resize(X.size());
    for (int i = 1; i < X.size(); i++) {
        L[i] = X[i] - X[i - 1];
    }

    vector<vector<int>> DP(N + 1, vector<int>(X.size() + 1, 0));
    vector<vector<int>> Choose(X.size() + 1, vector<int>(N + 1, 0));

    for (int i = 1; i < X.size(); i++) {
        Choose[i][0] = 1;
        for (int j = 1; j <= N; j++) {
            Choose[i][j] = (1ll * Choose[i][j - 1] * (L[i] - j + 1) % MOD) * inv[j] % MOD; // Choose j items from a total of L[i] items
        }
    }

    for (int i = 0; i < X.size(); i++) {
        DP[0][i] = 1;
    }

    for (int i = 1; i <= N; i++) {
        DP[i][0] = 1;
        for (int j = 1; j < X.size(); j++) {
            DP[i][j] = DP[i][j - 1];
            for (int k = i, cnt = 0, choose = 0; k > 0; k--) {
                if (A[k] <= j && j <= B[k]) {
                    cnt++;
                    choose = (1ll * choose + Choose[j][cnt]) % MOD; // Equal to: if we fix the current item, how many ways of using already proccesed items in which all of them is in interval j
                    DP[i][j] = (1ll * DP[i][j] + (1ll * choose * DP[k - 1][j - 1]) % MOD) % MOD;
                }
            }
        }
    }

    cout << (DP[N][X.size() - 1] - 1 + MOD) % MOD << "\n";
    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:46:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < X.size(); i++) {
                     ~~^~~~~~~~~~
boat.cpp:53:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < X.size(); i++) {
                     ~~^~~~~~~~~~
boat.cpp:60:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < X.size(); i++) {
                     ~~^~~~~~~~~~
boat.cpp:66:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 1; j < X.size(); j++) {
                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 384 ms 4372 KB Output is correct
2 Correct 378 ms 4388 KB Output is correct
3 Correct 396 ms 4344 KB Output is correct
4 Correct 394 ms 4376 KB Output is correct
5 Correct 383 ms 4388 KB Output is correct
6 Correct 163 ms 4472 KB Output is correct
7 Correct 158 ms 4344 KB Output is correct
8 Correct 168 ms 4344 KB Output is correct
9 Correct 171 ms 4472 KB Output is correct
10 Correct 158 ms 4372 KB Output is correct
11 Correct 164 ms 4380 KB Output is correct
12 Correct 157 ms 4372 KB Output is correct
13 Correct 163 ms 4472 KB Output is correct
14 Correct 150 ms 4376 KB Output is correct
15 Correct 167 ms 4472 KB Output is correct
16 Correct 78 ms 1144 KB Output is correct
17 Correct 83 ms 1144 KB Output is correct
18 Correct 80 ms 1272 KB Output is correct
19 Correct 80 ms 1144 KB Output is correct
20 Correct 80 ms 1144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 384 ms 4372 KB Output is correct
2 Correct 378 ms 4388 KB Output is correct
3 Correct 396 ms 4344 KB Output is correct
4 Correct 394 ms 4376 KB Output is correct
5 Correct 383 ms 4388 KB Output is correct
6 Correct 163 ms 4472 KB Output is correct
7 Correct 158 ms 4344 KB Output is correct
8 Correct 168 ms 4344 KB Output is correct
9 Correct 171 ms 4472 KB Output is correct
10 Correct 158 ms 4372 KB Output is correct
11 Correct 164 ms 4380 KB Output is correct
12 Correct 157 ms 4372 KB Output is correct
13 Correct 163 ms 4472 KB Output is correct
14 Correct 150 ms 4376 KB Output is correct
15 Correct 167 ms 4472 KB Output is correct
16 Correct 78 ms 1144 KB Output is correct
17 Correct 83 ms 1144 KB Output is correct
18 Correct 80 ms 1272 KB Output is correct
19 Correct 80 ms 1144 KB Output is correct
20 Correct 80 ms 1144 KB Output is correct
21 Incorrect 466 ms 4088 KB Output isn't correct
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 384 ms 4372 KB Output is correct
2 Correct 378 ms 4388 KB Output is correct
3 Correct 396 ms 4344 KB Output is correct
4 Correct 394 ms 4376 KB Output is correct
5 Correct 383 ms 4388 KB Output is correct
6 Correct 163 ms 4472 KB Output is correct
7 Correct 158 ms 4344 KB Output is correct
8 Correct 168 ms 4344 KB Output is correct
9 Correct 171 ms 4472 KB Output is correct
10 Correct 158 ms 4372 KB Output is correct
11 Correct 164 ms 4380 KB Output is correct
12 Correct 157 ms 4372 KB Output is correct
13 Correct 163 ms 4472 KB Output is correct
14 Correct 150 ms 4376 KB Output is correct
15 Correct 167 ms 4472 KB Output is correct
16 Correct 78 ms 1144 KB Output is correct
17 Correct 83 ms 1144 KB Output is correct
18 Correct 80 ms 1272 KB Output is correct
19 Correct 80 ms 1144 KB Output is correct
20 Correct 80 ms 1144 KB Output is correct
21 Incorrect 466 ms 4088 KB Output isn't correct
22 Halted 0 ms 0 KB -