Submission #579367

# Submission time Handle Problem Language Result Execution time Memory
579367 2022-06-19T03:12:29 Z jiahng Boat (APIO16_boat) C++14
100 / 100
1875 ms 24688 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define int ll
typedef pair<int32_t, int32_t> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 510
#define INF (ll)1e9+10
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
int N,A[maxn],B[maxn];
int fac[maxn], invfac[maxn];
int qexp(int a,int b){
    int ans = 1;
    while (b){
        if (b&1) (ans *= a) %= MOD;
        (a *= a) %= MOD;
        b >>= 1;
    }
    return ans;
}
int nck(int n,int k){
    if (k > n) return 0;
    return (((fac[n] * invfac[k]) % MOD) * invfac[n-k]) % MOD;
}
int suffmult[4*maxn][maxn];
int dp[maxn][4*maxn];
int S[4*maxn],E[4*maxn];
int specnck(int x,int k){
	//~ cout << E[x]-S[x]+1 << ' ' << k << ' ' << (invfac[k] * suffmult[x][k]) % MOD << '\n';
	return (invfac[k] * suffmult[x][k]) % MOD;
}

int memo[maxn][4*maxn];
int f(int x,int y){
	if (memo[x][y] != -1) return memo[x][y];
	int ans = 0;
	FOR(z,1,x){
		(ans += (nck(x-1,z-1) * specnck(y,z)) % MOD) %= MOD;
	}
	return memo[x][y] = ans;
}
int32_t main(){
    cin >> N;
    FOR(i,1,N) cin >> A[i] >> B[i];
    fac[0] = 1;
    FOR(i,1,N) fac[i] = (fac[i-1] * i) % MOD;
    invfac[N] = qexp(fac[N], MOD-2);
    DEC(i,N-1,0) invfac[i] = (invfac[i+1] * (i+1)) % MOD;
    vi v;
    FOR(i,1,N){
		v.pb(A[i]); v.pb(B[i]);
	}
	v.pb(0); v.pb(INF);
    sort(all(v)); v.erase(unique(all(v)),v.end());
	
	int M = 1;
	FOR(i,0,(int)v.size()-1){
		S[M] = v[i]; E[M] = v[i];
		M++;
        if (i + 1 < (int)v.size() && v[i] + 1 < v[i+1]){
			S[M] = v[i]+1; E[M] = v[i+1]-1; M++;
		}
    }
	M--;
    
    FOR(i,1,M){
		suffmult[i][0] = 1;
		int x = E[i] - S[i] + 1;
		FOR(j,1,N){
			suffmult[i][j] = (suffmult[i][j-1] * x) % MOD;
			x--;
		}
	}
    FOR(i,1,M+1) dp[N+1][i] = 1;
    mem(memo, -1);
    DEC(i,N,1){
		DEC(j,M,1){
			if (B[i] < S[j]){
				dp[i][j] = 0; continue;
			}
			dp[i][j] = dp[i][j+1];
			
			if (A[i] <= S[j] && E[j] <= B[i]){
				int num = 1;
				FOR(k,i+1,N){
					(dp[i][j] += (dp[k][j+1] * f(num, j)) % MOD) %= MOD;
					if (A[k] <= S[j] && E[j] <= B[k]) num++;
				}
				(dp[i][j] += f(num, j)) %= MOD; // if no more > j segments
			}
				
		}
    }
	int ans = 0;
	FOR(i,1,N){
		(ans += dp[i][1]) %= MOD;
	}
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 18352 KB Output is correct
2 Correct 13 ms 18324 KB Output is correct
3 Correct 13 ms 18436 KB Output is correct
4 Correct 14 ms 18360 KB Output is correct
5 Correct 16 ms 18364 KB Output is correct
6 Correct 15 ms 18436 KB Output is correct
7 Correct 15 ms 18356 KB Output is correct
8 Correct 13 ms 18388 KB Output is correct
9 Correct 13 ms 18364 KB Output is correct
10 Correct 13 ms 18380 KB Output is correct
11 Correct 13 ms 18428 KB Output is correct
12 Correct 13 ms 18332 KB Output is correct
13 Correct 13 ms 18388 KB Output is correct
14 Correct 12 ms 18384 KB Output is correct
15 Correct 13 ms 18388 KB Output is correct
16 Correct 7 ms 11860 KB Output is correct
17 Correct 8 ms 11988 KB Output is correct
18 Correct 8 ms 11860 KB Output is correct
19 Correct 9 ms 11988 KB Output is correct
20 Correct 7 ms 11932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 18352 KB Output is correct
2 Correct 13 ms 18324 KB Output is correct
3 Correct 13 ms 18436 KB Output is correct
4 Correct 14 ms 18360 KB Output is correct
5 Correct 16 ms 18364 KB Output is correct
6 Correct 15 ms 18436 KB Output is correct
7 Correct 15 ms 18356 KB Output is correct
8 Correct 13 ms 18388 KB Output is correct
9 Correct 13 ms 18364 KB Output is correct
10 Correct 13 ms 18380 KB Output is correct
11 Correct 13 ms 18428 KB Output is correct
12 Correct 13 ms 18332 KB Output is correct
13 Correct 13 ms 18388 KB Output is correct
14 Correct 12 ms 18384 KB Output is correct
15 Correct 13 ms 18388 KB Output is correct
16 Correct 7 ms 11860 KB Output is correct
17 Correct 8 ms 11988 KB Output is correct
18 Correct 8 ms 11860 KB Output is correct
19 Correct 9 ms 11988 KB Output is correct
20 Correct 7 ms 11932 KB Output is correct
21 Correct 833 ms 23176 KB Output is correct
22 Correct 824 ms 23200 KB Output is correct
23 Correct 755 ms 23116 KB Output is correct
24 Correct 811 ms 23240 KB Output is correct
25 Correct 877 ms 23052 KB Output is correct
26 Correct 1484 ms 22816 KB Output is correct
27 Correct 1412 ms 22732 KB Output is correct
28 Correct 1445 ms 22704 KB Output is correct
29 Correct 1482 ms 23004 KB Output is correct
30 Correct 28 ms 23840 KB Output is correct
31 Correct 24 ms 23804 KB Output is correct
32 Correct 21 ms 23892 KB Output is correct
33 Correct 25 ms 23824 KB Output is correct
34 Correct 21 ms 23796 KB Output is correct
35 Correct 15 ms 18388 KB Output is correct
36 Correct 15 ms 18368 KB Output is correct
37 Correct 16 ms 18388 KB Output is correct
38 Correct 17 ms 18432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 10724 KB Output is correct
2 Correct 12 ms 10688 KB Output is correct
3 Correct 13 ms 10708 KB Output is correct
4 Correct 13 ms 10708 KB Output is correct
5 Correct 14 ms 10676 KB Output is correct
6 Correct 18 ms 10672 KB Output is correct
7 Correct 17 ms 10724 KB Output is correct
8 Correct 17 ms 10696 KB Output is correct
9 Correct 20 ms 10672 KB Output is correct
10 Correct 18 ms 10676 KB Output is correct
11 Correct 13 ms 10776 KB Output is correct
12 Correct 12 ms 10708 KB Output is correct
13 Correct 14 ms 10772 KB Output is correct
14 Correct 14 ms 10736 KB Output is correct
15 Correct 17 ms 10688 KB Output is correct
16 Correct 9 ms 9720 KB Output is correct
17 Correct 9 ms 9684 KB Output is correct
18 Correct 8 ms 9628 KB Output is correct
19 Correct 7 ms 9684 KB Output is correct
20 Correct 7 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 18352 KB Output is correct
2 Correct 13 ms 18324 KB Output is correct
3 Correct 13 ms 18436 KB Output is correct
4 Correct 14 ms 18360 KB Output is correct
5 Correct 16 ms 18364 KB Output is correct
6 Correct 15 ms 18436 KB Output is correct
7 Correct 15 ms 18356 KB Output is correct
8 Correct 13 ms 18388 KB Output is correct
9 Correct 13 ms 18364 KB Output is correct
10 Correct 13 ms 18380 KB Output is correct
11 Correct 13 ms 18428 KB Output is correct
12 Correct 13 ms 18332 KB Output is correct
13 Correct 13 ms 18388 KB Output is correct
14 Correct 12 ms 18384 KB Output is correct
15 Correct 13 ms 18388 KB Output is correct
16 Correct 7 ms 11860 KB Output is correct
17 Correct 8 ms 11988 KB Output is correct
18 Correct 8 ms 11860 KB Output is correct
19 Correct 9 ms 11988 KB Output is correct
20 Correct 7 ms 11932 KB Output is correct
21 Correct 833 ms 23176 KB Output is correct
22 Correct 824 ms 23200 KB Output is correct
23 Correct 755 ms 23116 KB Output is correct
24 Correct 811 ms 23240 KB Output is correct
25 Correct 877 ms 23052 KB Output is correct
26 Correct 1484 ms 22816 KB Output is correct
27 Correct 1412 ms 22732 KB Output is correct
28 Correct 1445 ms 22704 KB Output is correct
29 Correct 1482 ms 23004 KB Output is correct
30 Correct 28 ms 23840 KB Output is correct
31 Correct 24 ms 23804 KB Output is correct
32 Correct 21 ms 23892 KB Output is correct
33 Correct 25 ms 23824 KB Output is correct
34 Correct 21 ms 23796 KB Output is correct
35 Correct 15 ms 18388 KB Output is correct
36 Correct 15 ms 18368 KB Output is correct
37 Correct 16 ms 18388 KB Output is correct
38 Correct 17 ms 18432 KB Output is correct
39 Correct 14 ms 10724 KB Output is correct
40 Correct 12 ms 10688 KB Output is correct
41 Correct 13 ms 10708 KB Output is correct
42 Correct 13 ms 10708 KB Output is correct
43 Correct 14 ms 10676 KB Output is correct
44 Correct 18 ms 10672 KB Output is correct
45 Correct 17 ms 10724 KB Output is correct
46 Correct 17 ms 10696 KB Output is correct
47 Correct 20 ms 10672 KB Output is correct
48 Correct 18 ms 10676 KB Output is correct
49 Correct 13 ms 10776 KB Output is correct
50 Correct 12 ms 10708 KB Output is correct
51 Correct 14 ms 10772 KB Output is correct
52 Correct 14 ms 10736 KB Output is correct
53 Correct 17 ms 10688 KB Output is correct
54 Correct 9 ms 9720 KB Output is correct
55 Correct 9 ms 9684 KB Output is correct
56 Correct 8 ms 9628 KB Output is correct
57 Correct 7 ms 9684 KB Output is correct
58 Correct 7 ms 9684 KB Output is correct
59 Correct 1027 ms 24500 KB Output is correct
60 Correct 918 ms 24500 KB Output is correct
61 Correct 892 ms 24408 KB Output is correct
62 Correct 991 ms 24516 KB Output is correct
63 Correct 979 ms 24688 KB Output is correct
64 Correct 1859 ms 24492 KB Output is correct
65 Correct 1848 ms 24556 KB Output is correct
66 Correct 1820 ms 24420 KB Output is correct
67 Correct 1789 ms 24396 KB Output is correct
68 Correct 1875 ms 24504 KB Output is correct
69 Correct 1041 ms 24484 KB Output is correct
70 Correct 990 ms 24412 KB Output is correct
71 Correct 1012 ms 24488 KB Output is correct
72 Correct 1102 ms 24496 KB Output is correct
73 Correct 1063 ms 24464 KB Output is correct
74 Correct 106 ms 12036 KB Output is correct
75 Correct 105 ms 12076 KB Output is correct
76 Correct 111 ms 12236 KB Output is correct
77 Correct 111 ms 12072 KB Output is correct
78 Correct 127 ms 12076 KB Output is correct