Submission #1106260

# Submission time Handle Problem Language Result Execution time Memory
1106260 2024-10-29T16:47:18 Z _rain_ Boat (APIO16_boat) C++14
100 / 100
1261 ms 6348 KB
#include<bits/stdc++.h>
using namespace std;
 
typedef long long ll ;
typedef pair<int,int> ii;
#define fi first
#define se second
#define name "main"
 
const int N=(int)500;
const int MOD=(int)1e9+7;
	int add(int a,int b){
		return a+b>=MOD?a+b-MOD:a+b;
	}
	int sub(int a,int b){
		return a-b<0?a-b+MOD:a-b;
	}
	int mul(int a,int b){
		return (ll)a*b%MOD;
	}
	int power(int a,int b){
		int res=1;
		for(;b;b>>=1,a=mul(a,a))
			if (b&1) res=mul(res,a);
		return res;
	}
vector<int>nen;
vector<ii>line;
int a[N+2],b[N+2],n;
int L[N+2]={},R[N+2]={},f[N*3+2][N+2]={},dp[N+2][N*3+2]={};
int sing[N+2]={},fact[N+2]={},inv[N+2]={},w[N+2]={};
int Combine(int x,int y){
	if (x>y) return 0;
	return (ll)fact[y]*inv[y-x]%MOD*inv[x]%MOD;
}

int C(int k,int n){
	if (k>n) return 0;
	int res=1;
	for(int i=n-k+1;i<=n;++i) res=mul(res,i);
	return mul(res,inv[k]);
}
bool intersec(ii x,int a,int b){
	if (b<x.fi||a>x.se) return false;
	return true;
}
 
void build(){
	memset(L,0x3f,sizeof L);
	memset(R,-0x3f,sizeof R);
	fact[0]=1;
	for(int i=1;i<=N;++i) fact[i]=mul(fact[i-1],i);
	inv[N]=power(fact[N],MOD-2);
	for(int i=N;i>=1;--i) inv[i-1]=mul(inv[i],i);
	for(int id=1;id<=line.size();++id){
		int dodai=line[id-1].se-line[id-1].fi+1;
		for(int j=0;j<=n;++j) w[j]=0;
		w[1]=dodai;
		int tich=dodai;
		for(int j=2;j<=min(dodai,n);++j) {
			tich=mul(tich,dodai-j+1);
			w[j]=mul(tich,inv[j]);
		}
		for(int j=1;j<=n;++j){
			for(int c=1;c<=min(j,dodai);++c)
				f[id][j]=add(f[id][j],mul(Combine(c-1,j-1),w[c]));
		}
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<=line.size();++j){
			if (intersec(line[j-1],a[i],b[i])) 
				L[i]=min(L[i],j),R[i]=max(R[i],j);
		}
	}
	return;
}
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin>>n;
	for(int i=1;i<=n;++i){
		cin>>a[i]>>b[i];
		nen.push_back(a[i]);
		nen.push_back(b[i]);
		nen.push_back(a[i]-1);
	}
	sort(nen.begin(),nen.end());
	nen.resize(unique(nen.begin(),nen.end())-nen.begin());
	for(int i=1;i<nen.size();++i){
		if (i-1==0) line.push_back({nen[i-1],nen[i]});
		else line.push_back({nen[i-1]+1,nen[i]});
	}
	// cout<<nen.size()<<'\n';
	build();
	for(int i=0;i<=line.size();++i) dp[0][i]=1;
	for(int i=1;i<=n;++i){
		// cout<<i<<'\n';
		for(int id=L[i];id<=R[i];++id){
			int cnt=0;
			pair<int,int> xx=line[id-1];
			if (id==L[i]){
				int dodai=line[id-1].se-a[i]+1;
				for(int j=0;j<=i;++j) sing[j]=0;
				for(int j=1;j<=i;++j) w[j]=C(j,dodai);
				for(int j=1;j<=i;++j){
					for(int c=1;c<=j;++c)
						sing[j]=add(sing[j],mul(Combine(c-1,j-1),w[c]));
				}
				xx={a[i],line[id-1].se};
			}
			for(int j=i;j>=1;--j){
				if (intersec(xx,a[j],b[j])) ++cnt;
				if (id==L[i]) dp[i][id]=add(dp[i][id],mul(sing[cnt],dp[j-1][id-1]));
				else dp[i][id]=add(dp[i][id],mul(f[id][cnt],dp[j-1][id-1]));
			}
		}
		for(int j=L[i];j<=line.size();++j) dp[i][j]=add(dp[i][j],dp[i][j-1]);
	}
	int ans=0;
	for(int i=1;i<=n;++i) ans=add(ans,dp[i][line.size()]);
	cout<<ans;
	exit(0);
}

