Submission #47296

# Submission time Handle Problem Language Result Execution time Memory
47296 2018-04-30T10:19:33 Z ngkan146 Boat (APIO16_boat) C++11
58 / 100
2000 ms 6936 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];

        for(int possiBoat=2;possiBoat<=n;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 700 ms 2552 KB Output is correct
2 Correct 679 ms 2676 KB Output is correct
3 Correct 683 ms 2676 KB Output is correct
4 Correct 680 ms 2676 KB Output is correct
5 Correct 686 ms 2740 KB Output is correct
6 Correct 699 ms 2740 KB Output is correct
7 Correct 688 ms 2740 KB Output is correct
8 Correct 702 ms 2760 KB Output is correct
9 Correct 693 ms 2808 KB Output is correct
10 Correct 685 ms 2876 KB Output is correct
11 Correct 699 ms 2876 KB Output is correct
12 Correct 737 ms 2876 KB Output is correct
13 Correct 672 ms 2876 KB Output is correct
14 Correct 692 ms 2876 KB Output is correct
15 Correct 666 ms 2876 KB Output is correct
16 Correct 151 ms 2876 KB Output is correct
17 Correct 136 ms 2876 KB Output is correct
18 Correct 131 ms 2876 KB Output is correct
19 Correct 131 ms 2876 KB Output is correct
20 Correct 133 ms 2876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 700 ms 2552 KB Output is correct
2 Correct 679 ms 2676 KB Output is correct
3 Correct 683 ms 2676 KB Output is correct
4 Correct 680 ms 2676 KB Output is correct
5 Correct 686 ms 2740 KB Output is correct
6 Correct 699 ms 2740 KB Output is correct
7 Correct 688 ms 2740 KB Output is correct
8 Correct 702 ms 2760 KB Output is correct
9 Correct 693 ms 2808 KB Output is correct
10 Correct 685 ms 2876 KB Output is correct
11 Correct 699 ms 2876 KB Output is correct
12 Correct 737 ms 2876 KB Output is correct
13 Correct 672 ms 2876 KB Output is correct
14 Correct 692 ms 2876 KB Output is correct
15 Correct 666 ms 2876 KB Output is correct
16 Correct 151 ms 2876 KB Output is correct
17 Correct 136 ms 2876 KB Output is correct
18 Correct 131 ms 2876 KB Output is correct
19 Correct 131 ms 2876 KB Output is correct
20 Correct 133 ms 2876 KB Output is correct
21 Correct 279 ms 4984 KB Output is correct
22 Correct 264 ms 4984 KB Output is correct
23 Correct 241 ms 4984 KB Output is correct
24 Correct 283 ms 5144 KB Output is correct
25 Correct 283 ms 5144 KB Output is correct
26 Correct 569 ms 5752 KB Output is correct
27 Correct 570 ms 5772 KB Output is correct
28 Correct 573 ms 5880 KB Output is correct
29 Correct 573 ms 5880 KB Output is correct
30 Correct 659 ms 5880 KB Output is correct
31 Correct 680 ms 5880 KB Output is correct
32 Correct 678 ms 5880 KB Output is correct
33 Correct 664 ms 5880 KB Output is correct
34 Correct 667 ms 5880 KB Output is correct
35 Correct 704 ms 5880 KB Output is correct
36 Correct 635 ms 5880 KB Output is correct
37 Correct 656 ms 5880 KB Output is correct
38 Correct 683 ms 5880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 5880 KB Output is correct
2 Correct 15 ms 5880 KB Output is correct
3 Correct 15 ms 5880 KB Output is correct
4 Correct 16 ms 5880 KB Output is correct
5 Correct 16 ms 5880 KB Output is correct
6 Correct 19 ms 5880 KB Output is correct
7 Correct 24 ms 5880 KB Output is correct
8 Correct 21 ms 5880 KB Output is correct
9 Correct 19 ms 5880 KB Output is correct
10 Correct 20 ms 5880 KB Output is correct
11 Correct 20 ms 5880 KB Output is correct
12 Correct 18 ms 5880 KB Output is correct
13 Correct 15 ms 5880 KB Output is correct
14 Correct 20 ms 5880 KB Output is correct
15 Correct 16 ms 5880 KB Output is correct
16 Correct 8 ms 5880 KB Output is correct
17 Correct 8 ms 5880 KB Output is correct
18 Correct 8 ms 5880 KB Output is correct
19 Correct 8 ms 5880 KB Output is correct
20 Correct 8 ms 5880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 700 ms 2552 KB Output is correct
2 Correct 679 ms 2676 KB Output is correct
3 Correct 683 ms 2676 KB Output is correct
4 Correct 680 ms 2676 KB Output is correct
5 Correct 686 ms 2740 KB Output is correct
6 Correct 699 ms 2740 KB Output is correct
7 Correct 688 ms 2740 KB Output is correct
8 Correct 702 ms 2760 KB Output is correct
9 Correct 693 ms 2808 KB Output is correct
10 Correct 685 ms 2876 KB Output is correct
11 Correct 699 ms 2876 KB Output is correct
12 Correct 737 ms 2876 KB Output is correct
13 Correct 672 ms 2876 KB Output is correct
14 Correct 692 ms 2876 KB Output is correct
15 Correct 666 ms 2876 KB Output is correct
16 Correct 151 ms 2876 KB Output is correct
17 Correct 136 ms 2876 KB Output is correct
18 Correct 131 ms 2876 KB Output is correct
19 Correct 131 ms 2876 KB Output is correct
20 Correct 133 ms 2876 KB Output is correct
21 Correct 279 ms 4984 KB Output is correct
22 Correct 264 ms 4984 KB Output is correct
23 Correct 241 ms 4984 KB Output is correct
24 Correct 283 ms 5144 KB Output is correct
25 Correct 283 ms 5144 KB Output is correct
26 Correct 569 ms 5752 KB Output is correct
27 Correct 570 ms 5772 KB Output is correct
28 Correct 573 ms 5880 KB Output is correct
29 Correct 573 ms 5880 KB Output is correct
30 Correct 659 ms 5880 KB Output is correct
31 Correct 680 ms 5880 KB Output is correct
32 Correct 678 ms 5880 KB Output is correct
33 Correct 664 ms 5880 KB Output is correct
34 Correct 667 ms 5880 KB Output is correct
35 Correct 704 ms 5880 KB Output is correct
36 Correct 635 ms 5880 KB Output is correct
37 Correct 656 ms 5880 KB Output is correct
38 Correct 683 ms 5880 KB Output is correct
39 Correct 18 ms 5880 KB Output is correct
40 Correct 15 ms 5880 KB Output is correct
41 Correct 15 ms 5880 KB Output is correct
42 Correct 16 ms 5880 KB Output is correct
43 Correct 16 ms 5880 KB Output is correct
44 Correct 19 ms 5880 KB Output is correct
45 Correct 24 ms 5880 KB Output is correct
46 Correct 21 ms 5880 KB Output is correct
47 Correct 19 ms 5880 KB Output is correct
48 Correct 20 ms 5880 KB Output is correct
49 Correct 20 ms 5880 KB Output is correct
50 Correct 18 ms 5880 KB Output is correct
51 Correct 15 ms 5880 KB Output is correct
52 Correct 20 ms 5880 KB Output is correct
53 Correct 16 ms 5880 KB Output is correct
54 Correct 8 ms 5880 KB Output is correct
55 Correct 8 ms 5880 KB Output is correct
56 Correct 8 ms 5880 KB Output is correct
57 Correct 8 ms 5880 KB Output is correct
58 Correct 8 ms 5880 KB Output is correct
59 Correct 1699 ms 5880 KB Output is correct
60 Correct 1650 ms 5880 KB Output is correct
61 Correct 1653 ms 5880 KB Output is correct
62 Correct 1680 ms 5880 KB Output is correct
63 Correct 1592 ms 5880 KB Output is correct
64 Execution timed out 2044 ms 6936 KB Time limit exceeded
65 Halted 0 ms 0 KB -