답안 #710557

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710557 2023-03-15T11:03:37 Z lukameladze Boat (APIO16_boat) C++14
100 / 100
1344 ms 27064 KB
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 1005, mod = 1e9 + 7;
int t,n,a[N],b[N],pr[N][N],dp[N][N],inv[N],fq[N],ch[N][N],chh[N][N],sum[N][N];
map <int, int> shes;
vector <int> v;
int f_p(int base, int power) {
	int result = 1;
	while (power > 0) {
		if (power % 2) result = (result * base) % mod;
		base = (base * base) % mod;
		power /= 2;
	}
	return result;
}
int C(int n, int k) {
	if (n < k) return 0;
	if (n == 0 && k == 0) return 1;
	int pas = 1;
	for (int i = n; i >= n - k + 1; i--){
		pas *= i;
		pas %= mod;
	} 
	pas *= inv[k];pas %= mod;
	return pas;
}
int check(int le, int ri, int a, int b) {
	return (max(a,le) <= min(b,ri));
}
main() {
    std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    for (int i = 1; i <= n; i++) {
    	cin>>a[i]>>b[i];
    	v.pb(a[i]);
    	v.pb(b[i] + 1);
	}
	fq[0] = 1; inv[0] = 1;
	for (int i = 1; i < N; i++) {
		fq[i] = fq[i - 1] * i % mod;
	}
	inv[N - 1] = f_p(fq[N - 1], mod - 2);
	for (int i = N - 2; i >= 1; i--) {
		inv[i] = (inv[i + 1] * (i + 1)) % mod;
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end())-v.begin());
	
	vector <pii> seg;
	vector <int> szz;
	seg.pb({-1,-1});
	for (int i = 1; i < v.size(); i++) {
		seg.pb({v[i - 1], v[i] - 1}); 
		szz.pb(v[i] - v[i - 1]);
	}
	sort(szz.begin(), szz.end());
	szz.resize(unique(szz.begin(), szz.end())-szz.begin());
	dp[0][0] = 1;
	pr[0][0] = 1; 
	ch[0][0] = 1;
	ch[1][1] = ch[1][0] = 1;
	for (int i = 2; i <= n; i++) {
	    ch[i][0] = 1;
	    for (int j = 1; j <= i; j++) {
	        ch[i][j] = (ch[i - 1][j] + ch[i - 1][j - 1]) % mod;
	    }
	}
	int raod = 0;
	for (int sz : szz) { raod++; shes[sz] = raod;
	    for (int cnt = 1; cnt <= n; cnt++) {
	        chh[raod][cnt] = C(sz, cnt);
	        for (int cn = 1; cn <= cnt; cn++) {
	            sum[raod][cnt] += chh[raod][cn] * ch[cnt - 1][cn - 1] % mod;
	            if (sum[raod][cnt] >= mod) sum[raod][cnt] -= mod;
	        }
	    }
	}
 
	int ans = 0;
	for (int i = 1; i < seg.size(); i++) pr[0][i] = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j < seg.size(); j++) {
			int le = seg[j].f; int ri = seg[j].s;
			if (check(le,ri,a[i],b[i])) { 
				int cnt = 1;
				int sh = shes[ri - le + 1];
				for (int prless = i - 1; prless >= 0; prless--) {
				    dp[i][j] += sum[sh][cnt] * pr[prless][j - 1] % mod;
				    //cout<<i<<" "<<j<<" "<<ri - le + 1<<" "<<cnt<<" "<<sum[ri - le + 1][cnt]<<"  "<<pr[prless][j - 1]<<endl;
				    if (dp[i][j] >= mod) dp[i][j] -= mod;
					if ((check(le,ri,a[prless],b[prless]))) cnt++;
				}
			}
			ans += dp[i][j]; if (ans >= mod) ans -= mod;
			pr[i][j] = (dp[i][j] + pr[i][j - 1]);  if (pr[i][j] >= mod) pr[i][j] -= mod;
		}
	}
	cout<<ans<<"\n";
}

Compilation message

