# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1085999 |
2024-09-09T09:23:10 Z |
quanlt206 |
Boat (APIO16_boat) |
C++17 |
|
2 ms |
348 KB |
#include<bits/stdc++.h>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 2e3 + 7;
const int mod = 1e9 + 7;
int n, m, a[N], b[N];
vector<int> E;
pii block[N];
int dp[2][2000][2000][2];
void add(int& x, int y) {
if ((x+=y) >= mod) x-=mod;
}
int Ckn[N][N];
long long fact[N], inv_fact[N];
long long mul(long long a, long long b) {
if (b == 0) return 1;
ll tmp = mul(a, b / 2);
tmp = tmp * tmp % mod;
if (b & 1) tmp = tmp * a % mod;
return tmp;
}
void buildCkn() {
fact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % mod;
inv_fact[n] = mul(fact[n], mod - 2);
for (int i = n - 1; i > 0; i--) inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod;
for (int i = 1; i <= m; i++) {
int sz = block[i].Y - block[i].X + 1;
ll tich = 1;
for (int j = sz; j > 0; j--) {
tich = tich * j % mod;
Ckn[i][sz - j + 1] = tich * inv_fact[sz - j + 1] % mod;
// cout<<sz<<" "<<sz - j + 1<<" "<< Ckn[i][sz - j + 1]<<"\n";
if (sz - j + 1 > n) break;
}
}
}
int cnt[N];
int main() {
#ifndef ONLINE_JUDGE
freopen("code.inp", "r", stdin);
freopen("code.out", "w", stdout);
#endif // ONLINE_JUDGE
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
set<int> s;
for (int i = 1; i <= n; i++) {
cin>>a[i]>>b[i];
s.insert(a[i]);
s.insert(b[i]);
}
vector<int> E(s.begin(), s.end());
for (int i = 0; i < E.size(); i++) {
block[++m] = {E[i], E[i]};
if (i + 1 < E.size() && E[i] + 1 <= E[i + 1] - 1) block[++m] = {E[i] + 1, E[i + 1] - 1};
}
buildCkn();
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= m; j++)
if (a[i] <= block[j].X && block[j].Y <= b[i]) cnt[j]++;
}
cnt[0] = n;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++)
if (a[i] <= block[j].X && block[j].Y <= b[i]) cnt[j]--;
for (int j = 0; j <= m; j++)
for (int k = 0; k <= cnt[j]; k++)
for (int z = 0; z <= 1; z++) {
int& res = dp[i & 1][j][k][z];
res = 0;
if (i == 0) {
res = Ckn[j][k];
continue;
}
if (a[i] <= block[j].X && block[j].Y <= b[i]) add(res, dp[i & 1 ^ 1][j][k + 1][0]);
if (z == 0) add(res, dp[i & 1 ^ 1][j][k][0]);
if (j > 0 && k > 0) add(res, 1LL * dp[i & 1][j - 1][0][1] * Ckn[j][k] % mod); else
if (j > 0 && k == 0) add(res, dp[i & 1][j - 1][0][1]);
}
}
cout<<dp[n & 1][m][0][0];
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:104:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (int i = 0; i < E.size(); i++) {
| ~~^~~~~~~~~~
boat.cpp:106:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | if (i + 1 < E.size() && E[i] + 1 <= E[i + 1] - 1) block[++m] = {E[i] + 1, E[i + 1] - 1};
| ~~~~~~^~~~~~~~~~
boat.cpp:126:81: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
126 | if (a[i] <= block[j].X && block[j].Y <= b[i]) add(res, dp[i & 1 ^ 1][j][k + 1][0]);
| ~~^~~
boat.cpp:127:47: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
127 | if (z == 0) add(res, dp[i & 1 ^ 1][j][k][0]);
| ~~^~~
boat.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen("code.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
boat.cpp:92:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen("code.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |