Submission #47297

# Submission time Handle Problem Language Result Execution time Memory
47297 2018-04-30T10:23:40 Z ngkan146 Boat (APIO16_boat) C++11
100 / 100
1280 ms 6992 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = (ll) 1e9+7;
int n, a[505], b[505];
ll f[505][3005];
ll g[505];
ll fact[505], inv[505];
ll Ckn(int x,int y){
    return fact[y] * inv[x] % mod * inv[y-x] % mod;
}
ll mod_inverse(ll a){
    ll r1 = a, r2 = mod, x1 = 1,x2 = 0, q, tmp;
    while(r2 != 0){
        q = r1 / r2;
        tmp = x1 -  q * x2;
        x1 = x2;
        x2 = tmp;
        tmp = r1 % r2;
        r1 = r2;
        r2 = tmp;
    }
    return (x1+mod)%mod;
}
int main(){
    fact[0] = 1;
    for(int i=1;i<=500;i++)
        fact[i] = fact[i-1] * i % mod;
    for(int i=0;i<=500;i++)
        inv[i] = mod_inverse(fact[i]);

    iostream::sync_with_stdio(0);
    cin >> n;
    vector <int> lst;
    for(int i=1;i<=n;i++)
        cin >> a[i] >> b[i],
        lst.push_back(a[i]),
        lst.push_back(b[i]);
    sort(lst.begin(), lst.end());
    lst.resize(unique(lst.begin(),lst.end()) - lst.begin());
    vector <pair<int,int> > rangeTmp;
    for(int i=0;i<lst.size();i++){
        rangeTmp.push_back({lst[i], lst[i]});
        if (i != lst.size() - 1 && lst[i]+1 <= lst[i+1]-1)
            rangeTmp.push_back({lst[i]+1, lst[i+1]-1});
    }

    rangeTmp.insert(rangeTmp.begin(), {-1,-1});
    pair<int,int> range[3004];
    for(int i=0;i<rangeTmp.size();i++)
        range[i] = rangeTmp[i];
    g[0] = 1;

    for(int j = 1;j < rangeTmp.size();j ++){
        // f[i][j] = cnt of way that end at i and value of i in range j
        // g[i] = sum(f[i][0] -> f[i][j-1])

        int len = range[j].second - range[j].first + 1;
        ll arr[505], sumG[505];
        arr[0] = 1;
        sumG[0] = g[0];
        for(int i=1;i<=n;i++)
            sumG[i] = (sumG[i-1] + g[i]) % mod,
            arr[i] = arr[i-1] * (len+1-i) % mod;

        ll numberOfWayToChoose[505];
        numberOfWayToChoose[0] = 1;
        numberOfWayToChoose[1] = arr[1];

        int mjk=0;
        for(int i=1;i<=n;i++) mjk += (a[i] <= range[j].first && range[j].second <= b[i]);

        for(int possiBoat=2;possiBoat<=mjk;possiBoat++){
            numberOfWayToChoose[possiBoat] = 0;
            for(int k=2;k<=min(possiBoat, len);k++)
                numberOfWayToChoose[possiBoat] = (numberOfWayToChoose[possiBoat] + Ckn(k-2, possiBoat-2) * arr[k] % mod * inv[k]) % mod;
        }

        for(int i=1;i<=n;i++){
            if (!(a[i] <= range[j].first && range[j].second <= b[i]))    continue;
            int possiBoat = 0;
            for(int ii=i;ii>=1;ii--){
                if (!(a[ii] <= range[j].first && range[j].second <= b[ii]))  continue;
                possiBoat++;
                f[i][j] = (f[i][j] + sumG[ii-1] * numberOfWayToChoose[possiBoat]) % mod;
            }
        }
        for(int i=0;i<=n;i++){
            g[i] = (g[i] + f[i][j]) % mod;
        }
    }

    ll ans = 0;
    for(int i=1;i<=n;i++)
        ans = (ans+ g[i]) % mod;
    cout << ans;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:42:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<lst.size();i++){
                 ~^~~~~~~~~~~
boat.cpp:44:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (i != lst.size() - 1 && lst[i]+1 <= lst[i+1]-1)
             ~~^~~~~~~~~~~~~~~~~
boat.cpp:50:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<rangeTmp.size();i++)
                 ~^~~~~~~~~~~~~~~~
