답안 #939726

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939726 2024-03-06T17:07:34 Z sysia Boat (APIO16_boat) C++17
100 / 100
1644 ms 16932 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7;
const int mod = 1e9+7;

int mul(int a, int b){
    return (a*b)%mod;
}

void add(int &a, int b){
    a += b;
    if (a >= mod) a-=mod;
}

int power(int a, int b){
    if (!b) return 1ll;
    int k = power(a, b/2);
    k = mul(k, k);
    if (b&1) k = mul(k, a);
    return k;
}

void solve(){
    int n; cin >> n;
    vector<int>a(n+1), b(n+1);
    vector<int>s = {0, 1};
    for (int i = 1; i<=n; i++) {
        cin >> a[i] >> b[i];
        s.emplace_back(a[i]);
        s.emplace_back(b[i]);
    }
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    debug(s);
    vector<T>seg;
    for (int i = 0; i<(int)s.size()-1; i++){
        seg.emplace_back(s[i], s[i]);
        if (s[i]+1 <= s[i+1]-1) seg.emplace_back(s[i]+1, s[i+1]-1);
    }
    seg.emplace_back(s.back(), s.back());
    debug(seg);
    vector<int>inv(n+1, 1);
    for (int i = 1; i<=n; i++){
        inv[i] = power(i, mod-2);
    }
    // auto nck = [&](int N, int K){
    //     if (N < 0 || K < 0 || N < K) return 0ll;
    //     return mul(f[N], mul(inv[K], inv[N-K]));
    // };
    int m = (int)seg.size();
    //len choose k moze byc bardzo dlugie, ale na szczescie dlugosci len jest O(n), wartosci k jest O(n)
    //i mozna uzyc lepszego wzorku by to spreprocessowac
    // vector nck(m, vector<int>(n+1, 1)), I(m, vector<int>(n+1));
    // for (int j = 0; j<m; j++){
    //     int len = seg[j].second - seg[j].first+1;
    //     for (int k = 0; k<=n; k++){
    //         for (int rep = 0, val = len; rep < k; rep++, val--){
    //             nck[j][k] = mul(nck[j][k], val);
    //         }
    //         nck[j][k] = mul(nck[j][k], inv[k]);
    //         I[j][k] = power(nck[j][k], mod-2);
    //     }
    // }
    //dp[i][j][k] = na ile sposobow da sie wybrac dzialajace rozwiazanie na prefiksie i, gdzie 
    //ostatnie k elementow nalezy do przedzialu seg[j]
    vector dp(m, vector<int>(n+1, 0));
    dp[0][1] = 1; //zerowy przedzial
    for (int i = 1; i<=n; i++){
        auto new_dp = dp;
        vector<int>pref(m);
        for (int s = 0; s < m; s++){
            if (s) pref[s] += pref[s-1];
            for (int k = 0; k <= n; k++) add(pref[s], dp[s][k]);
        }
        // dp[i] = dp[i-1]; //nic nie dokladamy
        for (int j = 1; j<m; j++){
            if (seg[j].second < a[i] || seg[j].first > b[i]) continue;
            int len = seg[j].second - seg[j].first + 1;
            // dp[i][j][1] = suma po dp[i-1][<j][cokolwiek]
            add(new_dp[j][1], mul(pref[j-1], len)); //dokladamy pierwszy element tego przedzialu
            // for (int s = 0; s < j; s++){
            //     for (int k = 0; k <= n; k++){
            //         if (dp[i-1][s][k]){
            //             debug(dp[i-1][s][k], len);
            //         }
            //         add(dp[i][j][1], mul(dp[i-1][s][k], len));
            //     }
            // } 
            for (int k = 2; k <= min(i, len); k++){
                //dokladamy juz nie pierwszy element tego przedzialu
                // add(new_dp[j][k], mul(dp[j][k-1], mul(power(nck(len, k-1), mod-2), nck(len, k))));
                // add(new_dp[j][k], mul(dp[j][k-1], mul(I[j][k-1], nck[j][k])));
                add(new_dp[j][k], mul(dp[j][k-1], mul(len-k+1, inv[k])));
            }
        }
        dp.swap(new_dp);
    }
    int ans = mod-1;
    for (int i = 0; i<m; i++){
        for (int j = 0; j<=n; j++){
            add(ans, dp[i][j]);
        }
    }
    cout << ans << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 461 ms 8428 KB Output is correct
2 Correct 451 ms 8624 KB Output is correct
3 Correct 426 ms 8472 KB Output is correct
4 Correct 447 ms 8668 KB Output is correct
5 Correct 427 ms 8552 KB Output is correct
6 Correct 465 ms 8476 KB Output is correct
7 Correct 437 ms 8752 KB Output is correct
8 Correct 426 ms 8492 KB Output is correct
9 Correct 438 ms 8504 KB Output is correct
10 Correct 455 ms 8652 KB Output is correct
11 Correct 428 ms 8540 KB Output is correct
12 Correct 423 ms 8532 KB Output is correct
13 Correct 434 ms 8496 KB Output is correct
14 Correct 435 ms 8664 KB Output is correct
15 Correct 426 ms 8556 KB Output is correct
16 Correct 70 ms 1880 KB Output is correct
17 Correct 77 ms 1992 KB Output is correct
18 Correct 87 ms 1936 KB Output is correct
19 Correct 74 ms 2000 KB Output is correct
20 Correct 73 ms 1956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 461 ms 8428 KB Output is correct
2 Correct 451 ms 8624 KB Output is correct
3 Correct 426 ms 8472 KB Output is correct
4 Correct 447 ms 8668 KB Output is correct
5 Correct 427 ms 8552 KB Output is correct
6 Correct 465 ms 8476 KB Output is correct
7 Correct 437 ms 8752 KB Output is correct
8 Correct 426 ms 8492 KB Output is correct
9 Correct 438 ms 8504 KB Output is correct
10 Correct 455 ms 8652 KB Output is correct
11 Correct 428 ms 8540 KB Output is correct
12 Correct 423 ms 8532 KB Output is correct
13 Correct 434 ms 8496 KB Output is correct
14 Correct 435 ms 8664 KB Output is correct
15 Correct 426 ms 8556 KB Output is correct
16 Correct 70 ms 1880 KB Output is correct
17 Correct 77 ms 1992 KB Output is correct
18 Correct 87 ms 1936 KB Output is correct
19 Correct 74 ms 2000 KB Output is correct
20 Correct 73 ms 1956 KB Output is correct
21 Correct 932 ms 14124 KB Output is correct
22 Correct 941 ms 13932 KB Output is correct
23 Correct 841 ms 14176 KB Output is correct
24 Correct 951 ms 13888 KB Output is correct
25 Correct 895 ms 13772 KB Output is correct
26 Correct 798 ms 13208 KB Output is correct
27 Correct 835 ms 13328 KB Output is correct
28 Correct 809 ms 13240 KB Output is correct
29 Correct 813 ms 13096 KB Output is correct
30 Correct 1087 ms 15492 KB Output is correct
31 Correct 1058 ms 15424 KB Output is correct
32 Correct 992 ms 15232 KB Output is correct
33 Correct 1103 ms 15648 KB Output is correct
34 Correct 1084 ms 15540 KB Output is correct
35 Correct 455 ms 8484 KB Output is correct
36 Correct 441 ms 8600 KB Output is correct
37 Correct 424 ms 8440 KB Output is correct
38 Correct 436 ms 8692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 1120 KB Output is correct
2 Correct 9 ms 1124 KB Output is correct
3 Correct 9 ms 1128 KB Output is correct
4 Correct 9 ms 1128 KB Output is correct
5 Correct 9 ms 1128 KB Output is correct
6 Correct 11 ms 1132 KB Output is correct
7 Correct 10 ms 1124 KB Output is correct
8 Correct 14 ms 1380 KB Output is correct
9 Correct 10 ms 1124 KB Output is correct
10 Correct 10 ms 1120 KB Output is correct
11 Correct 9 ms 1124 KB Output is correct
12 Correct 9 ms 1116 KB Output is correct
13 Correct 9 ms 1132 KB Output is correct
14 Correct 9 ms 1308 KB Output is correct
15 Correct 9 ms 1120 KB Output is correct
16 Correct 3 ms 740 KB Output is correct
17 Correct 3 ms 740 KB Output is correct
18 Correct 4 ms 744 KB Output is correct
19 Correct 3 ms 744 KB Output is correct
20 Correct 4 ms 756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 461 ms 8428 KB Output is correct
2 Correct 451 ms 8624 KB Output is correct
3 Correct 426 ms 8472 KB Output is correct
4 Correct 447 ms 8668 KB Output is correct
5 Correct 427 ms 8552 KB Output is correct
6 Correct 465 ms 8476 KB Output is correct
7 Correct 437 ms 8752 KB Output is correct
8 Correct 426 ms 8492 KB Output is correct
9 Correct 438 ms 8504 KB Output is correct
10 Correct 455 ms 8652 KB Output is correct
11 Correct 428 ms 8540 KB Output is correct
12 Correct 423 ms 8532 KB Output is correct
13 Correct 434 ms 8496 KB Output is correct
14 Correct 435 ms 8664 KB Output is correct
15 Correct 426 ms 8556 KB Output is correct
16 Correct 70 ms 1880 KB Output is correct
17 Correct 77 ms 1992 KB Output is correct
18 Correct 87 ms 1936 KB Output is correct
19 Correct 74 ms 2000 KB Output is correct
20 Correct 73 ms 1956 KB Output is correct
21 Correct 932 ms 14124 KB Output is correct
22 Correct 941 ms 13932 KB Output is correct
23 Correct 841 ms 14176 KB Output is correct
24 Correct 951 ms 13888 KB Output is correct
25 Correct 895 ms 13772 KB Output is correct
26 Correct 798 ms 13208 KB Output is correct
27 Correct 835 ms 13328 KB Output is correct
28 Correct 809 ms 13240 KB Output is correct
29 Correct 813 ms 13096 KB Output is correct
30 Correct 1087 ms 15492 KB Output is correct
31 Correct 1058 ms 15424 KB Output is correct
32 Correct 992 ms 15232 KB Output is correct
33 Correct 1103 ms 15648 KB Output is correct
34 Correct 1084 ms 15540 KB Output is correct
35 Correct 455 ms 8484 KB Output is correct
36 Correct 441 ms 8600 KB Output is correct
37 Correct 424 ms 8440 KB Output is correct
38 Correct 436 ms 8692 KB Output is correct
39 Correct 9 ms 1120 KB Output is correct
40 Correct 9 ms 1124 KB Output is correct
41 Correct 9 ms 1128 KB Output is correct
42 Correct 9 ms 1128 KB Output is correct
43 Correct 9 ms 1128 KB Output is correct
44 Correct 11 ms 1132 KB Output is correct
45 Correct 10 ms 1124 KB Output is correct
46 Correct 14 ms 1380 KB Output is correct
47 Correct 10 ms 1124 KB Output is correct
48 Correct 10 ms 1120 KB Output is correct
49 Correct 9 ms 1124 KB Output is correct
50 Correct 9 ms 1116 KB Output is correct
51 Correct 9 ms 1132 KB Output is correct
52 Correct 9 ms 1308 KB Output is correct
53 Correct 9 ms 1120 KB Output is correct
54 Correct 3 ms 740 KB Output is correct
55 Correct 3 ms 740 KB Output is correct
56 Correct 4 ms 744 KB Output is correct
57 Correct 3 ms 744 KB Output is correct
58 Correct 4 ms 756 KB Output is correct
59 Correct 1545 ms 16700 KB Output is correct
60 Correct 1524 ms 16864 KB Output is correct
61 Correct 1473 ms 16672 KB Output is correct
62 Correct 1566 ms 16792 KB Output is correct
63 Correct 1443 ms 16696 KB Output is correct
64 Correct 1620 ms 16932 KB Output is correct
65 Correct 1560 ms 16912 KB Output is correct
66 Correct 1594 ms 16748 KB Output is correct
67 Correct 1534 ms 16396 KB Output is correct
68 Correct 1644 ms 16676 KB Output is correct
69 Correct 1486 ms 16596 KB Output is correct
70 Correct 1462 ms 16472 KB Output is correct
71 Correct 1434 ms 16580 KB Output is correct
72 Correct 1475 ms 16408 KB Output is correct
73 Correct 1533 ms 16404 KB Output is correct
74 Correct 129 ms 2456 KB Output is correct
75 Correct 107 ms 2080 KB Output is correct
76 Correct 107 ms 2076 KB Output is correct
77 Correct 109 ms 2588 KB Output is correct
78 Correct 111 ms 2368 KB Output is correct