Submission #124987

# Submission time Handle Problem Language Result Execution time Memory
124987 2019-07-04T09:43:51 Z Mamnoon_Siam Boat (APIO16_boat) C++17
100 / 100
834 ms 14532 KB
#include <bits/stdc++.h>
using namespace std;

#define debug(x) cout << #x << " = " << x << endl;

using ll = long long;
const int mod = 1e9 + 7;

ll qpow(ll b, ll p) {
	ll ret = 1;
	while(p) {
		if(p & 1) ret = (ret * b) % mod;
		b = (b * b) % mod, p >>= 1;
	} return ret;
}

struct base {
	ll x;
	void fix() {
		if(x < 0 or x >= mod) x %= mod;
		if(x < 0) x += mod;
	}
	base() {x = 0;}
	base(int _x) { x = _x; fix(); }
	base(ll _x) { x = _x; fix(); }
	base operator + (const base &o) const {
		base ret;
		ret.x = x + o.x;
		if(ret.x >= mod) {
			ret.x -= mod;
		}
		return ret;
	}
	base operator - (const base &o) const { return base(x - o.x); }
	base operator * (const base &o) const { return base(x * o.x); }
	base operator / (const base &o) const {
		return base(x * qpow(o.x, mod - 2)); }
	friend istream& operator >> (istream &in, base &o) {
		return in >> o.x; }
	friend ostream& operator << (ostream &out, const base &o) {
		return out << o.x; }
	void operator += (const base &o) { (*this) = (*this) + o; }
	void operator -= (const base &o) { (*this) = (*this) - o; }
	void operator *= (const base &o) { (*this) = (*this) * o; }
	void operator /= (const base &o) { (*this) = (*this) / o; }
};

const int maxn = 505;

int n;
int a[maxn], b[maxn];

int N_rng;
int l[maxn << 1], r[maxn << 1];
// bool vis[maxn][maxn << 1];

base inv[maxn];
base dp[maxn << 1][maxn];
/* 	dp[rng][i] = # of ways to distribute [1..i] among [1..rng]
	ranges s.t. i-th school sends positive amount of boats.
 */
base BC[maxn << 1][maxn];	/* BC = nCr for big N = range size, [range][r] */
base SC[maxn][maxn]; 		/* SC = nCr for small N, all pair (N, R) */
base g[maxn << 1][maxn];
/* g[range][k] = \sum{i = 1}^{k} \binom{k - 1}{i - 1} * \binom{range_size}{i} */

int main(int argc, char const *argv[])
{
	cin >> n;
	vector<int> tmp;
	for(int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
		a[i]--;
		tmp.emplace_back(a[i]);
		tmp.emplace_back(b[i]);
	}

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

	for(int i = 1; i < tmp.size(); i++) {
		N_rng++;
		l[N_rng] = tmp[i - 1];
		r[N_rng] = tmp[i];
	}

	{ // precalc i^{-1}
		inv[0] = 1;
		for(int i = 1; i <= n; i++) {
			inv[i] = base(1) / i;
		}
	}
	{ // precalc SC[][]
		for(int i = 0; i <= n; i++) {
			SC[i][0] = 1;
		}
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= i; j++) {
				SC[i][j] = SC[i - 1][j] + SC[i - 1][j - 1];
			}
		}
	}
	{ // precalc BC[][]
		for(int i = 1; i <= N_rng; i++) {
			BC[i][0] = 1;
			for(int j = 1, sz = r[i] - l[i]; j <= n and sz; j++, sz--) {
				BC[i][j] = BC[i][j - 1] * inv[j] * sz;
			}
		}
	}
	{ // precalc g[][]
		for(int range = 1; range <= N_rng; range++) {
			for(int k = 1; k <= n; k++) {
				for(int i = 1; i <= k; i++) {
					g[range][k] += SC[k - 1][i - 1] * BC[range][i];
				}
			}
		}
	}
	for(int i = 0; i <= N_rng; i++) {
		dp[i][0] = 1;
	}
	for(int rng = 1; rng <= N_rng; rng++) {
		for(int i = 1; i <= n; i++) {
			dp[rng][i] = dp[rng - 1][i];
			if(a[i] <= l[rng] and r[rng] <= b[i]) {
				int cnt = 0;
				for(int k = i; k >= 1; k--) {
					if(a[k] <= l[rng] and r[rng] <= b[k]) {
						cnt++;
					}
					dp[rng][i] += dp[rng - 1][k - 1] * g[rng][cnt];
				}
			}
		}
	}

	base ans = 0;
	for(int i = 1; i <= n; i++) {
		ans += dp[N_rng][i];
	}
	cout << ans << endl;
	return 0;
}