boat.cpp:54:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j = 1;j < rangeTmp.size();j ++){
                   ~~^~~~~~~~~~~~~~~~~
boat.cpp:68:39: warning: 'arr[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
         numberOfWayToChoose[1] = arr[1];
                                  ~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 16 ms 2424 KB Output is correct
2 Correct 13 ms 2496 KB Output is correct
3 Correct 13 ms 2596 KB Output is correct
4 Correct 13 ms 2596 KB Output is correct
5 Correct 13 ms 2716 KB Output is correct
6 Correct 11 ms 2716 KB Output is correct
7 Correct 11 ms 2716 KB Output is correct
8 Correct 11 ms 2716 KB Output is correct
9 Correct 12 ms 2716 KB Output is correct
10 Correct 11 ms 2716 KB Output is correct
11 Correct 12 ms 2716 KB Output is correct
12 Correct 11 ms 2716 KB Output is correct
13 Correct 14 ms 2716 KB Output is correct
14 Correct 11 ms 2716 KB Output is correct
15 Correct 12 ms 2716 KB Output is correct
16 Correct 6 ms 2716 KB Output is correct
17 Correct 6 ms 2716 KB Output is correct
18 Correct 6 ms 2716 KB Output is correct
19 Correct 6 ms 2716 KB Output is correct
20 Correct 6 ms 2716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 2424 KB Output is correct
2 Correct 13 ms 2496 KB Output is correct
3 Correct 13 ms 2596 KB Output is correct
4 Correct 13 ms 2596 KB Output is correct
5 Correct 13 ms 2716 KB Output is correct
6 Correct 11 ms 2716 KB Output is correct
7 Correct 11 ms 2716 KB Output is correct
8 Correct 11 ms 2716 KB Output is correct
9 Correct 12 ms 2716 KB Output is correct
10 Correct 11 ms 2716 KB Output is correct
11 Correct 12 ms 2716 KB Output is correct
12 Correct 11 ms 2716 KB Output is correct
13 Correct 14 ms 2716 KB Output is correct
14 Correct 11 ms 2716 KB Output is correct
15 Correct 12 ms 2716 KB Output is correct
16 Correct 6 ms 2716 KB Output is correct
17 Correct 6 ms 2716 KB Output is correct
18 Correct 6 ms 2716 KB Output is correct
19 Correct 6 ms 2716 KB Output is correct
20 Correct 6 ms 2716 KB Output is correct
21 Correct 268 ms 4892 KB Output is correct
22 Correct 256 ms 4936 KB Output is correct
23 Correct 246 ms 4936 KB Output is correct
24 Correct 277 ms 4936 KB Output is correct
25 Correct 256 ms 4936 KB Output is correct
26 Correct 555 ms 5884 KB Output is correct
27 Correct 567 ms 5884 KB Output is correct
28 Correct 652 ms 5932 KB Output is correct
29 Correct 627 ms 5932 KB Output is correct
30 Correct 19 ms 5932 KB Output is correct
31 Correct 18 ms 5932 KB Output is correct
32 Correct 17 ms 5932 KB Output is correct
33 Correct 18 ms 5932 KB Output is correct
34 Correct 26 ms 5932 KB Output is correct
35 Correct 15 ms 5932 KB Output is correct
36 Correct 14 ms 5932 KB Output is correct
37 Correct 14 ms 5932 KB Output is correct
38 Correct 14 ms 5932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5932 KB Output is correct
2 Correct 6 ms 5932 KB Output is correct
3 Correct 6 ms 5932 KB Output is correct
4 Correct 6 ms 5932 KB Output is correct
5 Correct 7 ms 5932 KB Output is correct
6 Correct 12 ms 5932 KB Output is correct
7 Correct 12 ms 5932 KB Output is correct
8 Correct 12 ms 5932 KB Output is correct
9 Correct 12 ms 5932 KB Output is correct
10 Correct 15 ms 5932 KB Output is correct
11 Correct 7 ms 5932 KB Output is correct
12 Correct 6 ms 5932 KB Output is correct
13 Correct 7 ms 5932 KB Output is correct
14 Correct 7 ms 5932 KB Output is correct
15 Correct 7 ms 5932 KB Output is correct
16 Correct 4 ms 5932 KB Output is correct
17 Correct 4 ms 5932 KB Output is correct
18 Correct 4 ms 5932 KB Output is correct
19 Correct 4 ms 5932 KB Output is correct
20 Correct 5 ms 5932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 2424 KB Output is correct
2 Correct 13 ms 2496 KB Output is correct
3 Correct 13 ms 2596 KB Output is correct
4 Correct 13 ms 2596 KB Output is correct
5 Correct 13 ms 2716 KB Output is correct
6 Correct 11 ms 2716 KB Output is correct
7 Correct 11 ms 2716 KB Output is correct
8 Correct 11 ms 2716 KB Output is correct
9 Correct 12 ms 2716 KB Output is correct
10 Correct 11 ms 2716 KB Output is correct
11 Correct 12 ms 2716 KB Output is correct
12 Correct 11 ms 2716 KB Output is correct
13 Correct 14 ms 2716 KB Output is correct
14 Correct 11 ms 2716 KB Output is correct
15 Correct 12 ms 2716 KB Output is correct
16 Correct 6 ms 2716 KB Output is correct
17 Correct 6 ms 2716 KB Output is correct
18 Correct 6 ms 2716 KB Output is correct
19 Correct 6 ms 2716 KB Output is correct
20 Correct 6 ms 2716 KB Output is correct
21 Correct 268 ms 4892 KB Output is correct
22 Correct 256 ms 4936 KB Output is correct
23 Correct 246 ms 4936 KB Output is correct
24 Correct 277 ms 4936 KB Output is correct
25 Correct 256 ms 4936 KB Output is correct
26 Correct 555 ms 5884 KB Output is correct
27 Correct 567 ms 5884 KB Output is correct
28 Correct 652 ms 5932 KB Output is correct
29 Correct 627 ms 5932 KB Output is correct
30 Correct 19 ms 5932 KB Output is correct
31 Correct 18 ms 5932 KB Output is correct
32 Correct 17 ms 5932 KB Output is correct
33 Correct 18 ms 5932 KB Output is correct
34 Correct 26 ms 5932 KB Output is correct
35 Correct 15 ms 5932 KB Output is correct
36 Correct 14 ms 5932 KB Output is correct
37 Correct 14 ms 5932 KB Output is correct
38 Correct 14 ms 5932 KB Output is correct
39 Correct 7 ms 5932 KB Output is correct
40 Correct 6 ms 5932 KB Output is correct
41 Correct 6 ms 5932 KB Output is correct
42 Correct 6 ms 5932 KB Output is correct
43 Correct 7 ms 5932 KB Output is correct
44 Correct 12 ms 5932 KB Output is correct
45 Correct 12 ms 5932 KB Output is correct
46 Correct 12 ms 5932 KB Output is correct
47 Correct 12 ms 5932 KB Output is correct
48 Correct 15 ms 5932 KB Output is correct
49 Correct 7 ms 5932 KB Output is correct
50 Correct 6 ms 5932 KB Output is correct
51 Correct 7 ms 5932 KB Output is correct
52 Correct 7 ms 5932 KB Output is correct
53 Correct 7 ms 5932 KB Output is correct
54 Correct 4 ms 5932 KB Output is correct
55 Correct 4 ms 5932 KB Output is correct
56 Correct 4 ms 5932 KB Output is correct
57 Correct 4 ms 5932 KB Output is correct
58 Correct 5 ms 5932 KB Output is correct
59 Correct 491 ms 5932 KB Output is correct
60 Correct 445 ms 5932 KB Output is correct
61 Correct 425 ms 5932 KB Output is correct
62 Correct 520 ms 5932 KB Output is correct
63 Correct 479 ms 5932 KB Output is correct
64 Correct 1164 ms 6704 KB Output is correct
65 Correct 1161 ms 6704 KB Output is correct
66 Correct 1280 ms 6780 KB Output is correct
67 Correct 1153 ms 6824 KB Output is correct
68 Correct 1166 ms 6992 KB Output is correct
69 Correct 433 ms 6992 KB Output is correct
70 Correct 440 ms 6992 KB Output is correct
71 Correct 445 ms 6992 KB Output is correct
72 Correct 476 ms 6992 KB Output is correct
73 Correct 513 ms 6992 KB Output is correct
74 Correct 98 ms 6992 KB Output is correct
75 Correct 51 ms 6992 KB Output is correct
76 Correct 56 ms 6992 KB Output is correct
77 Correct 65 ms 6992 KB Output is correct
78 Correct 57 ms 6992 KB Output is correct