답안 #958114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
958114 2024-04-05T00:18:12 Z thomas_li Boat (APIO16_boat) C++17
100 / 100
1253 ms 37492 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define ary(k) array<int,k>
template<class A,class B> void cmx(A& x, B y) {x=max<A>(x,y);}
template<class A,class B> void cmn(A& x, B y) {x=min<A>(x,y);}
typedef pair<int, int> pii;
typedef vector<int> vi;
const int MM = 1e3+20,MOD = 1e9+7;

typedef int T;
int n; T dp[MM][MM]; vi pts; vector<pii> grps;
pii school[MM]; int lft[MM],rit[MM];
T big_c[MM][MM];

T pre[MM][2][MM],C[MM][MM];
int fpow(int x, int y){
	int res = 1;
	while(y){
		if(y & 1) res = 1ll*res*x%MOD;
		x = 1ll*x*x%MOD;
		y /= 2;
	}
	return res;
}
signed main() {
	cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
    C[0][0] = 1;
    rep(i,1,MM){
        rep(j,0,i+1){
            C[i][j] = C[i-1][j];
            if(j) C[i][j] = (C[i][j]+C[i-1][j-1])%MOD;
        }
    }
    cin >> n;
    rep(i,1,n+1){
        int a,b; cin >> a >> b;
        school[i] = {a,b};
        pts.PB(a-1);
        pts.PB(b);
    }
    pts.PB(-1);
    sort(all(pts));     
    rep(i,0,sz(pts)-1){
        if(pts[i] < pts[i+1]) grps.PB({pts[i]+1,pts[i+1]});
    }
    int m = sz(grps);
    rep(i,1,n+1){
        auto[a,b] = school[i];
        lft[i] = -1;
        rep(j,0,m){
            auto[l,r] = grps[j];
            if(a <= l && r <= b){
                rit[i] = j;
                if(!~lft[i]) lft[i] = j;
            }
        }
        assert(~lft[i]);
    } 
    rep(j,0,m){
        dp[0][j] = 1;
    }
    
    auto fC = [&](int x, int y){
        if(x < 0 || x < y || y < 0) return T(0);
        return C[x][y];
    };
    rep(i,1,m){
        int len = grps[i].SD-grps[i].FS+1;
        T kf = 1,num = 1;
        rep(j,1,min(len+1,n+1)){
            num = num*(len-j+1)%MOD;
            kf = kf*j%MOD;
            big_c[i][j] = num*fpow(kf,MOD-2)%MOD;
        }
    } 
    assert(m <= 1010);
    rep(j,1,m){
        rep(p,0,2) rep(num,1,n+1){
            T cur = 0;
            rep(l,1+p,num+1){
                cur = (cur+fC(num-1-p,l-1-p)*big_c[j][l]%MOD)%MOD;
            } 
            pre[j][p][num] = cur;
        } 
    }
    
    T ans = 0; 
    rep(i,1,n+1){
        rep(j,lft[i],rit[i]+1){
            // fix first one we use same group j
            int num_with = 0;
            for(int k = i; k >= 1; k--)if(lft[k] <= j && j <= rit[k]){
                num_with++;
                dp[i][j] = (dp[i][j]+pre[j][k!=i][num_with]*dp[k-1][j-1]%MOD)%MOD;
            }
            ans = (ans+dp[i][j])%MOD;
        }
        // build psa
        rep(j,1,m){
            dp[i][j] = (dp[i][j]+dp[i][j-1])%MOD;
        }
        rep(j,0,m){
            dp[i][j] = (dp[i][j]+dp[i-1][j])%MOD;
        }
    }
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 995 ms 36948 KB Output is correct
2 Correct 991 ms 37164 KB Output is correct
3 Correct 996 ms 37204 KB Output is correct
4 Correct 989 ms 37220 KB Output is correct
5 Correct 989 ms 36972 KB Output is correct
6 Correct 994 ms 36980 KB Output is correct
7 Correct 992 ms 36976 KB Output is correct
8 Correct 982 ms 36992 KB Output is correct
9 Correct 999 ms 36992 KB Output is correct
10 Correct 986 ms 36996 KB Output is correct
11 Correct 993 ms 36992 KB Output is correct
12 Correct 998 ms 37492 KB Output is correct
13 Correct 999 ms 36984 KB Output is correct
14 Correct 990 ms 37204 KB Output is correct
15 Correct 992 ms 36984 KB Output is correct
16 Correct 179 ms 21336 KB Output is correct
17 Correct 191 ms 21576 KB Output is correct
18 Correct 186 ms 21320 KB Output is correct
19 Correct 192 ms 21580 KB Output is correct
20 Correct 187 ms 21328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 995 ms 36948 KB Output is correct
2 Correct 991 ms 37164 KB Output is correct
3 Correct 996 ms 37204 KB Output is correct
4 Correct 989 ms 37220 KB Output is correct
5 Correct 989 ms 36972 KB Output is correct
6 Correct 994 ms 36980 KB Output is correct
7 Correct 992 ms 36976 KB Output is correct
8 Correct 982 ms 36992 KB Output is correct
9 Correct 999 ms 36992 KB Output is correct
10 Correct 986 ms 36996 KB Output is correct
11 Correct 993 ms 36992 KB Output is correct
12 Correct 998 ms 37492 KB Output is correct
13 Correct 999 ms 36984 KB Output is correct
14 Correct 990 ms 37204 KB Output is correct
15 Correct 992 ms 36984 KB Output is correct
16 Correct 179 ms 21336 KB Output is correct
17 Correct 191 ms 21576 KB Output is correct
18 Correct 186 ms 21320 KB Output is correct
19 Correct 192 ms 21580 KB Output is correct
20 Correct 187 ms 21328 KB Output is correct
21 Correct 974 ms 36980 KB Output is correct
22 Correct 982 ms 37244 KB Output is correct
23 Correct 961 ms 36968 KB Output is correct
24 Correct 957 ms 36980 KB Output is correct
25 Correct 977 ms 36948 KB Output is correct
26 Correct 1043 ms 36944 KB Output is correct
27 Correct 1055 ms 36852 KB Output is correct
28 Correct 1033 ms 36984 KB Output is correct
29 Correct 1027 ms 37200 KB Output is correct
30 Correct 992 ms 36980 KB Output is correct
31 Correct 993 ms 36984 KB Output is correct
32 Correct 995 ms 37200 KB Output is correct
33 Correct 997 ms 36980 KB Output is correct
34 Correct 990 ms 37492 KB Output is correct
35 Correct 985 ms 37228 KB Output is correct
36 Correct 993 ms 37228 KB Output is correct
37 Correct 987 ms 37200 KB Output is correct
38 Correct 998 ms 37216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19288 KB Output is correct
2 Correct 14 ms 19288 KB Output is correct
3 Correct 14 ms 19292 KB Output is correct
4 Correct 14 ms 19288 KB Output is correct
5 Correct 16 ms 19288 KB Output is correct
6 Correct 15 ms 19288 KB Output is correct
7 Correct 16 ms 19280 KB Output is correct
8 Correct 16 ms 19292 KB Output is correct
9 Correct 15 ms 19280 KB Output is correct
10 Correct 16 ms 19280 KB Output is correct
11 Correct 15 ms 19292 KB Output is correct
12 Correct 14 ms 19288 KB Output is correct
13 Correct 14 ms 19292 KB Output is correct
14 Correct 14 ms 19292 KB Output is correct
15 Correct 14 ms 19256 KB Output is correct
16 Correct 11 ms 17232 KB Output is correct
17 Correct 9 ms 17244 KB Output is correct
18 Correct 9 ms 17244 KB Output is correct
19 Correct 9 ms 17220 KB Output is correct
20 Correct 9 ms 17244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 995 ms 36948 KB Output is correct
2 Correct 991 ms 37164 KB Output is correct
3 Correct 996 ms 37204 KB Output is correct
4 Correct 989 ms 37220 KB Output is correct
5 Correct 989 ms 36972 KB Output is correct
6 Correct 994 ms 36980 KB Output is correct
7 Correct 992 ms 36976 KB Output is correct
8 Correct 982 ms 36992 KB Output is correct
9 Correct 999 ms 36992 KB Output is correct
10 Correct 986 ms 36996 KB Output is correct
11 Correct 993 ms 36992 KB Output is correct
12 Correct 998 ms 37492 KB Output is correct
13 Correct 999 ms 36984 KB Output is correct
14 Correct 990 ms 37204 KB Output is correct
15 Correct 992 ms 36984 KB Output is correct
16 Correct 179 ms 21336 KB Output is correct
17 Correct 191 ms 21576 KB Output is correct
18 Correct 186 ms 21320 KB Output is correct
19 Correct 192 ms 21580 KB Output is correct
20 Correct 187 ms 21328 KB Output is correct
21 Correct 974 ms 36980 KB Output is correct
22 Correct 982 ms 37244 KB Output is correct
23 Correct 961 ms 36968 KB Output is correct
24 Correct 957 ms 36980 KB Output is correct
25 Correct 977 ms 36948 KB Output is correct
26 Correct 1043 ms 36944 KB Output is correct
27 Correct 1055 ms 36852 KB Output is correct
28 Correct 1033 ms 36984 KB Output is correct
29 Correct 1027 ms 37200 KB Output is correct
30 Correct 992 ms 36980 KB Output is correct
31 Correct 993 ms 36984 KB Output is correct
32 Correct 995 ms 37200 KB Output is correct
33 Correct 997 ms 36980 KB Output is correct
34 Correct 990 ms 37492 KB Output is correct
35 Correct 985 ms 37228 KB Output is correct
36 Correct 993 ms 37228 KB Output is correct
37 Correct 987 ms 37200 KB Output is correct
38 Correct 998 ms 37216 KB Output is correct
39 Correct 15 ms 19288 KB Output is correct
40 Correct 14 ms 19288 KB Output is correct
41 Correct 14 ms 19292 KB Output is correct
42 Correct 14 ms 19288 KB Output is correct
43 Correct 16 ms 19288 KB Output is correct
44 Correct 15 ms 19288 KB Output is correct
45 Correct 16 ms 19280 KB Output is correct
46 Correct 16 ms 19292 KB Output is correct
47 Correct 15 ms 19280 KB Output is correct
48 Correct 16 ms 19280 KB Output is correct
49 Correct 15 ms 19292 KB Output is correct
50 Correct 14 ms 19288 KB Output is correct
51 Correct 14 ms 19292 KB Output is correct
52 Correct 14 ms 19292 KB Output is correct
53 Correct 14 ms 19256 KB Output is correct
54 Correct 11 ms 17232 KB Output is correct
55 Correct 9 ms 17244 KB Output is correct
56 Correct 9 ms 17244 KB Output is correct
57 Correct 9 ms 17220 KB Output is correct
58 Correct 9 ms 17244 KB Output is correct
59 Correct 1126 ms 37232 KB Output is correct
60 Correct 1118 ms 37208 KB Output is correct
61 Correct 1113 ms 37204 KB Output is correct
62 Correct 1134 ms 36972 KB Output is correct
63 Correct 1121 ms 36980 KB Output is correct
64 Correct 1232 ms 36992 KB Output is correct
65 Correct 1235 ms 36968 KB Output is correct
66 Correct 1253 ms 37048 KB Output is correct
67 Correct 1228 ms 37228 KB Output is correct
68 Correct 1237 ms 37204 KB Output is correct
69 Correct 1102 ms 36996 KB Output is correct
70 Correct 1106 ms 36980 KB Output is correct
71 Correct 1106 ms 36968 KB Output is correct
72 Correct 1109 ms 36972 KB Output is correct
73 Correct 1114 ms 37200 KB Output is correct
74 Correct 211 ms 21324 KB Output is correct
75 Correct 212 ms 21320 KB Output is correct
76 Correct 209 ms 21332 KB Output is correct
77 Correct 208 ms 21324 KB Output is correct
78 Correct 207 ms 21328 KB Output is correct