답안 #434962

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
434962 2021-06-22T14:26:47 Z Lam_lai_cuoc_doi Boat (APIO16_boat) C++17
100 / 100
1874 ms 8344 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

constexpr bool typetest = 0;
constexpr int N = 5e2 + 2;
constexpr ll mod = 1e9 + 7;

/* https://codeforces.com/blog/entry/57324?#comment-409800 */

int n;

/// dp[i][j]: choose i first elements in j first blocks
ll dp[N][N * 2], d[N * 2][N], inv[N];

pair<int, int> s[N];
vector<int> block;

void Read()
{
    scanf("%d", &n); //cin >> n;

    block.reserve(n * 2);

    for (int i = 1; i <= n; ++i)
    {
        //cin >> s[i].first >> s[i].second;
        scanf("%d%d", &s[i].first, &s[i].second);

        block.emplace_back(s[i].first - 1);
        block.emplace_back(s[i].second);
    }

    sort(block.begin(), block.end());
    block.resize(unique(block.begin(), block.end()) - block.begin());
}

ll Pow(ll a, ll b)
{
    ll ans(1);

    for (; b > 0; b >>= 1)
    {
        if (b & 1)
            ans = ans * a % mod;
        a = a * a % mod;
    }

    return ans;
}

void Solve()
{
    inv[0] = 1;
    for (int i = 1; i <= n; ++i)
        inv[i] = inv[i - 1] * i % mod;

    ll tmp = Pow(inv[n], mod - 2);

    for (int i = n; i; --i)
    {
        inv[i] = tmp * inv[i - 1] % mod;
        (tmp *= i) %= mod;
    }

    /// Pre-calculate d[], I'll define d[] below

    for (int i = 1; i < (int)block.size(); ++i)
    {
        d[i][0] = 1;
        for (int j = 1; j <= n; ++j)
        {
            ll c1(1), c2(1);
            for (int t = 0; t < j && t + 1 <= block[i] - block[i - 1]; ++t)
            {
                (c2 *= inv[t + 1] * (block[i] - block[i - 1] - (t + 1) + 1) % mod) %= mod;

                d[i][j] += c1 * c2 % mod;

                (c1 *= inv[t + 1] * (j - 1 - (t + 1) + 1) % mod) %= mod;
            }

            d[i][j] %= mod;
        }
    }

    for (int i = 0; i < (int)block.size(); ++i)
        dp[0][i] = 1;

    for (int i = 1; i <= n; ++i)
    {
        dp[i][0] = 1;
        for (int j = 1; j < (int)block.size(); ++j)
        {
            /// no element in j block
            dp[i][j] = dp[i][j - 1];

            /// there're some elements in j block
            for (int t = i, cnt(0); t; --t)
                if (s[t].first <= block[j - 1] + 1 && s[t].second >= block[j])
                {
                    ++cnt;
                    dp[i][j] += dp[t - 1][j - 1] * d[j][cnt] % mod;
                    /// d[j][i] : number of ways to make array that have less or equal i element only from block j
                    /// to prevent overlap counting, we need to change sthg about definition
                }

            dp[i][j] %= mod;
        }
    }

    cout << dp[n][block.size() - 1] - 1;
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t(1);
    if (typetest)
        cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        Read();
        Solve();
    }
}

Compilation message

boat.cpp: In function 'void Read()':
boat.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |     scanf("%d", &n); //cin >> n;
      |     ~~~~~^~~~~~~~~~
