Submission #461290

# Submission time Handle Problem Language Result Execution time Memory
461290 2021-08-09T16:51:28 Z wind_reaper Boat (APIO16_boat) C++17
100 / 100
1916 ms 4492 KB
#pragma GCC Optimize "trapv"
#include <bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
*/

using namespace std;
// using namespace __gnu_pbds;
using namespace chrono;

// mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
/*
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
*/

//***************** CONSTANTS *****************

const int MOD = 1000000007;
const int MXN = 505;

//***************** GLOBAL VARIABLES *****************

int A[MXN], B[MXN];
int64_t dp[MXN][MXN << 1], F[MXN], iF[MXN], C[MXN];

//***************** AUXILIARY STRUCTS *****************

int64_t power(int64_t n, int64_t x = MOD - 2){
	int64_t res = 1;
	while(x){
		if(x & 1)
			res = (res * n) % MOD;

		x >>= 1;
		n = (n * n) % MOD;
	}
	return res;
}

inline int64_t nCk(int n, int k){
	return F[n] * ((iF[n - k] * iF[k]) % MOD) % MOD;
}


//***************** MAIN BODY *****************

/*
	processing a range of values [D[i], D[i+1])

	we consider every [l, r] such that [1, l-1] lie in [..., D[i])
	and [l, r] lie in [D[i], D[i+1])

	> find the number of schools in [l, r] which can send boats in range [D[i], D[i+1])

	other schools are ignored

	>> add (D[i+1] - D[i]) C x where x is value calculated in >

	dp[i][j] -> insert i into j th interval

	dp[r][j] = sum(dp[l-1][j-1] + ways(l, r, j))

	ways(l, r, j) = >>

	C[i] -> no. of ways of picking up <= i items in increasing order from len items

	dp[i][j] = 
*/

void solve(){
	int N;
	cin >> N;

	vector<int> D;

	for(int i = 0; i < N; i++){
		cin >> A[i] >> B[i];
		D.push_back(A[i]);
		D.push_back(B[i] + 1);
	}

	F[0] = F[1] = 1;
	for(int i = 2; i < MXN; i++){
		F[i] = (F[i-1] * 1LL * i) % MOD;
	}

	iF[MXN - 1] = power(F[MXN - 1]);

	for(int i = MXN - 2; i >= 0; --i){
		iF[i] = (iF[i+1] * 1LL * (i + 1)) % MOD;
	}

	sort(D.begin(), D.end());
	D.erase(unique(D.begin(), D.end()), D.end());

	int n = D.size() - 1;

	int64_t ans = 0;

	for(int _ = 0; _ < n; _++){
		int len = D[_+1] - D[_];
		for(int i = 1; i <= N; i++){
			int64_t p = 1; C[i] = 0;
			for(int j = 1; j <= i; j++){
				p = (p * 1LL * (len - j + 1)) % MOD;
				C[i] = (C[i] + (((p * iF[j]) % MOD) * nCk(i - 1, j - 1)) % MOD) % MOD;
			}
		}

		for(int i = 0; i < N; i++) if(A[i] <= D[_] && B[i] >= D[_+1] - 1) {
			int cnt = 1;
			for(int j = i; --j >= 0;){
				if(_) (dp[i][_] += dp[j][_-1] * C[cnt]) %= MOD;
				cnt += (A[j] <= D[_] && B[j] >= D[_+1] - 1);
			}
			(dp[i][_] += C[cnt]) %= MOD;
			ans = (ans + dp[i][_]) % MOD;
		}

		for(int i = 0; i < N; i++)
			(dp[i][_] += dp[i][_-1]) %= MOD;
	}

	cout << ans << '\n';
}

//***************** *****************

int32_t main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);

	#ifdef LOCAL
		auto begin = high_resolution_clock::now();
	#endif

	int tc = 1;
	// cin >> tc; 
	for (int t = 0; t < tc; t++)
		solve();

	#ifdef LOCAL 
		auto end = high_resolution_clock::now();
		cout << fixed << setprecision(4);
		cout << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
	#endif

	return 0;
}

/*
If code gives a WA, check for the following : 
1. I/O format

2. Are you clearing all global variables in between tests if multitests are a thing

3. Can you definitively prove the logic

4. If the code gives an inexplicable TLE, and you are sure you have the best possible complexity,
use faster io
*/

Compilation message

