QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#738163 | #9631. Median Replacement | AKALemon | WA | 0ms | 3664kb | C++23 | 3.1kb | 2024-11-12 18:00:05 | 2024-11-12 18:00:06 |
Judging History
answer
#include<bits/stdc++.h>
using i64 = long long;
constexpr int P = 1e9 + 7;
i64 qpow(i64 a, i64 b) {
i64 res = 1;
while (b) {
if (b & 1) res = res * a % P;
b >>= 1;
a = a * a % P;
}
return res;
}
//x范围[1, n], 求f(n)
template<class T>
T lagrange_interpolation(const std::vector<T> &y, const T k) {
const int n = y.size();
if (k <= n) return y[k - 1];
std::vector<T> pre(n + 1), suf(n + 2), fac(n + 1), inv(n + 1);
pre[0] = suf[n + 1] = fac[0] = inv[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = fac[i - 1] * i % P;
pre[i] = pre[i - 1] * (k - i) % P;
}
inv[n] = qpow(fac[n], P - 2);
for (int i = n; i >= 1; i--) {
inv[i - 1] = inv[i] * i % P;
suf[i] = suf[i + 1] * (k - i) % P;
}
T ans = 0;
for (int i = 1; i <= n; i++) {
T p = pre[i - 1] * suf[i + 1] % P;
T Q = inv[i - 1] * inv[n - i] % P;
T mul = ((n - i) & 1) ? -1 : 1;
ans = (ans + (Q * mul + P) % P * p % P * y[i - 1] % P) % P;
}
return ans;
}
void solve() {
int n; std::cin >> n;
std::vector<int> l(n), r(n);
i64 sum = 1;
for (int i = 0; i < n; i++) std::cin >> l[i];
for (int i = 0; i < n; i++) std::cin >> r[i];
for (int i = 0; i < n; i++) sum = sum * (r[i] - l[i] + 1) % P;
std::vector<int> x = l;
x.insert(x.end(), r.begin(), r.end());
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
i64 ans = 0;
for (int i = 0; i < x.size(); i++) {
auto getc = [&](int x) -> i64 {
std::array<i64, 3> dp;
dp[0] = 1;
for (int i = 0; i < n; i++) {
std::array<i64, 3> ndp;
{
int val = std::max<int>(0, std::min(x - 1, r[i]) - l[i] + 1);
ndp[0] = (dp[0] + dp[2]) % P * val % P;
ndp[2] = dp[1] * val % P;
}
{
int val = std::max<int>(0, r[i] - std::max(x, l[i]) + 1);
ndp[1] = dp[0] * val % P;
}
swap(dp, ndp);
}
return (sum - (dp[0] + dp[1] + dp[2]) % P + P) % P;
};
auto calc = [&](int L, int R) -> i64 {
if (R - L + 1 >= n + 2) {
std::vector<i64> c(n + 3);
for (int i = 1; i <= n + 2; i++) {
c[i] = (c[i - 1] + getc(L + i - 1)) % P;
}
return lagrange_interpolation<i64>(c, R - L + 1);
}
i64 res = 0;
for (int i = L; i <= R; i++) {
res = (res + getc(i));
}
return res;
};
if (i + 1 == x.size()) {
(ans += calc(x[i], x[i])) %= P;
} else {
(ans += calc(x[i], x[i + 1] - 1)) %= P;
}
}
std::cout << ans << "\n";
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr), std::cout.tie(nullptr);
int t; std::cin >> t;
while (t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3664kb
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:
200 170 650 293873430 999999781 113 120 999989951 911647943 103
result:
wrong answer 1st lines differ - expected: '180', found: '200'