Submission #1314224

#TimeUsernameProblemLanguageResultExecution timeMemory
1314224thuhienneBoat (APIO16_boat)C++20
100 / 100
476 ms2448 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define thuhien ""
#define re exit(0);

const int mod = 1e9 + 7;
const int maxn = 509;

int pw(int a,int b) {
	if (b == 0) return 1;
	if (b % 2 == 0) {
		int x = pw(a,b / 2);
		return 1ll * x * x % mod;
	} else {
		int x = pw(a,b - 1);
		return 1ll * x * a % mod;
	}
}
int power[maxn];

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

pair <int,int> seg[2 * maxn];

vector <int> X;

//int dp[maxn][2 * maxn][maxn],pref[maxn][2 * maxn];
//dp i j k:xet den vi tri i, nhom to nhat la nhom j va trong nhom nay dang co k phan tu

int dp[2 * maxn][maxn],pref[2 * maxn],newpref[2 * maxn];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
	if (fopen(thuhien".inp","r")) {
	   freopen(thuhien".inp","r",stdin);
	   freopen(thuhien".out","w",stdout);
	}
	
	cin >> n;
	for (int i = 1;i <= n;i++) {
		cin >> a[i] >> b[i];
		
		X.push_back(a[i] - 1);
		X.push_back(b[i]);
	}
	
	sort(X.begin(),X.end());
	X.resize(unique(X.begin(),X.end()) - X.begin());
	
	for (int i = 0;i < (int)X.size() - 1;i++) {
		seg[i + 1] = {X[i],X[i + 1]};
	}
	
//	for (int i = 1;i < 2 * n;i++) cout << seg[i].first << " " << seg[i].second << '\n';
	
	//prebuild power
	
	for (int i = 1;i <= n;i++) power[i] = pw(i,mod - 2);
	
	//dynamic programming
//	cout << "POS GROUP NUM VALUE\n";
	for (int i = 1;i <= n;i++) {
		
		for (int j = 1;j < 2 * n;j++) {
			newpref[j] = newpref[j - 1];
			int len = seg[j].second - seg[j].first;
			
			for (int k = min(i,len);k >= 1;k--) {
				
				//khong chon
//				dp[i][j][k] = dp[i - 1][j][k];
				
				//chon
				if (a[i] <= seg[j].first + 1 && seg[j].second <= b[i]) {
					
					//thang truoc do la thang duoi hoac khong thang nao
					if (k == 1) {
						dp[j][k] = (dp[j][k] + 1ll * (pref[j - 1] + 1) * len) % mod;
					}
					
					//thang truoc do la thang cung khoi
					dp[j][k] = (dp[j][k] + 1ll * dp[j][k - 1] * (len - k + 1) % mod * power[k]) % mod;
					
				}
				
				newpref[j] = (newpref[j] + dp[j][k]) % mod;
				
			}
			
		}
		
		for (int j = 1;j < 2 * n;j++) pref[j] = newpref[j];
		
	}
	
	cout << pref[2 * n - 1];
	

}

Compilation message (stderr)

boat.cpp: In function 'int main()':
boat.cpp:39:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |            freopen(thuhien".inp","r",stdin);
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
boat.cpp:40:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |            freopen(thuhien".out","w",stdout);
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...