Compilation message

boat.cpp: In function 'int main(int, const char**)':
boat.cpp:81:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < tmp.size(); i++) {
                 ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 494 ms 14328 KB Output is correct
2 Correct 494 ms 14328 KB Output is correct
3 Correct 495 ms 14456 KB Output is correct
4 Correct 495 ms 14384 KB Output is correct
5 Correct 497 ms 14364 KB Output is correct
6 Correct 495 ms 14352 KB Output is correct
7 Correct 494 ms 14456 KB Output is correct
8 Correct 494 ms 14456 KB Output is correct
9 Correct 497 ms 14328 KB Output is correct
10 Correct 494 ms 14456 KB Output is correct
11 Correct 494 ms 14456 KB Output is correct
12 Correct 495 ms 14328 KB Output is correct
13 Correct 494 ms 14456 KB Output is correct
14 Correct 494 ms 14456 KB Output is correct
15 Correct 499 ms 14328 KB Output is correct
16 Correct 99 ms 14328 KB Output is correct
17 Correct 107 ms 14428 KB Output is correct
18 Correct 102 ms 14460 KB Output is correct
19 Correct 105 ms 14332 KB Output is correct
20 Correct 103 ms 14328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 494 ms 14328 KB Output is correct
2 Correct 494 ms 14328 KB Output is correct
3 Correct 495 ms 14456 KB Output is correct
4 Correct 495 ms 14384 KB Output is correct
5 Correct 497 ms 14364 KB Output is correct
6 Correct 495 ms 14352 KB Output is correct
7 Correct 494 ms 14456 KB Output is correct
8 Correct 494 ms 14456 KB Output is correct
9 Correct 497 ms 14328 KB Output is correct
10 Correct 494 ms 14456 KB Output is correct
11 Correct 494 ms 14456 KB Output is correct
12 Correct 495 ms 14328 KB Output is correct
13 Correct 494 ms 14456 KB Output is correct
14 Correct 494 ms 14456 KB Output is correct
15 Correct 499 ms 14328 KB Output is correct
16 Correct 99 ms 14328 KB Output is correct
17 Correct 107 ms 14428 KB Output is correct
18 Correct 102 ms 14460 KB Output is correct
19 Correct 105 ms 14332 KB Output is correct
20 Correct 103 ms 14328 KB Output is correct
21 Correct 603 ms 14356 KB Output is correct
22 Correct 606 ms 14456 KB Output is correct
23 Correct 583 ms 14456 KB Output is correct
24 Correct 589 ms 14348 KB Output is correct
25 Correct 590 ms 14456 KB Output is correct
26 Correct 663 ms 14476 KB Output is correct
27 Correct 683 ms 14352 KB Output is correct
28 Correct 663 ms 14356 KB Output is correct
29 Correct 664 ms 14388 KB Output is correct
30 Correct 492 ms 14456 KB Output is correct
31 Correct 493 ms 14480 KB Output is correct
32 Correct 499 ms 14328 KB Output is correct
33 Correct 495 ms 14456 KB Output is correct
34 Correct 493 ms 14328 KB Output is correct
35 Correct 497 ms 14384 KB Output is correct
36 Correct 492 ms 14456 KB Output is correct
37 Correct 493 ms 14356 KB Output is correct
38 Correct 492 ms 14400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 14328 KB Output is correct
2 Correct 19 ms 14332 KB Output is correct
3 Correct 20 ms 14328 KB Output is correct
4 Correct 20 ms 14328 KB Output is correct
5 Correct 19 ms 14328 KB Output is correct
6 Correct 21 ms 14300 KB Output is correct
7 Correct 20 ms 14328 KB Output is correct
8 Correct 21 ms 14328 KB Output is correct
9 Correct 20 ms 14328 KB Output is correct
10 Correct 21 ms 14456 KB Output is correct
11 Correct 19 ms 14328 KB Output is correct
12 Correct 19 ms 14328 KB Output is correct
13 Correct 20 ms 14328 KB Output is correct
14 Correct 20 ms 14328 KB Output is correct
15 Correct 19 ms 14328 KB Output is correct
16 Correct 17 ms 14328 KB Output is correct
17 Correct 17 ms 14328 KB Output is correct
18 Correct 17 ms 14328 KB Output is correct
19 Correct 16 ms 14328 KB Output is correct
20 Correct 17 ms 14532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 494 ms 14328 KB Output is correct
2 Correct 494 ms 14328 KB Output is correct
3 Correct 495 ms 14456 KB Output is correct
4 Correct 495 ms 14384 KB Output is correct
5 Correct 497 ms 14364 KB Output is correct
6 Correct 495 ms 14352 KB Output is correct
7 Correct 494 ms 14456 KB Output is correct
8 Correct 494 ms 14456 KB Output is correct
9 Correct 497 ms 14328 KB Output is correct
10 Correct 494 ms 14456 KB Output is correct
11 Correct 494 ms 14456 KB Output is correct
12 Correct 495 ms 14328 KB Output is correct
13 Correct 494 ms 14456 KB Output is correct
14 Correct 494 ms 14456 KB Output is correct
15 Correct 499 ms 14328 KB Output is correct
16 Correct 99 ms 14328 KB Output is correct
17 Correct 107 ms 14428 KB Output is correct
18 Correct 102 ms 14460 KB Output is correct
19 Correct 105 ms 14332 KB Output is correct
20 Correct 103 ms 14328 KB Output is correct
21 Correct 603 ms 14356 KB Output is correct
22 Correct 606 ms 14456 KB Output is correct
23 Correct 583 ms 14456 KB Output is correct
24 Correct 589 ms 14348 KB Output is correct
25 Correct 590 ms 14456 KB Output is correct
26 Correct 663 ms 14476 KB Output is correct
27 Correct 683 ms 14352 KB Output is correct
28 Correct 663 ms 14356 KB Output is correct
29 Correct 664 ms 14388 KB Output is correct
30 Correct 492 ms 14456 KB Output is correct
31 Correct 493 ms 14480 KB Output is correct
32 Correct 499 ms 14328 KB Output is correct
33 Correct 495 ms 14456 KB Output is correct
34 Correct 493 ms 14328 KB Output is correct
35 Correct 497 ms 14384 KB Output is correct
36 Correct 492 ms 14456 KB Output is correct
37 Correct 493 ms 14356 KB Output is correct
38 Correct 492 ms 14400 KB Output is correct
39 Correct 20 ms 14328 KB Output is correct
40 Correct 19 ms 14332 KB Output is correct
41 Correct 20 ms 14328 KB Output is correct
42 Correct 20 ms 14328 KB Output is correct
43 Correct 19 ms 14328 KB Output is correct
44 Correct 21 ms 14300 KB Output is correct
45 Correct 20 ms 14328 KB Output is correct
46 Correct 21 ms 14328 KB Output is correct
47 Correct 20 ms 14328 KB Output is correct
48 Correct 21 ms 14456 KB Output is correct
49 Correct 19 ms 14328 KB Output is correct
50 Correct 19 ms 14328 KB Output is correct
51 Correct 20 ms 14328 KB Output is correct
52 Correct 20 ms 14328 KB Output is correct
53 Correct 19 ms 14328 KB Output is correct
54 Correct 17 ms 14328 KB Output is correct
55 Correct 17 ms 14328 KB Output is correct
56 Correct 17 ms 14328 KB Output is correct
57 Correct 16 ms 14328 KB Output is correct
58 Correct 17 ms 14532 KB Output is correct
59 Correct 720 ms 14328 KB Output is correct
60 Correct 710 ms 14328 KB Output is correct
61 Correct 697 ms 14456 KB Output is correct
62 Correct 717 ms 14456 KB Output is correct
63 Correct 717 ms 14400 KB Output is correct
64 Correct 833 ms 14328 KB Output is correct
65 Correct 834 ms 14328 KB Output is correct
66 Correct 834 ms 14328 KB Output is correct
67 Correct 834 ms 14456 KB Output is correct
68 Correct 832 ms 14332 KB Output is correct
69 Correct 687 ms 14332 KB Output is correct
70 Correct 682 ms 14456 KB Output is correct
71 Correct 682 ms 14328 KB Output is correct
72 Correct 691 ms 14364 KB Output is correct
73 Correct 691 ms 14360 KB Output is correct
74 Correct 143 ms 14328 KB Output is correct
75 Correct 139 ms 14364 KB Output is correct
76 Correct 139 ms 14368 KB Output is correct
77 Correct 138 ms 14364 KB Output is correct
78 Correct 137 ms 14304 KB Output is correct