QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#738315 | #9631. Median Replacement | VegJJJy | WA | 1ms | 3652kb | C++17 | 5.5kb | 2024-11-12 18:39:33 | 2024-11-12 18:39:33 |
Judging History
answer
#include<bits/stdc++.h>
#define dbg(x) cout << #x"=" << (x) << ' '
// #define endl '\n'
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
struct Poly {
vector<int> p;
Poly(int n = 0) : p(n) {}
Poly(const vector<int> &v) : p(v) {}
int size() const {
return p.size();
}
int get(int x) {
int res = 0, t = 1;
for(int i = 0; i < p.size(); i++) {
res = (res + 1ll * p[i] * t) % MOD;
t = 1ll * t * x % MOD;
}
return res;
}
};
ostream& operator<<(ostream &out, const Poly &u) {
out << "{";
for(auto t : u.p)
out << ' ' << t;
out << "}";
return out;
}
Poly operator*(const Poly &u, const Poly &v) {
Poly res(max(0, u.size() + v.size() - 1));
for(int i = 0; i < u.size(); i++)
for(int j = 0; j < v.size(); j++)
res.p[i + j] = (res.p[i + j] + 1ll * u.p[i] * v.p[j]) % MOD;
return res;
}
Poly operator+(const Poly &u, const Poly &v) {
Poly res(max(u.size(), v.size()));
for(int i = 0; i < u.size(); i++)
res.p[i] = u.p[i];
for(int i = 0; i < v.size(); i++)
res.p[i] = (res.p[i] + v.p[i]) % MOD;
return res;
}
int qpow(int a, int b) {
int res = 1;
while(b) {
if(b & 1) res = 1ll * res * a % MOD;
a = 1ll * a * a % MOD;
b >>= 1;
}
return res;
}
Poly f[155][4][2];
void solve() {
int n;
cin >> n;
vector<int> l(n + 1), r(n + 1), a;
for(int i = 1; i <= n; i++) {
cin >> l[i];
a.push_back(l[i]);
}
for(int i = 1; i <= n; i++) {
cin >> r[i];
a.push_back(r[i]);
a.push_back(r[i] + 1);
}
sort(a.begin(), a.end());
{
vector<int> a2;
for(int i = 0; i < a.size(); i++)
if(i == 0 || a[i] != a[i - 1])
a2.push_back(a[i]);
a = a2;
}
a.push_back(a.back() + 1);
vector<tuple<Poly, int, int>> g;
auto getPoly = [&](int p, int L, int R, int tp) -> Poly {
if(tp == 0) {//<x
if(l[p] >= R) {
return vector<int>{0};
} else if(r[p] < L) {
return vector<int>{r[p] - l[p] + 1};
} else if(r[p] == L) {
return vector<int>{L - l[p]};
} else {
return vector<int>{-l[p], 1};
}
} else {//>=x
if(r[p] < L) {
return vector<int>{0};
} else if(r[p] == L) {
return vector<int>{1};
} else if(l[p] >= R) {
return vector<int>{r[p] - l[p] + 1};
} else {
return vector<int>{r[p] + 1, -1};
}
}
};
for(int p = 0; p + 1 < a.size(); p++) {
int L = a[p], R = a[p + 1] - 1;
//0:00, 1:01, 2:10, 3:11
//0<, 1>=
f[1][0][0] = getPoly(1, L, R, 0);
f[1][1][0] = getPoly(1, L, R, 1);
// cout << "--- p = " << p << " "; dbg(L); dbg(R) << endl;
// dbg(f[1][0][0]) << endl;
// dbg(f[1][1][0]) << endl;
for(int i = 2; i <= n; i++) {
Poly p[2] = {getPoly(i, L, R, 0), getPoly(i, L, R, 1)};
for(int j = 0; j < 4; j++)
for(int k = 0; k < 2; k++)
f[i][j][k] = vector<int>{0};
for(int c1 = 0; c1 <= 1; c1++)
for(int c2 = 0; c2 <= 1; c2++)
for(int c3 = 0; c3 <= 1; c3++) {
int t1 = c1 * 2 + c2, t2 = c2 * 2 + c3;
if(c1 + c2 + c3 >= 2) {
f[i][t2][1] = f[i][t2][1] + (f[i - 1][t1][0] + f[i - 1][t1][1]) * p[c3];
} else {
f[i][t2][0] = f[i][t2][0] + f[i - 1][t1][0] * p[c3];
f[i][t2][1] = f[i][t2][1] + f[i - 1][t1][1] * p[c3];
}
}
// dbg(i); dbg(p[0]); dbg(p[1]) << endl;
// dbg(f[i][0][0]); dbg(f[i][0][1]) << endl;
// dbg(f[i][1][0]); dbg(f[i][1][1]) << endl;
// dbg(f[i][2][0]); dbg(f[i][2][1]) << endl;
// dbg(f[i][3][0]); dbg(f[i][3][1]) << endl;
}
g.push_back({f[n][0][1] + f[n][1][1] + f[n][2][1] + f[n][3][1], L, R});
}
int ans = 0;
for(auto [p, L, R] : g) {
// dbg(L); dbg(R); dbg(p) << endl;
if(R - L + 1 <= n + 2) {
for(int i = L; i <= R; i++)
ans = (ans + p.get(i)) % MOD;
} else {
vector<pair<int, int>> s;
for(int i = L; i <= L + n; i++) {
s.push_back({i, p.get(i)});
}
for(int i = 1; i < s.size(); i++)
s[i].second = (s[i].second + s[i - 1].second) % MOD;
for(int i = 0; i < s.size(); i++) {
int fz = 1, fm = 1;
for(int j = 0; j < s.size(); j++) {
if(j == i) continue;
fz = 1ll * fz * (R - s[j].first) % MOD;
fm = 1ll * fm * (s[i].first - s[j].first) % MOD;
}
ans = (ans + 1ll * s[i].second * fz % MOD * qpow(fm, MOD - 2)) % MOD;
}
}
}
cout << (ans % MOD + MOD) % MOD << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
cin >> T;
while(T--) {
solve();
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3652kb
input:
10 5 5 1 4 3 2 14 2 5 3 2 5 4 5 1 2 3 13 7 1 2 3 5 5 2 5 3 1 10 2 12 3 2 5 5 5 3 1 5 57 5 3 1 5 5 2 2 3 3 5 4 5 4 4 5 5 4 5 3 5 3 13 7 3 5 3 5 5 1 4 2 3 14 3 4 2 3 5 1 2 5 4 5 2 8 5 7 5 5 1 1 3 5 1 8 2 3 8 1 5 4 4 4 2 3 5 10 5 2 3
output:
180 170 650 265 134 113 120 296 192 103
result:
wrong answer 5th lines differ - expected: '182', found: '134'