Compilation message

boat.cpp: In function 'void build()':
boat.cpp:55:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |  for(int id=1;id<=line.size();++id){
      |               ~~^~~~~~~~~~~~~
boat.cpp:70:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |   for(int j=1;j<=line.size();++j){
      |               ~^~~~~~~~~~~~~
boat.cpp: In function 'int32_t main()':
boat.cpp:91:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |  for(int i=1;i<nen.size();++i){
      |              ~^~~~~~~~~~~
boat.cpp:97:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |  for(int i=0;i<=line.size();++i) dp[0][i]=1;
      |              ~^~~~~~~~~~~~~
boat.cpp:119:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |   for(int j=L[i];j<=line.size();++j) dp[i][j]=add(dp[i][j],dp[i][j-1]);
      |                  ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 523 ms 4936 KB Output is correct
2 Correct 523 ms 5044 KB Output is correct
3 Correct 517 ms 4972 KB Output is correct
4 Correct 526 ms 4936 KB Output is correct
5 Correct 517 ms 4936 KB Output is correct
6 Correct 520 ms 5140 KB Output is correct
7 Correct 523 ms 4936 KB Output is correct
8 Correct 523 ms 4952 KB Output is correct
9 Correct 522 ms 5140 KB Output is correct
10 Correct 525 ms 4936 KB Output is correct
11 Correct 527 ms 4936 KB Output is correct
12 Correct 524 ms 4936 KB Output is correct
13 Correct 523 ms 4964 KB Output is correct
14 Correct 532 ms 4936 KB Output is correct
15 Correct 527 ms 4940 KB Output is correct
16 Correct 195 ms 2884 KB Output is correct
17 Correct 193 ms 2932 KB Output is correct
18 Correct 197 ms 2912 KB Output is correct
19 Correct 193 ms 3068 KB Output is correct
20 Correct 194 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 523 ms 4936 KB Output is correct
2 Correct 523 ms 5044 KB Output is correct
3 Correct 517 ms 4972 KB Output is correct
4 Correct 526 ms 4936 KB Output is correct
5 Correct 517 ms 4936 KB Output is correct
6 Correct 520 ms 5140 KB Output is correct
7 Correct 523 ms 4936 KB Output is correct
8 Correct 523 ms 4952 KB Output is correct
9 Correct 522 ms 5140 KB Output is correct
10 Correct 525 ms 4936 KB Output is correct
11 Correct 527 ms 4936 KB Output is correct
12 Correct 524 ms 4936 KB Output is correct
13 Correct 523 ms 4964 KB Output is correct
14 Correct 532 ms 4936 KB Output is correct
15 Correct 527 ms 4940 KB Output is correct
16 Correct 195 ms 2884 KB Output is correct
17 Correct 193 ms 2932 KB Output is correct
18 Correct 197 ms 2912 KB Output is correct
19 Correct 193 ms 3068 KB Output is correct
20 Correct 194 ms 3064 KB Output is correct
21 Correct 368 ms 5960 KB Output is correct
22 Correct 344 ms 5844 KB Output is correct
23 Correct 339 ms 5712 KB Output is correct
24 Correct 346 ms 5960 KB Output is correct
25 Correct 330 ms 5704 KB Output is correct
26 Correct 384 ms 5640 KB Output is correct
27 Correct 387 ms 5732 KB Output is correct
28 Correct 395 ms 5704 KB Output is correct
29 Correct 413 ms 5764 KB Output is correct
30 Correct 503 ms 5960 KB Output is correct
31 Correct 506 ms 6128 KB Output is correct
32 Correct 513 ms 5960 KB Output is correct
33 Correct 516 ms 5944 KB Output is correct
34 Correct 501 ms 5964 KB Output is correct
35 Correct 515 ms 4952 KB Output is correct
36 Correct 494 ms 4936 KB Output is correct
37 Correct 506 ms 4936 KB Output is correct
38 Correct 498 ms 5064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1360 KB Output is correct
2 Correct 10 ms 1360 KB Output is correct
3 Correct 12 ms 1404 KB Output is correct
4 Correct 11 ms 1360 KB Output is correct
5 Correct 11 ms 1360 KB Output is correct
6 Correct 13 ms 1360 KB Output is correct
7 Correct 12 ms 1528 KB Output is correct
8 Correct 12 ms 1360 KB Output is correct
9 Correct 11 ms 1360 KB Output is correct
10 Correct 11 ms 1356 KB Output is correct
11 Correct 11 ms 1492 KB Output is correct
12 Correct 10 ms 1360 KB Output is correct
13 Correct 11 ms 1388 KB Output is correct
14 Correct 10 ms 1372 KB Output is correct
15 Correct 11 ms 1468 KB Output is correct
16 Correct 6 ms 1104 KB Output is correct
17 Correct 6 ms 1104 KB Output is correct
18 Correct 6 ms 1104 KB Output is correct
19 Correct 6 ms 1104 KB Output is correct
20 Correct 6 ms 1192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 523 ms 4936 KB Output is correct
2 Correct 523 ms 5044 KB Output is correct
3 Correct 517 ms 4972 KB Output is correct
4 Correct 526 ms 4936 KB Output is correct
5 Correct 517 ms 4936 KB Output is correct
6 Correct 520 ms 5140 KB Output is correct
7 Correct 523 ms 4936 KB Output is correct
8 Correct 523 ms 4952 KB Output is correct
9 Correct 522 ms 5140 KB Output is correct
10 Correct 525 ms 4936 KB Output is correct
11 Correct 527 ms 4936 KB Output is correct
12 Correct 524 ms 4936 KB Output is correct
13 Correct 523 ms 4964 KB Output is correct
14 Correct 532 ms 4936 KB Output is correct
15 Correct 527 ms 4940 KB Output is correct
16 Correct 195 ms 2884 KB Output is correct
17 Correct 193 ms 2932 KB Output is correct
18 Correct 197 ms 2912 KB Output is correct
19 Correct 193 ms 3068 KB Output is correct
20 Correct 194 ms 3064 KB Output is correct
21 Correct 368 ms 5960 KB Output is correct
22 Correct 344 ms 5844 KB Output is correct
23 Correct 339 ms 5712 KB Output is correct
24 Correct 346 ms 5960 KB Output is correct
25 Correct 330 ms 5704 KB Output is correct
26 Correct 384 ms 5640 KB Output is correct
27 Correct 387 ms 5732 KB Output is correct
28 Correct 395 ms 5704 KB Output is correct
29 Correct 413 ms 5764 KB Output is correct
30 Correct 503 ms 5960 KB Output is correct
31 Correct 506 ms 6128 KB Output is correct
32 Correct 513 ms 5960 KB Output is correct
33 Correct 516 ms 5944 KB Output is correct
34 Correct 501 ms 5964 KB Output is correct
35 Correct 515 ms 4952 KB Output is correct
36 Correct 494 ms 4936 KB Output is correct
37 Correct 506 ms 4936 KB Output is correct
38 Correct 498 ms 5064 KB Output is correct
39 Correct 11 ms 1360 KB Output is correct
40 Correct 10 ms 1360 KB Output is correct
41 Correct 12 ms 1404 KB Output is correct
42 Correct 11 ms 1360 KB Output is correct
43 Correct 11 ms 1360 KB Output is correct
44 Correct 13 ms 1360 KB Output is correct
45 Correct 12 ms 1528 KB Output is correct
46 Correct 12 ms 1360 KB Output is correct
47 Correct 11 ms 1360 KB Output is correct
48 Correct 11 ms 1356 KB Output is correct
49 Correct 11 ms 1492 KB Output is correct
50 Correct 10 ms 1360 KB Output is correct
51 Correct 11 ms 1388 KB Output is correct
52 Correct 10 ms 1372 KB Output is correct
53 Correct 11 ms 1468 KB Output is correct
54 Correct 6 ms 1104 KB Output is correct
55 Correct 6 ms 1104 KB Output is correct
56 Correct 6 ms 1104 KB Output is correct
57 Correct 6 ms 1104 KB Output is correct
58 Correct 6 ms 1192 KB Output is correct
59 Correct 1153 ms 6216 KB Output is correct
60 Correct 1145 ms 6204 KB Output is correct
61 Correct 1139 ms 6296 KB Output is correct
62 Correct 1151 ms 6216 KB Output is correct
63 Correct 1146 ms 6316 KB Output is correct
64 Correct 1229 ms 6272 KB Output is correct
65 Correct 1229 ms 6216 KB Output is correct
66 Correct 1229 ms 6216 KB Output is correct
67 Correct 1230 ms 6216 KB Output is correct
68 Correct 1261 ms 6248 KB Output is correct
69 Correct 1087 ms 6348 KB Output is correct
70 Correct 1102 ms 6292 KB Output is correct
71 Correct 1100 ms 6216 KB Output is correct
72 Correct 1085 ms 6184 KB Output is correct
73 Correct 1108 ms 6332 KB Output is correct
74 Correct 240 ms 2924 KB Output is correct
75 Correct 226 ms 2932 KB Output is correct
76 Correct 231 ms 2892 KB Output is correct
77 Correct 231 ms 3056 KB Output is correct
78 Correct 239 ms 3144 KB Output is correct