boat.cpp:1: warning: ignoring '#pragma GCC Optimize' [-Wunknown-pragmas]
    1 | #pragma GCC Optimize "trapv"
      |
# Verdict Execution time Memory Grader output
1 Correct 1598 ms 4268 KB Output is correct
2 Correct 1613 ms 4336 KB Output is correct
3 Correct 1589 ms 4264 KB Output is correct
4 Correct 1619 ms 4352 KB Output is correct
5 Correct 1588 ms 4384 KB Output is correct
6 Correct 1615 ms 4272 KB Output is correct
7 Correct 1598 ms 4372 KB Output is correct
8 Correct 1607 ms 4276 KB Output is correct
9 Correct 1595 ms 4492 KB Output is correct
10 Correct 1595 ms 4336 KB Output is correct
11 Correct 1600 ms 4268 KB Output is correct
12 Correct 1606 ms 4400 KB Output is correct
13 Correct 1592 ms 4276 KB Output is correct
14 Correct 1591 ms 4276 KB Output is correct
15 Correct 1589 ms 4336 KB Output is correct
16 Correct 279 ms 3012 KB Output is correct
17 Correct 308 ms 3100 KB Output is correct
18 Correct 293 ms 3012 KB Output is correct
19 Correct 305 ms 3020 KB Output is correct
20 Correct 295 ms 2936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1598 ms 4268 KB Output is correct
2 Correct 1613 ms 4336 KB Output is correct
3 Correct 1589 ms 4264 KB Output is correct
4 Correct 1619 ms 4352 KB Output is correct
5 Correct 1588 ms 4384 KB Output is correct
6 Correct 1615 ms 4272 KB Output is correct
7 Correct 1598 ms 4372 KB Output is correct
8 Correct 1607 ms 4276 KB Output is correct
9 Correct 1595 ms 4492 KB Output is correct
10 Correct 1595 ms 4336 KB Output is correct
11 Correct 1600 ms 4268 KB Output is correct
12 Correct 1606 ms 4400 KB Output is correct
13 Correct 1592 ms 4276 KB Output is correct
14 Correct 1591 ms 4276 KB Output is correct
15 Correct 1589 ms 4336 KB Output is correct
16 Correct 279 ms 3012 KB Output is correct
17 Correct 308 ms 3100 KB Output is correct
18 Correct 293 ms 3012 KB Output is correct
19 Correct 305 ms 3020 KB Output is correct
20 Correct 295 ms 2936 KB Output is correct
21 Correct 1653 ms 4396 KB Output is correct
22 Correct 1706 ms 4360 KB Output is correct
23 Correct 1644 ms 4348 KB Output is correct
24 Correct 1642 ms 4320 KB Output is correct
25 Correct 1652 ms 4304 KB Output is correct
26 Correct 1668 ms 4284 KB Output is correct
27 Correct 1745 ms 4276 KB Output is correct
28 Correct 1699 ms 4396 KB Output is correct
29 Correct 1648 ms 4220 KB Output is correct
30 Correct 1602 ms 4444 KB Output is correct
31 Correct 1593 ms 4420 KB Output is correct
32 Correct 1606 ms 4288 KB Output is correct
33 Correct 1625 ms 4424 KB Output is correct
34 Correct 1631 ms 4284 KB Output is correct
35 Correct 1638 ms 4356 KB Output is correct
36 Correct 1660 ms 4276 KB Output is correct
37 Correct 1619 ms 4288 KB Output is correct
38 Correct 1593 ms 4272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 860 KB Output is correct
2 Correct 16 ms 836 KB Output is correct
3 Correct 16 ms 912 KB Output is correct
4 Correct 15 ms 792 KB Output is correct
5 Correct 16 ms 880 KB Output is correct
6 Correct 16 ms 860 KB Output is correct
7 Correct 16 ms 852 KB Output is correct
8 Correct 16 ms 844 KB Output is correct
9 Correct 17 ms 900 KB Output is correct
10 Correct 16 ms 852 KB Output is correct
11 Correct 16 ms 972 KB Output is correct
12 Correct 15 ms 876 KB Output is correct
13 Correct 15 ms 816 KB Output is correct
14 Correct 15 ms 836 KB Output is correct
15 Correct 15 ms 800 KB Output is correct
16 Correct 9 ms 716 KB Output is correct
17 Correct 9 ms 760 KB Output is correct
18 Correct 9 ms 716 KB Output is correct
19 Correct 9 ms 716 KB Output is correct
20 Correct 9 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1598 ms 4268 KB Output is correct
2 Correct 1613 ms 4336 KB Output is correct
3 Correct 1589 ms 4264 KB Output is correct
4 Correct 1619 ms 4352 KB Output is correct
5 Correct 1588 ms 4384 KB Output is correct
6 Correct 1615 ms 4272 KB Output is correct
7 Correct 1598 ms 4372 KB Output is correct
8 Correct 1607 ms 4276 KB Output is correct
9 Correct 1595 ms 4492 KB Output is correct
10 Correct 1595 ms 4336 KB Output is correct
11 Correct 1600 ms 4268 KB Output is correct
12 Correct 1606 ms 4400 KB Output is correct
13 Correct 1592 ms 4276 KB Output is correct
14 Correct 1591 ms 4276 KB Output is correct
15 Correct 1589 ms 4336 KB Output is correct
16 Correct 279 ms 3012 KB Output is correct
17 Correct 308 ms 3100 KB Output is correct
18 Correct 293 ms 3012 KB Output is correct
19 Correct 305 ms 3020 KB Output is correct
20 Correct 295 ms 2936 KB Output is correct
21 Correct 1653 ms 4396 KB Output is correct
22 Correct 1706 ms 4360 KB Output is correct
23 Correct 1644 ms 4348 KB Output is correct
24 Correct 1642 ms 4320 KB Output is correct
25 Correct 1652 ms 4304 KB Output is correct
26 Correct 1668 ms 4284 KB Output is correct
27 Correct 1745 ms 4276 KB Output is correct
28 Correct 1699 ms 4396 KB Output is correct
29 Correct 1648 ms 4220 KB Output is correct
30 Correct 1602 ms 4444 KB Output is correct
31 Correct 1593 ms 4420 KB Output is correct
32 Correct 1606 ms 4288 KB Output is correct
33 Correct 1625 ms 4424 KB Output is correct
34 Correct 1631 ms 4284 KB Output is correct
35 Correct 1638 ms 4356 KB Output is correct
36 Correct 1660 ms 4276 KB Output is correct
37 Correct 1619 ms 4288 KB Output is correct
38 Correct 1593 ms 4272 KB Output is correct
39 Correct 15 ms 860 KB Output is correct
40 Correct 16 ms 836 KB Output is correct
41 Correct 16 ms 912 KB Output is correct
42 Correct 15 ms 792 KB Output is correct
43 Correct 16 ms 880 KB Output is correct
44 Correct 16 ms 860 KB Output is correct
45 Correct 16 ms 852 KB Output is correct
46 Correct 16 ms 844 KB Output is correct
47 Correct 17 ms 900 KB Output is correct
48 Correct 16 ms 852 KB Output is correct
49 Correct 16 ms 972 KB Output is correct
50 Correct 15 ms 876 KB Output is correct
51 Correct 15 ms 816 KB Output is correct
52 Correct 15 ms 836 KB Output is correct
53 Correct 15 ms 800 KB Output is correct
54 Correct 9 ms 716 KB Output is correct
55 Correct 9 ms 760 KB Output is correct
56 Correct 9 ms 716 KB Output is correct
57 Correct 9 ms 716 KB Output is correct
58 Correct 9 ms 716 KB Output is correct
59 Correct 1813 ms 4304 KB Output is correct
60 Correct 1801 ms 4360 KB Output is correct
61 Correct 1807 ms 4280 KB Output is correct
62 Correct 1813 ms 4380 KB Output is correct
63 Correct 1798 ms 4284 KB Output is correct
64 Correct 1916 ms 4440 KB Output is correct
65 Correct 1899 ms 4288 KB Output is correct
66 Correct 1891 ms 4316 KB Output is correct
67 Correct 1886 ms 4292 KB Output is correct
68 Correct 1901 ms 4316 KB Output is correct
69 Correct 1761 ms 4340 KB Output is correct
70 Correct 1750 ms 4300 KB Output is correct
71 Correct 1756 ms 4280 KB Output is correct
72 Correct 1766 ms 4416 KB Output is correct
73 Correct 1778 ms 4284 KB Output is correct
74 Correct 334 ms 2988 KB Output is correct
75 Correct 343 ms 3056 KB Output is correct
76 Correct 331 ms 3012 KB Output is correct
77 Correct 336 ms 2984 KB Output is correct
78 Correct 329 ms 2924 KB Output is correct