QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#740754 | #9631. Median Replacement | wsyear | WA | 1ms | 3636kb | C++20 | 4.5kb | 2024-11-13 11:21:21 | 2024-11-13 11:21:21 |
Judging History
answer
// Author: Klay Thompson
// Problem: M. Median Replacement
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }
using namespace std;
template <int P>
class mod_int {
using Z = mod_int;
private:
static int mo(int x) { return x < 0 ? x + P : x; }
public:
int x;
int val() const { return x; }
mod_int() : x(0) {}
template <class T>
mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
bool operator==(const Z &rhs) const { return x == rhs.x; }
bool operator!=(const Z &rhs) const { return x != rhs.x; }
Z operator-() const { return Z(x ? P - x : 0); }
Z pow(long long k) const {
Z res = 1, t = *this;
while (k) {
if (k & 1) res *= t;
if (k >>= 1) t *= t;
}
return res;
}
Z &operator++() {
x < P - 1 ? ++x : x = 0;
return *this;
}
Z &operator--() {
x ? --x : x = P - 1;
return *this;
}
Z operator++(int) {
Z ret = x;
x < P - 1 ? ++x : x = 0;
return ret;
}
Z operator--(int) {
Z ret = x;
x ? --x : x = P - 1;
return ret;
}
Z inv() const { return pow(P - 2); }
Z &operator+=(const Z &rhs) {
(x += rhs.x) >= P && (x -= P);
return *this;
}
Z &operator-=(const Z &rhs) {
(x -= rhs.x) < 0 && (x += P);
return *this;
}
Z &operator*=(const Z &rhs) {
x = 1ULL * x * rhs.x % P;
return *this;
}
Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o) \
friend T operator o(const Z &lhs, const Z &rhs) {\
Z res = lhs; \
return res o## = rhs; \
}
setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
};
const int P = 1e9 + 7;
using Z = mod_int<P>;
using poly = vector<Z>;
poly operator+(poly f, poly g) {
int n = SZ(f) - 1, m = SZ(g) - 1, k = max(n, m);
poly h(k + 1, 0);
rep (i, 0, n) h[i] += f[i];
rep (i, 0, m) h[i] += g[i];
return h;
}
poly operator*(poly f, poly g) {
int n = SZ(f) - 1, m = SZ(g) - 1, k = n + m;
poly h(k + 1, 0);
rep (i, 0, n) rep (j, 0, m) h[i + j] += f[i] * g[j];
return h;
}
const int maxn = 310;
int n, l[maxn], r[maxn], c[maxn], tot;
poly a[maxn][2], f[maxn][2][4];
inline int o(int x) { return __builtin_popcount(x) >= 2; }
Z calc(poly f, Z x) {
Z res = 0;
per (i, SZ(f) - 1, 0) res = res * x + f[i];
return res;
}
namespace lag {
int n;
Z x[maxn], y[maxn];
Z solve(int k) {
Z ans = 0;
rep (i, 1, n) {
Z s1 = y[i], s2 = 1;
rep (j, 1, n) if (i != j) {
s1 = s1 * (k - x[j]);
s2 = s2 * (x[i] - x[j]);
}
ans += s1 * s2.inv();
}
return ans;
}
}
void work() {
cin >> n, tot = 0;
rep (i, 1, n) cin >> l[i];
rep (i, 1, n) cin >> r[i];
rep (i, 1, n) c[++tot] = l[i], c[++tot] = r[i] + 1;
sort(c + 1, c + tot + 1), tot = unique(c + 1, c + tot + 1) - c - 1;
rep (i, 1, n) l[i] = lower_bound(c + 1, c + tot + 1, l[i]) - c;
rep (i, 1, n) r[i] = lower_bound(c + 1, c + tot + 1, r[i] + 1) - c;
Z ans = 0;
rep (d, 1, tot - 1) {
rep (i, 1, n) {
if (r[i] <= d) a[i][0] = poly({c[r[i]] - c[l[i]]}), a[i][1] = poly({0});
else if (l[i] > d) a[i][0] = poly({0}), a[i][1] = poly({c[r[i]] - c[l[i]]});
else a[i][0] = poly({-c[l[i]], 1}), a[i][1] = poly({c[r[i]], -1});
}
rep (i, 0, n) rep (j, 0, 1) rep (k, 0, 3) f[i][j][k] = poly({0});
f[0][0][0] = poly({1});
rep (i, 1, n) rep (j, 0, 1) rep (k, 0, 3) rep (p, 0, 1) {
f[i][j | o(k << 1 | p)][(k << 1 | p) & 3] = f[i][j | o(k << 1 | p)][(k << 1 | p) & 3] + f[i - 1][j][k] * a[i][p];
}
poly res({0});
rep (i, 0, 3) res = res + f[n][1][i];
lag::n = n + 2;
rep (i, 1, n + 2) lag::x[i] = i, lag::y[i] = lag::y[i - 1] + calc(res, i);
ans += lag::solve(c[d + 1] - 1) - lag::solve(c[d] - 1);
}
cout << ans.val() << '\n';
}
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
int t; cin >> t;
while (t--) work();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3636kb
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'