Submission #540952

# Submission time Handle Problem Language Result Execution time Memory
540952 2022-03-22T04:59:31 Z amunduzbaev Boat (APIO16_boat) C++17
0 / 100
710 ms 4232 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array

const int mod = 1e9 + 7;

int bp(int a, int b){
	int r=1;
	while(b){
		if(b&1) r = r * 1ll * a % mod;
		a = a * 1ll * a % mod; b >>= 1;
	} return r;
}

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n; cin>>n;
	vector<int> a(2 * n), p(2 * n), og;
	iota(p.begin(), p.end(), 0);
	for(int i=0;i<n;i++){
		cin>>a[i<<1]>>a[i<<1|1];
		a[i<<1|1]++;
	}
	
	sort(p.begin(), p.end(), [&](int i, int j){
		return (a[i] < a[j]);
	});
	int last=0;
	for(int i=0, j=0;i<n+n;last++){
		while(j<n+n && a[p[j]] == a[p[i]]) j++;
		og.push_back(a[p[i]]);
		while(i < j) a[p[i]] = last, i++;
	}
	
	vector<int> f(n + 1), inv(n + 1);
	f[0] = inv[0] = 1;
	for(int i=1;i<=n;i++){
		f[i] = f[i-1] * 1ll * i % mod;
		inv[i] = bp(f[i], mod - 2);
	}
	
	auto C = [&](int n, int k){
		if(n < k) return 0ll;
		return f[n] * 1ll * inv[k] % mod * inv[n - k] % mod;
	};

/*

5
1 5
1 5
1 5
1 5
1 5

*/

	vector<vector<int>> cnt(n + 1, vector<int>(last));
	for(int c=1;c<=n;c++){
		for(int k=0;k+1<last;k++){
			int x = og[k+1] - og[k], f = x;
			for(int j=0;j<c && j<x;j++){
				cnt[c][k] = (cnt[c][k] + C(c - 1, j) * 1ll * f % mod * inv[j + 1]) % mod;
				f = f * 1ll * (x - j - 1) % mod; 
			}
		}
	}
	//~ cout<<cnt[1][1]<<endl;
	//~ cout<<cnt[2][1]<<endl;
	int res = 0;
	vector<vector<int>> dp(n, vector<int>(last));
	for(int i=0;i<n;i++){
		int l = a[i<<1], r = a[i<<1|1];
		for(int k=l;k<r;k++){
			//~ assert(k+1 < last);
			int c=1;
			for(int j=i-1;~j;j--){
				dp[i][k] = (dp[i][k] + (k ? dp[j][k-1] : 0) * 1ll * cnt[c][k]) % mod;
				if(a[j<<1] <= k && k < a[j<<1|1]) c++;
			}
			
			dp[i][k] = (dp[i][k] + cnt[c][k]) % mod;
			if(k) dp[i][k] = (dp[i][k] + dp[i][k-1]) % mod;
		}
		res = (res + dp[i][r-1]) % mod;
		//~ for(int k=0;k<last;k++) cout<<dp[i][k]<<" ";
		//~ cout<<"\n";
	}
	
	cout<<res<<"\n";
}
# Verdict Execution time Memory Grader output
1 Incorrect 710 ms 4232 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 710 ms 4232 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 710 ms 4232 KB Output isn't correct
2 Halted 0 ms 0 KB -