QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#736960 | #9631. Median Replacement | OIer_kzc | WA | 2ms | 3848kb | C++17 | 4.3kb | 2024-11-12 14:04:06 | 2024-11-12 14:04:11 |
Judging History
answer
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <vector>
#include <random>
#include <chrono>
#include <algorithm>
#define LOG(FMT...) fprintf(stderr, FMT)
#define eb emplace_back
using namespace std;
typedef long long LL;
constexpr int N = 305;
constexpr int mod = 1e9 + 7;
constexpr int inv(int x, int k = mod - 2) {
int r = 1;
while (k) {
if (k & 1) {
r = x * (LL)r % mod;
}
x = x * (LL)x % mod;
k >>= 1;
}
return r;
}
void Add(int &x, int y) {
if ((x += y) >= mod) {
x -= mod;
}
}
void Sub(int &x, int y) {
if ((x -= y) < 0) {
x += mod;
}
}
constexpr int neg(int x) {
return x ? mod - x : 0;
}
int fact[N], infact[N];
void prefact() {
constexpr int n = N - 1;
*fact = 1;
for (int i = 1; i <= n; ++i) {
fact[i] = fact[i - 1] * (LL)i % mod;
}
infact[n] = inv(fact[n]);
for (int i = n; i; --i) {
infact[i - 1] = infact[i] * (LL)i % mod;
}
}
int n, res, prod;
struct Seg {
int l, r;
} sg[N];
int df[N], szd;
struct Poly {
vector<int> a;
Poly() {}
Poly(int _x) : a{_x} {}
Poly(int _x, int _y) : a{_x, _y} {}
Poly(const vector<int> &_a) : a(_a) {}
bool empty() const {
return a.empty();
}
int size() const {
return (int)a.size();
}
void clear() {
a.clear();
}
void eb(int x) {
a.eb(x);
}
int operator[] (int x) const {
assert(0 <= x && x < (int)a.size());
return a[x];
}
int &operator[] (int x) {
assert(0 <= x && x < (int)a.size());
return a[x];
}
void operator += (const Poly &t) {
while (a.size() < t.size()) {
a.eb(0);
}
for (int i = 0; i < t.size(); ++i) {
Add(a[i], t[i]);
}
}
Poly operator * (const Poly &t) const {
if (a.empty() || t.empty()) {
return Poly();
}
vector<int> c(a.size() + t.size() - 1, 0);
for (int i = 0; i < t.size(); ++i) {
for (int j = 0; j < a.size(); ++j) {
c[i + j] = (c[i + j] + t[i] * (LL)a[j]) % mod;
}
}
return Poly(c);
}
};
Poly f[3], g[3], F;
int w[N], y[N], pr[N], sf[N], cntP;
void reset() {
cntP = 0;
for (int i = 0; i <= n + 1; ++i) {
w[i] = 1;
}
}
int calc(int m) {
int ret = 0;
for (int i = 0; i <= m; ++i) {
Add(ret, inv(i, cntP));
}
y[0] = w[0];
for (int i = 1; i <= n + 1; ++i) {
y[i] = y[i - 1] + w[i];
if (y[i] >= mod) {
y[i] -= mod;
}
}
if (m <= n + 1) {
return y[m];
}
pr[0] = 1;
for (int i = 0; i <= n; ++i) {
pr[i + 1] = pr[i] * (LL)(m - i) % mod;
}
sf[n + 1] = 1;
for (int i = n + 1; i; --i) {
sf[i - 1] = sf[i] * (LL)(m - i) % mod;
}
int s = 0;
for (int i = 0; i <= n + 1; ++i) {
int v = y[i] * (LL)infact[i] % mod * infact[n + 1 - i] % mod * pr[i] % mod * sf[i] % mod;
(n + 1 - i & 1 ? Sub : Add)(s, v);
}
return s;
}
void mov() {
cntP += 1;
for (int i = 0; i <= n + 1; ++i) {
w[i] = w[i] * (LL)i % mod;
}
}
// L <= x < R
void solve(int L, int R) {
f[0].clear(), f[1].clear(), f[2].clear();
f[2].eb(1);
for (int i = 0; i < n; ++i) {
auto &[l, r] = sg[i];
if (r <= L) {
g[1] += f[0] * (r - l);
g[2] += f[1] * (r - l);
g[2] += f[2] * (r - l);
} else if (l <= L) {
g[1] += f[0] * Poly(mod - l, 1);
g[2] += f[1] * Poly(mod - l, 1);
g[2] += f[2] * Poly(mod - l, 1);
}
if (R <= l) {
g[0] += f[2] * (r - l);
} else if (R <= r) {
g[0] += f[2] * Poly(r, mod - 1);
}
for (int j = 0; j < 3; ++j) {
f[j] = g[j];
g[j].clear();
}
}
F.clear();
F += f[0];
F += f[1];
F += f[2];
for (int i = 0; i < F.size(); ++i) {
F[i] = neg(F[i]);
}
if (F.empty()) {
F.eb(0);
}
Add(F[0], prod);
reset();
for (int i = 0; i < F.size(); ++i) {
res = (res + (calc(R - 1) - calc(L - 1) + mod) * (LL)F[i]) % mod;
mov();
}
}
int main() {
prefact();
int task;
for (scanf("%d", &task); task--; ) {
scanf("%d", &n);
res = 0, prod = 1;
for (int i = 0; i < n; ++i) {
auto &[l, r] = sg[i];
scanf("%d", &l);
}
for (int i = 0; i < n; ++i) {
auto &[l, r] = sg[i];
scanf("%d", &r);
}
for (int i = 0; i < n; ++i) {
auto &[l, r] = sg[i];
r += 1;
df[szd++] = l, df[szd++] = r;
prod = prod * (LL)(r - l) % mod;
}
df[szd++] = 1;
sort(df, df + szd), szd = unique(df, df + szd) - df;
for (int i = 0; i + 1 < szd; ++i) {
solve(df[i], df[i + 1]);
}
printf("%d\n", res);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3784kb
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 182 173 120 296 192 131
result:
ok 10 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3716kb
input:
10 5 1 2 2 5 3 6 4 2 6 3 5 4 4 1 4 3 6 7 2 5 3 5 5 3 4 2 4 5 7 5 2 6 5 1 5 3 5 2 7 7 3 5 2 5 1 3 3 2 2 10 5 3 2 2 5 4 4 4 5 3 4 11 9 5 3 5 5 3 2 1 3 13 5 2 1 5 5 5 4 1 2 5 10 6 1 2 5 5 3 5 3 4 2 5 9 3 5 2 5 1 1 3 2 1 7 3 3 3 1
output:
120 230 144 110 110 289 324 89 140 122
result:
ok 10 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3776kb
input:
10 5 3 1 3 4 4 9 1 3 10 4 5 1 1 3 1 1 9 1 3 3 1 5 5 1 2 3 1 74 1 2 3 1 5 2 5 5 3 4 5 6 8 3 4 5 2 1 3 4 5 2 4 6 4 5 5 2 4 2 1 3 2 11 3 2 3 5 1 5 4 4 2 1 14 6 6 2 5 4 1 3 5 1 9 2 4 5 1 5 4 1 2 4 4 6 1 6 4 4 5 3 2 5 3 5 8 8 5 3 5
output:
196 76 140 172 72 80 486 84 65 224
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 3764kb
input:
10 5 676437428 903889545 700650370 965758082 146716866 676437431 903889567 700650370 965758082 146716866 5 517457740 64600397 388618400 783268973 388618400 517457797 64600397 388618400 783268973 388618400 5 971452763 106948541 259878781 537741075 9504353 971452780 106948544 259878781 537741075 95043...
output:
157838571 539867046 711272106 123881231 497944943 9791579 539012259 963879245 315607794 495624077
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 1ms
memory: 3828kb
input:
10 5 462008700 417744555 925098328 70231697 547596413 462008735 417744555 925098328 70231697 547596413 5 294230630 403894618 294230635 403894620 403894617 294230638 403894620 294230635 403894620 403894617 5 757647830 757647826 757647828 184694646 161891480 757647839 757647827 757647830 184694646 161...
output:
713470735 905154643 458869425 477055327 633375786 349097981 980046476 478461437 573246310 810688575
result:
ok 10 lines
Test #6:
score: -100
Wrong Answer
time: 2ms
memory: 3848kb
input:
10 150 7 1 8 8 2 3 8 2 1 3 2 10 9 7 3 9 4 5 4 5 10 7 9 9 9 3 4 7 10 8 5 3 5 1 8 4 1 2 7 9 10 9 1 7 4 7 6 8 7 6 6 7 4 5 10 8 7 10 2 8 1 4 9 2 9 3 9 6 2 2 7 7 10 8 4 10 4 1 7 3 3 5 4 3 9 7 4 1 8 1 4 4 2 7 5 4 9 6 5 8 6 4 8 7 4 6 8 8 2 9 8 3 10 9 2 4 6 10 2 8 9 1 6 6 7 8 8 7 8 8 8 3 4 6 3 8 10 10 10 3 ...
output:
8640 994312007 44418240 80190 55721016 61220610 977185607 693746939 720976546 551255824
result:
wrong answer 2nd lines differ - expected: '8000', found: '994312007'