boat.cpp:35:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   35 | main() {
      | ^~~~
boat.cpp: In function 'int main()':
boat.cpp:57:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |  for (int i = 1; i < v.size(); i++) {
      |                  ~~^~~~~~~~~~
boat.cpp:85:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |  for (int i = 1; i < seg.size(); i++) pr[0][i] = 1;
      |                  ~~^~~~~~~~~~~~
boat.cpp:87:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   for (int j = 1; j < seg.size(); j++) {
      |                   ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 446 ms 17164 KB Output is correct
2 Correct 415 ms 17168 KB Output is correct
3 Correct 410 ms 17032 KB Output is correct
4 Correct 436 ms 17132 KB Output is correct
5 Correct 402 ms 17012 KB Output is correct
6 Correct 423 ms 17216 KB Output is correct
7 Correct 410 ms 17164 KB Output is correct
8 Correct 435 ms 17180 KB Output is correct
9 Correct 413 ms 17212 KB Output is correct
10 Correct 412 ms 17268 KB Output is correct
11 Correct 418 ms 17184 KB Output is correct
12 Correct 437 ms 17156 KB Output is correct
13 Correct 433 ms 17164 KB Output is correct
14 Correct 410 ms 17172 KB Output is correct
15 Correct 439 ms 17356 KB Output is correct
16 Correct 78 ms 9492 KB Output is correct
17 Correct 82 ms 9628 KB Output is correct
18 Correct 80 ms 9468 KB Output is correct
19 Correct 83 ms 9676 KB Output is correct
20 Correct 77 ms 9540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 446 ms 17164 KB Output is correct
2 Correct 415 ms 17168 KB Output is correct
3 Correct 410 ms 17032 KB Output is correct
4 Correct 436 ms 17132 KB Output is correct
5 Correct 402 ms 17012 KB Output is correct
6 Correct 423 ms 17216 KB Output is correct
7 Correct 410 ms 17164 KB Output is correct
8 Correct 435 ms 17180 KB Output is correct
9 Correct 413 ms 17212 KB Output is correct
10 Correct 412 ms 17268 KB Output is correct
11 Correct 418 ms 17184 KB Output is correct
12 Correct 437 ms 17156 KB Output is correct
13 Correct 433 ms 17164 KB Output is correct
14 Correct 410 ms 17172 KB Output is correct
15 Correct 439 ms 17356 KB Output is correct
16 Correct 78 ms 9492 KB Output is correct
17 Correct 82 ms 9628 KB Output is correct
18 Correct 80 ms 9468 KB Output is correct
19 Correct 83 ms 9676 KB Output is correct
20 Correct 77 ms 9540 KB Output is correct
21 Correct 210 ms 10852 KB Output is correct
22 Correct 201 ms 10828 KB Output is correct
23 Correct 200 ms 10708 KB Output is correct
24 Correct 231 ms 10732 KB Output is correct
25 Correct 208 ms 10828 KB Output is correct
26 Correct 266 ms 11316 KB Output is correct
27 Correct 270 ms 11372 KB Output is correct
28 Correct 314 ms 11280 KB Output is correct
29 Correct 261 ms 11396 KB Output is correct
30 Correct 386 ms 16828 KB Output is correct
31 Correct 348 ms 16636 KB Output is correct
32 Correct 400 ms 16844 KB Output is correct
33 Correct 377 ms 16776 KB Output is correct
34 Correct 362 ms 16648 KB Output is correct
35 Correct 397 ms 16592 KB Output is correct
36 Correct 355 ms 16728 KB Output is correct
37 Correct 378 ms 16684 KB Output is correct
38 Correct 355 ms 16636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 3660 KB Output is correct
2 Correct 11 ms 3668 KB Output is correct
3 Correct 10 ms 3668 KB Output is correct
4 Correct 11 ms 3688 KB Output is correct
5 Correct 11 ms 3660 KB Output is correct
6 Correct 14 ms 3796 KB Output is correct
7 Correct 15 ms 3780 KB Output is correct
8 Correct 12 ms 3796 KB Output is correct
9 Correct 14 ms 3764 KB Output is correct
10 Correct 13 ms 3668 KB Output is correct
11 Correct 13 ms 3668 KB Output is correct
12 Correct 10 ms 3692 KB Output is correct
13 Correct 12 ms 3720 KB Output is correct
14 Correct 10 ms 3768 KB Output is correct
15 Correct 10 ms 3668 KB Output is correct
16 Correct 5 ms 2516 KB Output is correct
17 Correct 5 ms 2516 KB Output is correct
18 Correct 5 ms 2516 KB Output is correct
19 Correct 5 ms 2516 KB Output is correct
20 Correct 6 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 446 ms 17164 KB Output is correct
2 Correct 415 ms 17168 KB Output is correct
3 Correct 410 ms 17032 KB Output is correct
4 Correct 436 ms 17132 KB Output is correct
5 Correct 402 ms 17012 KB Output is correct
6 Correct 423 ms 17216 KB Output is correct
7 Correct 410 ms 17164 KB Output is correct
8 Correct 435 ms 17180 KB Output is correct
9 Correct 413 ms 17212 KB Output is correct
10 Correct 412 ms 17268 KB Output is correct
11 Correct 418 ms 17184 KB Output is correct
12 Correct 437 ms 17156 KB Output is correct
13 Correct 433 ms 17164 KB Output is correct
14 Correct 410 ms 17172 KB Output is correct
15 Correct 439 ms 17356 KB Output is correct
16 Correct 78 ms 9492 KB Output is correct
17 Correct 82 ms 9628 KB Output is correct
18 Correct 80 ms 9468 KB Output is correct
19 Correct 83 ms 9676 KB Output is correct
20 Correct 77 ms 9540 KB Output is correct
21 Correct 210 ms 10852 KB Output is correct
22 Correct 201 ms 10828 KB Output is correct
23 Correct 200 ms 10708 KB Output is correct
24 Correct 231 ms 10732 KB Output is correct
25 Correct 208 ms 10828 KB Output is correct
26 Correct 266 ms 11316 KB Output is correct
27 Correct 270 ms 11372 KB Output is correct
28 Correct 314 ms 11280 KB Output is correct
29 Correct 261 ms 11396 KB Output is correct
30 Correct 386 ms 16828 KB Output is correct
31 Correct 348 ms 16636 KB Output is correct
32 Correct 400 ms 16844 KB Output is correct
33 Correct 377 ms 16776 KB Output is correct
34 Correct 362 ms 16648 KB Output is correct
35 Correct 397 ms 16592 KB Output is correct
36 Correct 355 ms 16728 KB Output is correct
37 Correct 378 ms 16684 KB Output is correct
38 Correct 355 ms 16636 KB Output is correct
39 Correct 13 ms 3660 KB Output is correct
40 Correct 11 ms 3668 KB Output is correct
41 Correct 10 ms 3668 KB Output is correct
42 Correct 11 ms 3688 KB Output is correct
43 Correct 11 ms 3660 KB Output is correct
44 Correct 14 ms 3796 KB Output is correct
45 Correct 15 ms 3780 KB Output is correct
46 Correct 12 ms 3796 KB Output is correct
47 Correct 14 ms 3764 KB Output is correct
48 Correct 13 ms 3668 KB Output is correct
49 Correct 13 ms 3668 KB Output is correct
50 Correct 10 ms 3692 KB Output is correct
51 Correct 12 ms 3720 KB Output is correct
52 Correct 10 ms 3768 KB Output is correct
53 Correct 10 ms 3668 KB Output is correct
54 Correct 5 ms 2516 KB Output is correct
55 Correct 5 ms 2516 KB Output is correct
56 Correct 5 ms 2516 KB Output is correct
57 Correct 5 ms 2516 KB Output is correct
58 Correct 6 ms 2644 KB Output is correct
59 Correct 1067 ms 26140 KB Output is correct
60 Correct 1139 ms 26256 KB Output is correct
61 Correct 1034 ms 26040 KB Output is correct
62 Correct 1067 ms 26136 KB Output is correct
63 Correct 1052 ms 26148 KB Output is correct
64 Correct 1197 ms 27064 KB Output is correct
65 Correct 1338 ms 26992 KB Output is correct
66 Correct 1245 ms 26992 KB Output is correct
67 Correct 1344 ms 27020 KB Output is correct
68 Correct 1318 ms 26920 KB Output is correct
69 Correct 1129 ms 26160 KB Output is correct
70 Correct 1170 ms 26140 KB Output is correct
71 Correct 1060 ms 26176 KB Output is correct
72 Correct 1060 ms 26164 KB Output is correct
73 Correct 1029 ms 26276 KB Output is correct
74 Correct 123 ms 9968 KB Output is correct
75 Correct 120 ms 9896 KB Output is correct
76 Correct 121 ms 9928 KB Output is correct
77 Correct 124 ms 9932 KB Output is correct
78 Correct 132 ms 9964 KB Output is correct