Submission #447806

# Submission time Handle Problem Language Result Execution time Memory
447806 2021-07-27T14:54:22 Z dutch Boat (APIO16_boat) C++17
100 / 100
1631 ms 4420 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int MOD = 1e9+7, LIM = 501;

int modInverse(int x){
	int r = 1, p = (MOD - 2) * 2;
	for(int y=x; p/=2; y=(y*y) % MOD)
		if(p & 1) r = (r*y) % MOD;
	return r;
}

int n, a[LIM], b[LIM], dp[LIM][LIM*2], F[LIM], invF[LIM], C[LIM];
bool valid[LIM];
vector<int> pts;

int nCr(int x, int y){
	return (((F[x] * invF[y]) % MOD) * invF[x-y]) % MOD;
}

signed main(){
	cin.tie(0)->sync_with_stdio(0);

	cin >> n;
	for(int i=0; i<n; ++i){
		cin >> a[i] >> b[i];
		pts.push_back(a[i]);
		pts.push_back(b[i]+1);
	}
	sort(pts.begin(), pts.end());
	pts.resize(unique(pts.begin(), pts.end()) - pts.begin());

	int m = size(pts) - 1, ans = 0;

	F[0] = invF[0] = 1;
	for(int i=1; i<=n; ++i){
		F[i] = (F[i-1] * i) % MOD;
		invF[i] = modInverse(F[i]);
	}
	for(int k=0; k<m; ++k){
		for(int i=1; i<=n; ++i){
			int p = 1; C[i] = 0;
			for(int j=1; j<=i; ++j){
				(p *= pts[k+1] - pts[k] - j + 1) %= MOD;
				(C[i] += ((p * invF[j]) % MOD) * nCr(i-1, j-1)) %= MOD;
			}
		}
		for(int i=0; i<n; ++i){
			valid[i] = a[i] <= pts[k] && pts[k+1] <= b[i]+1;
			if(valid[i]){
				int cnt = 1;
				for(int j=i; --j>=0; ){
					if(k) (dp[i][k] += dp[j][k-1] * C[cnt]) %= MOD;
					cnt += valid[j];
				}
				(dp[i][k] += C[cnt]) %= MOD;
				(ans += dp[i][k]) %= MOD;
			}
			if(k){
				(dp[i][k] += dp[i][k-1]) %= MOD;
			}
		}
	}
	cout << (ans % MOD + MOD) % MOD;
}
# Verdict Execution time Memory Grader output
1 Correct 1314 ms 4256 KB Output is correct
2 Correct 1370 ms 4292 KB Output is correct
3 Correct 1320 ms 4420 KB Output is correct
4 Correct 1322 ms 4292 KB Output is correct
5 Correct 1310 ms 4164 KB Output is correct
6 Correct 1352 ms 4264 KB Output is correct
7 Correct 1313 ms 4268 KB Output is correct
8 Correct 1326 ms 4292 KB Output is correct
9 Correct 1314 ms 4192 KB Output is correct
10 Correct 1323 ms 4312 KB Output is correct
11 Correct 1331 ms 4252 KB Output is correct
12 Correct 1313 ms 4300 KB Output is correct
13 Correct 1324 ms 4196 KB Output is correct
14 Correct 1335 ms 4164 KB Output is correct
15 Correct 1317 ms 4244 KB Output is correct
16 Correct 231 ms 2884 KB Output is correct
17 Correct 252 ms 3012 KB Output is correct
18 Correct 248 ms 3012 KB Output is correct
19 Correct 252 ms 2968 KB Output is correct
20 Correct 244 ms 3012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1314 ms 4256 KB Output is correct
2 Correct 1370 ms 4292 KB Output is correct
3 Correct 1320 ms 4420 KB Output is correct
4 Correct 1322 ms 4292 KB Output is correct
5 Correct 1310 ms 4164 KB Output is correct
6 Correct 1352 ms 4264 KB Output is correct
7 Correct 1313 ms 4268 KB Output is correct
8 Correct 1326 ms 4292 KB Output is correct
9 Correct 1314 ms 4192 KB Output is correct
10 Correct 1323 ms 4312 KB Output is correct
11 Correct 1331 ms 4252 KB Output is correct
12 Correct 1313 ms 4300 KB Output is correct
13 Correct 1324 ms 4196 KB Output is correct
14 Correct 1335 ms 4164 KB Output is correct
15 Correct 1317 ms 4244 KB Output is correct
16 Correct 231 ms 2884 KB Output is correct
17 Correct 252 ms 3012 KB Output is correct
18 Correct 248 ms 3012 KB Output is correct
19 Correct 252 ms 2968 KB Output is correct
20 Correct 244 ms 3012 KB Output is correct
21 Correct 1437 ms 4340 KB Output is correct
22 Correct 1402 ms 4368 KB Output is correct
23 Correct 1379 ms 4256 KB Output is correct
24 Correct 1374 ms 4256 KB Output is correct
25 Correct 1367 ms 4252 KB Output is correct
26 Correct 1410 ms 4344 KB Output is correct
27 Correct 1485 ms 4252 KB Output is correct
28 Correct 1434 ms 4256 KB Output is correct
29 Correct 1503 ms 4256 KB Output is correct
30 Correct 1390 ms 4256 KB Output is correct
31 Correct 1390 ms 4260 KB Output is correct
32 Correct 1371 ms 4256 KB Output is correct
33 Correct 1368 ms 4260 KB Output is correct
34 Correct 1372 ms 4252 KB Output is correct
35 Correct 1314 ms 4352 KB Output is correct
36 Correct 1324 ms 4256 KB Output is correct
37 Correct 1334 ms 4256 KB Output is correct
38 Correct 1370 ms 4260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 844 KB Output is correct
2 Correct 13 ms 844 KB Output is correct
3 Correct 13 ms 872 KB Output is correct
4 Correct 15 ms 868 KB Output is correct
5 Correct 28 ms 836 KB Output is correct
6 Correct 14 ms 820 KB Output is correct
7 Correct 15 ms 848 KB Output is correct
8 Correct 15 ms 796 KB Output is correct
9 Correct 14 ms 844 KB Output is correct
10 Correct 14 ms 844 KB Output is correct
11 Correct 14 ms 844 KB Output is correct
12 Correct 13 ms 844 KB Output is correct
13 Correct 13 ms 844 KB Output is correct
14 Correct 13 ms 800 KB Output is correct
15 Correct 16 ms 844 KB Output is correct
16 Correct 10 ms 716 KB Output is correct
17 Correct 10 ms 764 KB Output is correct
18 Correct 8 ms 772 KB Output is correct
19 Correct 8 ms 716 KB Output is correct
20 Correct 11 ms 784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1314 ms 4256 KB Output is correct
2 Correct 1370 ms 4292 KB Output is correct
3 Correct 1320 ms 4420 KB Output is correct
4 Correct 1322 ms 4292 KB Output is correct
5 Correct 1310 ms 4164 KB Output is correct
6 Correct 1352 ms 4264 KB Output is correct
7 Correct 1313 ms 4268 KB Output is correct
8 Correct 1326 ms 4292 KB Output is correct
9 Correct 1314 ms 4192 KB Output is correct
10 Correct 1323 ms 4312 KB Output is correct
11 Correct 1331 ms 4252 KB Output is correct
12 Correct 1313 ms 4300 KB Output is correct
13 Correct 1324 ms 4196 KB Output is correct
14 Correct 1335 ms 4164 KB Output is correct
15 Correct 1317 ms 4244 KB Output is correct
16 Correct 231 ms 2884 KB Output is correct
17 Correct 252 ms 3012 KB Output is correct
18 Correct 248 ms 3012 KB Output is correct
19 Correct 252 ms 2968 KB Output is correct
20 Correct 244 ms 3012 KB Output is correct
21 Correct 1437 ms 4340 KB Output is correct
22 Correct 1402 ms 4368 KB Output is correct
23 Correct 1379 ms 4256 KB Output is correct
24 Correct 1374 ms 4256 KB Output is correct
25 Correct 1367 ms 4252 KB Output is correct
26 Correct 1410 ms 4344 KB Output is correct
27 Correct 1485 ms 4252 KB Output is correct
28 Correct 1434 ms 4256 KB Output is correct
29 Correct 1503 ms 4256 KB Output is correct
30 Correct 1390 ms 4256 KB Output is correct
31 Correct 1390 ms 4260 KB Output is correct
32 Correct 1371 ms 4256 KB Output is correct
33 Correct 1368 ms 4260 KB Output is correct
34 Correct 1372 ms 4252 KB Output is correct
35 Correct 1314 ms 4352 KB Output is correct
36 Correct 1324 ms 4256 KB Output is correct
37 Correct 1334 ms 4256 KB Output is correct
38 Correct 1370 ms 4260 KB Output is correct
39 Correct 13 ms 844 KB Output is correct
40 Correct 13 ms 844 KB Output is correct
41 Correct 13 ms 872 KB Output is correct
42 Correct 15 ms 868 KB Output is correct
43 Correct 28 ms 836 KB Output is correct
44 Correct 14 ms 820 KB Output is correct
45 Correct 15 ms 848 KB Output is correct
46 Correct 15 ms 796 KB Output is correct
47 Correct 14 ms 844 KB Output is correct
48 Correct 14 ms 844 KB Output is correct
49 Correct 14 ms 844 KB Output is correct
50 Correct 13 ms 844 KB Output is correct
51 Correct 13 ms 844 KB Output is correct
52 Correct 13 ms 800 KB Output is correct
53 Correct 16 ms 844 KB Output is correct
54 Correct 10 ms 716 KB Output is correct
55 Correct 10 ms 764 KB Output is correct
56 Correct 8 ms 772 KB Output is correct
57 Correct 8 ms 716 KB Output is correct
58 Correct 11 ms 784 KB Output is correct
59 Correct 1567 ms 4260 KB Output is correct
60 Correct 1551 ms 4272 KB Output is correct
61 Correct 1503 ms 4292 KB Output is correct
62 Correct 1523 ms 4336 KB Output is correct
63 Correct 1534 ms 4268 KB Output is correct
64 Correct 1624 ms 4260 KB Output is correct
65 Correct 1631 ms 4252 KB Output is correct
66 Correct 1606 ms 4392 KB Output is correct
67 Correct 1604 ms 4256 KB Output is correct
68 Correct 1595 ms 4260 KB Output is correct
69 Correct 1470 ms 4332 KB Output is correct
70 Correct 1462 ms 4304 KB Output is correct
71 Correct 1491 ms 4256 KB Output is correct
72 Correct 1488 ms 4300 KB Output is correct
73 Correct 1471 ms 4296 KB Output is correct
74 Correct 277 ms 3024 KB Output is correct
75 Correct 281 ms 3036 KB Output is correct
76 Correct 280 ms 3008 KB Output is correct
77 Correct 285 ms 3028 KB Output is correct
78 Correct 279 ms 3020 KB Output is correct