boat.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         scanf("%d%d", &s[i].first, &s[i].second);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1002 ms 8180 KB Output is correct
2 Correct 1014 ms 8180 KB Output is correct
3 Correct 1061 ms 8068 KB Output is correct
4 Correct 1027 ms 8216 KB Output is correct
5 Correct 1027 ms 8096 KB Output is correct
6 Correct 915 ms 8140 KB Output is correct
7 Correct 897 ms 8260 KB Output is correct
8 Correct 913 ms 8108 KB Output is correct
9 Correct 909 ms 8212 KB Output is correct
10 Correct 910 ms 8156 KB Output is correct
11 Correct 898 ms 8188 KB Output is correct
12 Correct 905 ms 8140 KB Output is correct
13 Correct 901 ms 8160 KB Output is correct
14 Correct 907 ms 8252 KB Output is correct
15 Correct 933 ms 8176 KB Output is correct
16 Correct 194 ms 3776 KB Output is correct
17 Correct 196 ms 3776 KB Output is correct
18 Correct 194 ms 3712 KB Output is correct
19 Correct 198 ms 3768 KB Output is correct
20 Correct 196 ms 3644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1002 ms 8180 KB Output is correct
2 Correct 1014 ms 8180 KB Output is correct
3 Correct 1061 ms 8068 KB Output is correct
4 Correct 1027 ms 8216 KB Output is correct
5 Correct 1027 ms 8096 KB Output is correct
6 Correct 915 ms 8140 KB Output is correct
7 Correct 897 ms 8260 KB Output is correct
8 Correct 913 ms 8108 KB Output is correct
9 Correct 909 ms 8212 KB Output is correct
10 Correct 910 ms 8156 KB Output is correct
11 Correct 898 ms 8188 KB Output is correct
12 Correct 905 ms 8140 KB Output is correct
13 Correct 901 ms 8160 KB Output is correct
14 Correct 907 ms 8252 KB Output is correct
15 Correct 933 ms 8176 KB Output is correct
16 Correct 194 ms 3776 KB Output is correct
17 Correct 196 ms 3776 KB Output is correct
18 Correct 194 ms 3712 KB Output is correct
19 Correct 198 ms 3768 KB Output is correct
20 Correct 196 ms 3644 KB Output is correct
21 Correct 349 ms 7796 KB Output is correct
22 Correct 354 ms 7844 KB Output is correct
23 Correct 339 ms 7820 KB Output is correct
24 Correct 351 ms 7768 KB Output is correct
25 Correct 348 ms 7728 KB Output is correct
26 Correct 317 ms 7744 KB Output is correct
27 Correct 318 ms 7748 KB Output is correct
28 Correct 306 ms 7620 KB Output is correct
29 Correct 314 ms 7772 KB Output is correct
30 Correct 877 ms 8176 KB Output is correct
31 Correct 864 ms 8096 KB Output is correct
32 Correct 881 ms 8208 KB Output is correct
33 Correct 881 ms 8176 KB Output is correct
34 Correct 883 ms 8240 KB Output is correct
35 Correct 974 ms 8240 KB Output is correct
36 Correct 975 ms 8180 KB Output is correct
37 Correct 980 ms 8072 KB Output is correct
38 Correct 965 ms 8172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 1636 KB Output is correct
2 Correct 15 ms 1604 KB Output is correct
3 Correct 16 ms 1556 KB Output is correct
4 Correct 16 ms 1668 KB Output is correct
5 Correct 16 ms 1600 KB Output is correct
6 Correct 16 ms 1612 KB Output is correct
7 Correct 16 ms 1604 KB Output is correct
8 Correct 16 ms 1576 KB Output is correct
9 Correct 16 ms 1596 KB Output is correct
10 Correct 16 ms 1608 KB Output is correct
11 Correct 16 ms 1612 KB Output is correct
12 Correct 15 ms 1648 KB Output is correct
13 Correct 16 ms 1612 KB Output is correct
14 Correct 18 ms 1636 KB Output is correct
15 Correct 16 ms 1620 KB Output is correct
16 Correct 8 ms 1228 KB Output is correct
17 Correct 8 ms 1228 KB Output is correct
18 Correct 8 ms 1244 KB Output is correct
19 Correct 8 ms 1228 KB Output is correct
20 Correct 9 ms 1228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1002 ms 8180 KB Output is correct
2 Correct 1014 ms 8180 KB Output is correct
3 Correct 1061 ms 8068 KB Output is correct
4 Correct 1027 ms 8216 KB Output is correct
5 Correct 1027 ms 8096 KB Output is correct
6 Correct 915 ms 8140 KB Output is correct
7 Correct 897 ms 8260 KB Output is correct
8 Correct 913 ms 8108 KB Output is correct
9 Correct 909 ms 8212 KB Output is correct
10 Correct 910 ms 8156 KB Output is correct
11 Correct 898 ms 8188 KB Output is correct
12 Correct 905 ms 8140 KB Output is correct
13 Correct 901 ms 8160 KB Output is correct
14 Correct 907 ms 8252 KB Output is correct
15 Correct 933 ms 8176 KB Output is correct
16 Correct 194 ms 3776 KB Output is correct
17 Correct 196 ms 3776 KB Output is correct
18 Correct 194 ms 3712 KB Output is correct
19 Correct 198 ms 3768 KB Output is correct
20 Correct 196 ms 3644 KB Output is correct
21 Correct 349 ms 7796 KB Output is correct
22 Correct 354 ms 7844 KB Output is correct
23 Correct 339 ms 7820 KB Output is correct
24 Correct 351 ms 7768 KB Output is correct
25 Correct 348 ms 7728 KB Output is correct
26 Correct 317 ms 7744 KB Output is correct
27 Correct 318 ms 7748 KB Output is correct
28 Correct 306 ms 7620 KB Output is correct
29 Correct 314 ms 7772 KB Output is correct
30 Correct 877 ms 8176 KB Output is correct
31 Correct 864 ms 8096 KB Output is correct
32 Correct 881 ms 8208 KB Output is correct
33 Correct 881 ms 8176 KB Output is correct
34 Correct 883 ms 8240 KB Output is correct
35 Correct 974 ms 8240 KB Output is correct
36 Correct 975 ms 8180 KB Output is correct
37 Correct 980 ms 8072 KB Output is correct
38 Correct 965 ms 8172 KB Output is correct
39 Correct 16 ms 1636 KB Output is correct
40 Correct 15 ms 1604 KB Output is correct
41 Correct 16 ms 1556 KB Output is correct
42 Correct 16 ms 1668 KB Output is correct
43 Correct 16 ms 1600 KB Output is correct
44 Correct 16 ms 1612 KB Output is correct
45 Correct 16 ms 1604 KB Output is correct
46 Correct 16 ms 1576 KB Output is correct
47 Correct 16 ms 1596 KB Output is correct
48 Correct 16 ms 1608 KB Output is correct
49 Correct 16 ms 1612 KB Output is correct
50 Correct 15 ms 1648 KB Output is correct
51 Correct 16 ms 1612 KB Output is correct
52 Correct 18 ms 1636 KB Output is correct
53 Correct 16 ms 1620 KB Output is correct
54 Correct 8 ms 1228 KB Output is correct
55 Correct 8 ms 1228 KB Output is correct
56 Correct 8 ms 1244 KB Output is correct
57 Correct 8 ms 1228 KB Output is correct
58 Correct 9 ms 1228 KB Output is correct
59 Correct 1817 ms 8140 KB Output is correct
60 Correct 1845 ms 8232 KB Output is correct
61 Correct 1863 ms 8132 KB Output is correct
62 Correct 1835 ms 8308 KB Output is correct
63 Correct 1874 ms 8260 KB Output is correct
64 Correct 1793 ms 8104 KB Output is correct
65 Correct 1832 ms 8260 KB Output is correct
66 Correct 1830 ms 8344 KB Output is correct
67 Correct 1801 ms 8072 KB Output is correct
68 Correct 1823 ms 8184 KB Output is correct
69 Correct 1796 ms 8232 KB Output is correct
70 Correct 1811 ms 8236 KB Output is correct
71 Correct 1814 ms 8204 KB Output is correct
72 Correct 1808 ms 8224 KB Output is correct
73 Correct 1824 ms 8268 KB Output is correct
74 Correct 226 ms 3652 KB Output is correct
75 Correct 230 ms 3852 KB Output is correct
76 Correct 229 ms 3652 KB Output is correct
77 Correct 225 ms 3724 KB Output is correct
78 Correct 224 ms 3704 KB Output is correct