답안 #47295

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
47295 2018-04-30T10:14:24 Z ngkan146 Boat (APIO16_boat) C++11
58 / 100
2000 ms 6872 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> > range;
    for(int i=0;i<lst.size();i++){
        range.push_back({lst[i], lst[i]});
        if (i != lst.size() - 1 && lst[i]+1 <= lst[i+1]-1)
            range.push_back({lst[i]+1, lst[i+1]-1});
    }

    range.insert(range.begin(), {-1,-1});
    g[0] = 1;

    ll ans = 0;
    for(int j = 1;j < range.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] += Ckn(k-2, possiBoat-2) % mod * arr[k] % mod * inv[k] % mod;
            numberOfWayToChoose[possiBoat] %= mod;
        }

        for(int i=1;i<=n;i++){
            if (!(a[i] <= range[j].first && range[j].second <= b[i]))    continue;

            int possiBoat = 0;
            ll tmp = 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;
            }
            ans = (ans + f[i][j]) % mod;
        }
        for(int i=0;i<=n;i++){
            g[i] = (g[i] + f[i][j]) % 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:52:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j = 1;j < range.size();j ++){
                   ~~^~~~~~~~~~~~~~
boat.cpp:78:16: warning: unused variable 'tmp' [-Wunused-variable]
             ll tmp = 0;
                ^~~
boat.cpp:65:39: warning: 'arr[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
         numberOfWayToChoose[1] = arr[1];
                                  ~~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 775 ms 2700 KB Output is correct
2 Correct 684 ms 2700 KB Output is correct
3 Correct 679 ms 2700 KB Output is correct
4 Correct 705 ms 2700 KB Output is correct
5 Correct 685 ms 2700 KB Output is correct
6 Correct 682 ms 2808 KB Output is correct
7 Correct 669 ms 2808 KB Output is correct
8 Correct 685 ms 2808 KB Output is correct
9 Correct 676 ms 2808 KB Output is correct
10 Correct 670 ms 2808 KB Output is correct
11 Correct 686 ms 2888 KB Output is correct
12 Correct 676 ms 2888 KB Output is correct
13 Correct 675 ms 2888 KB Output is correct
14 Correct 689 ms 2888 KB Output is correct
15 Correct 680 ms 2888 KB Output is correct
16 Correct 121 ms 2888 KB Output is correct
17 Correct 135 ms 2888 KB Output is correct
18 Correct 126 ms 2888 KB Output is correct
19 Correct 138 ms 2888 KB Output is correct
20 Correct 134 ms 2888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 775 ms 2700 KB Output is correct
2 Correct 684 ms 2700 KB Output is correct
3 Correct 679 ms 2700 KB Output is correct
4 Correct 705 ms 2700 KB Output is correct
5 Correct 685 ms 2700 KB Output is correct
6 Correct 682 ms 2808 KB Output is correct
7 Correct 669 ms 2808 KB Output is correct
8 Correct 685 ms 2808 KB Output is correct
9 Correct 676 ms 2808 KB Output is correct
10 Correct 670 ms 2808 KB Output is correct
11 Correct 686 ms 2888 KB Output is correct
12 Correct 676 ms 2888 KB Output is correct
13 Correct 675 ms 2888 KB Output is correct
14 Correct 689 ms 2888 KB Output is correct
15 Correct 680 ms 2888 KB Output is correct
16 Correct 121 ms 2888 KB Output is correct
17 Correct 135 ms 2888 KB Output is correct
18 Correct 126 ms 2888 KB Output is correct
19 Correct 138 ms 2888 KB Output is correct
20 Correct 134 ms 2888 KB Output is correct
21 Correct 264 ms 4880 KB Output is correct
22 Correct 255 ms 4880 KB Output is correct
23 Correct 228 ms 4880 KB Output is correct
24 Correct 255 ms 4924 KB Output is correct
25 Correct 262 ms 4988 KB Output is correct
26 Correct 424 ms 5748 KB Output is correct
27 Correct 433 ms 5948 KB Output is correct
28 Correct 443 ms 5948 KB Output is correct
29 Correct 446 ms 5948 KB Output is correct
30 Correct 648 ms 5948 KB Output is correct
31 Correct 653 ms 5948 KB Output is correct
32 Correct 685 ms 5948 KB Output is correct
33 Correct 674 ms 5948 KB Output is correct
34 Correct 682 ms 5948 KB Output is correct
35 Correct 657 ms 5948 KB Output is correct
36 Correct 636 ms 5948 KB Output is correct
37 Correct 689 ms 5948 KB Output is correct
38 Correct 649 ms 5948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 5948 KB Output is correct
2 Correct 18 ms 5948 KB Output is correct
3 Correct 15 ms 5948 KB Output is correct
4 Correct 16 ms 5948 KB Output is correct
5 Correct 16 ms 5948 KB Output is correct
6 Correct 18 ms 5948 KB Output is correct
7 Correct 18 ms 5948 KB Output is correct
8 Correct 18 ms 5948 KB Output is correct
9 Correct 18 ms 5948 KB Output is correct
10 Correct 18 ms 5948 KB Output is correct
11 Correct 16 ms 5948 KB Output is correct
12 Correct 15 ms 5948 KB Output is correct
13 Correct 15 ms 5948 KB Output is correct
14 Correct 15 ms 5948 KB Output is correct
15 Correct 19 ms 5948 KB Output is correct
16 Correct 8 ms 5948 KB Output is correct
17 Correct 8 ms 5948 KB Output is correct
18 Correct 8 ms 5948 KB Output is correct
19 Correct 8 ms 5948 KB Output is correct
20 Correct 9 ms 5948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 775 ms 2700 KB Output is correct
2 Correct 684 ms 2700 KB Output is correct
3 Correct 679 ms 2700 KB Output is correct
4 Correct 705 ms 2700 KB Output is correct
5 Correct 685 ms 2700 KB Output is correct
6 Correct 682 ms 2808 KB Output is correct
7 Correct 669 ms 2808 KB Output is correct
8 Correct 685 ms 2808 KB Output is correct
9 Correct 676 ms 2808 KB Output is correct
10 Correct 670 ms 2808 KB Output is correct
11 Correct 686 ms 2888 KB Output is correct
12 Correct 676 ms 2888 KB Output is correct
13 Correct 675 ms 2888 KB Output is correct
14 Correct 689 ms 2888 KB Output is correct
15 Correct 680 ms 2888 KB Output is correct
16 Correct 121 ms 2888 KB Output is correct
17 Correct 135 ms 2888 KB Output is correct
18 Correct 126 ms 2888 KB Output is correct
19 Correct 138 ms 2888 KB Output is correct
20 Correct 134 ms 2888 KB Output is correct
21 Correct 264 ms 4880 KB Output is correct
22 Correct 255 ms 4880 KB Output is correct
23 Correct 228 ms 4880 KB Output is correct
24 Correct 255 ms 4924 KB Output is correct
25 Correct 262 ms 4988 KB Output is correct
26 Correct 424 ms 5748 KB Output is correct
27 Correct 433 ms 5948 KB Output is correct
28 Correct 443 ms 5948 KB Output is correct
29 Correct 446 ms 5948 KB Output is correct
30 Correct 648 ms 5948 KB Output is correct
31 Correct 653 ms 5948 KB Output is correct
32 Correct 685 ms 5948 KB Output is correct
33 Correct 674 ms 5948 KB Output is correct
34 Correct 682 ms 5948 KB Output is correct
35 Correct 657 ms 5948 KB Output is correct
36 Correct 636 ms 5948 KB Output is correct
37 Correct 689 ms 5948 KB Output is correct
38 Correct 649 ms 5948 KB Output is correct
39 Correct 16 ms 5948 KB Output is correct
40 Correct 18 ms 5948 KB Output is correct
41 Correct 15 ms 5948 KB Output is correct
42 Correct 16 ms 5948 KB Output is correct
43 Correct 16 ms 5948 KB Output is correct
44 Correct 18 ms 5948 KB Output is correct
45 Correct 18 ms 5948 KB Output is correct
46 Correct 18 ms 5948 KB Output is correct
47 Correct 18 ms 5948 KB Output is correct
48 Correct 18 ms 5948 KB Output is correct
49 Correct 16 ms 5948 KB Output is correct
50 Correct 15 ms 5948 KB Output is correct
51 Correct 15 ms 5948 KB Output is correct
52 Correct 15 ms 5948 KB Output is correct
53 Correct 19 ms 5948 KB Output is correct
54 Correct 8 ms 5948 KB Output is correct
55 Correct 8 ms 5948 KB Output is correct
56 Correct 8 ms 5948 KB Output is correct
57 Correct 8 ms 5948 KB Output is correct
58 Correct 9 ms 5948 KB Output is correct
59 Correct 1570 ms 5948 KB Output is correct
60 Correct 1558 ms 5948 KB Output is correct
61 Correct 1524 ms 5948 KB Output is correct
62 Correct 1662 ms 5948 KB Output is correct
63 Correct 1628 ms 5948 KB Output is correct
64 Correct 1867 ms 6856 KB Output is correct
65 Execution timed out 2072 ms 6872 KB Time limit exceeded
66 Halted 0 ms 0 KB -