QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#762871 | #9631. Median Replacement | superguymj# | WA | 0ms | 3876kb | C++20 | 14.6kb | 2024-11-19 17:01:45 | 2024-11-19 17:01:52 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,x,y) for (int i = x; i <= y; i++)
#define repd(i,x,y) for (int i = x; i >= y; i--)
#define mid ((l + r) >> 1)
#define lch (rt << 1)
#define rch (rt << 1 | 1)
using namespace std;
using i64 = long long;
template<class T>
T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
template<int P>
struct MInt {
int x;
MInt() : x{} {}
MInt(i64 x) : x{norm(x % getMod())} {}
static int Mod;
static int getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
static void setMod(int Mod_) {
Mod = Mod_;
}
int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
int val() const {
return x;
}
explicit operator int() const {
return x;
}
MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
MInt &operator*=(MInt rhs) & {
x = 1LL * x * rhs.x % getMod();
return *this;
}
MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<>
int MInt<0>::Mod = 1;
template<int V, int P>
MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 1000000007;
using Z = MInt<P>;
std::vector<int> rev;
template<int P>
std::vector<MInt<P>> roots{0, 1};
template<int P>
MInt<P> findPrimitiveRoot() {
MInt<P> i = 2;
int k = __builtin_ctz(P - 1);
while (true) {
if (power(i, (P - 1) / 2) != 1) {
break;
}
i += 1;
}
return power(i, (P - 1) >> k);
}
template<int P>
MInt<P> primitiveRoot = findPrimitiveRoot<P>();
template<>
MInt<998244353> primitiveRoot<998244353> {31};
template<int P>
void dft(std::vector<MInt<P>> &a) {
int n = a.size();
if (int(rev.size()) != n) {
int k = __builtin_ctz(n) - 1;
rev.resize(n);
for (int i = 0; i < n; i++) {
rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
}
}
for (int i = 0; i < n; i++) {
if (rev[i] < i) {
std::swap(a[i], a[rev[i]]);
}
}
if (roots<P>.size() < n) {
int k = __builtin_ctz(roots<P>.size());
roots<P>.resize(n);
while ((1 << k) < n) {
auto e = power(primitiveRoot<P>, 1 << (__builtin_ctz(P - 1) - k - 1));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots<P>[2 * i] = roots<P>[i];
roots<P>[2 * i + 1] = roots<P>[i] * e;
}
k++;
}
}
for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
MInt<P> u = a[i + j];
MInt<P> v = a[i + j + k] * roots<P>[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}
template<int P>
void idft(std::vector<MInt<P>> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
MInt<P> inv = (1 - P) / n;
for (int i = 0; i < n; i++) {
a[i] *= inv;
}
}
template<int P = 1000000007>
struct Poly : public std::vector<MInt<P>> {
using Value = MInt<P>;
Poly() : std::vector<Value>() {}
explicit Poly(int n) : std::vector<Value>(n) {}
explicit Poly(const std::vector<Value> &a) : std::vector<Value>(a) {}
Poly(const std::initializer_list<Value> &a) : std::vector<Value>(a) {}
template<class InputIt, class = std::_RequireInputIter<InputIt>>
explicit Poly(InputIt first, InputIt last) : std::vector<Value>(first, last) {}
template<class F>
explicit Poly(int n, F f) : std::vector<Value>(n) {
for (int i = 0; i < n; i++) {
(*this)[i] = f(i);
}
}
Poly shift(int k) const {
if (k >= 0) {
auto b = *this;
b.insert(b.begin(), k, 0);
return b;
} else if (this->size() <= -k) {
return Poly();
} else {
return Poly(this->begin() + (-k), this->end());
}
}
Poly reverse() const {
Poly f = *this;
std::reverse(f.begin(), f.end());
return f;
}
Poly trunc(int k) const {
Poly f = *this;
f.resize(k);
return f;
}
friend Poly operator+(const Poly &a, const Poly &b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < a.size(); i++) {
res[i] += a[i];
}
for (int i = 0; i < b.size(); i++) {
res[i] += b[i];
}
return res;
}
friend Poly operator-(const Poly &a, const Poly &b) {
Poly res(std::max(a.size(), b.size()));
for (int i = 0; i < a.size(); i++) {
res[i] += a[i];
}
for (int i = 0; i < b.size(); i++) {
res[i] -= b[i];
}
return res;
}
friend Poly operator-(const Poly &a) {
std::vector<Value> res(a.size());
for (int i = 0; i < int(res.size()); i++) {
res[i] = -a[i];
}
return Poly(res);
}
friend Poly operator*(Poly a, Poly b) {
if (a.size() == 0 || b.size() == 0) {
return Poly();
}
if (a.size() < b.size()) {
std::swap(a, b);
}
int n = 1, tot = a.size() + b.size() - 1;
while (n < tot) {
n *= 2;
}
if (((P - 1) & (n - 1)) != 0 || b.size() < 128) {
Poly c(a.size() + b.size() - 1);
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
c[i + j] += a[i] * b[j];
}
}
return c;
}
a.resize(n);
b.resize(n);
dft(a);
dft(b);
for (int i = 0; i < n; ++i) {
a[i] *= b[i];
}
idft(a);
a.resize(tot);
return a;
}
friend Poly operator*(Value a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}
friend Poly operator*(Poly a, Value b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}
friend Poly operator/(Poly a, Value b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] /= b;
}
return a;
}
friend Poly operator%(Poly a, Poly b) {
assert(b.size() > 0);
if (a.size() < b.size()) {
return a;
}
int n = a.size();
int m = b.size();
return (a - (a.reverse().trunc(n - m + 1) * b.reverse().inv(n - m + 1)).trunc(n - m + 1).reverse() * b).trunc(m - 1);
}
Poly &operator+=(Poly b) {
if (this -> size() < b.size()) {
this -> resize(b.size());
}
for (int i = 0; i < b.size(); i++) {
(*this)[i] += b[i];
}
return *this;
}
Poly &operator-=(Poly b) {
if (this -> size() < b.size()) {
this -> resize(b.size());
}
for (int i = 0; i < b.size(); i++) {
(*this)[i] -= b[i];
}
return *this;
}
Poly &operator*=(Poly b) {
return (*this) = (*this) * b;
}
Poly &operator*=(Value b) {
return (*this) = (*this) * b;
}
Poly &operator/=(Value b) {
return (*this) = (*this) / b;
}
Poly deriv() const {
if (this->empty()) {
return Poly();
}
Poly res(this->size() - 1);
for (int i = 0; i < this->size() - 1; ++i) {
res[i] = (i + 1) * (*this)[i + 1];
}
return res;
}
Poly integr() const {
Poly res(this->size() + 1);
for (int i = 0; i < this->size(); ++i) {
res[i + 1] = (*this)[i] / (i + 1);
}
return res;
}
Poly inv(int m) const {
Poly x{(*this)[0].inv()};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{2} - trunc(k) * x)).trunc(k);
}
return x.trunc(m);
}
Poly log(int m) const {
return (deriv() * inv(m)).integr().trunc(m);
}
Poly exp(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{1} - x.log(k) + trunc(k))).trunc(k);
}
return x.trunc(m);
}
Poly pow(int k, int m) const {
int i = 0;
while (i < this->size() && (*this)[i] == 0) {
i++;
}
if (i == this->size() || 1LL * i * k >= m) {
return Poly(m);
}
Value v = (*this)[i];
auto f = shift(-i) * v.inv();
return (f.log(m - i * k) * k).exp(m - i * k).shift(i * k) * power(v, k);
}
Poly sqrt(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x + (trunc(k) * x.inv(k)).trunc(k)) * CInv<2, P>;
}
return x.trunc(m);
}
Poly mulT(Poly b) const {
if (b.size() == 0) {
return Poly();
}
int n = b.size();
std::reverse(b.begin(), b.end());
return ((*this) * b).shift(-(n - 1));
}
};
using M = vector<vector<Z>>;
M inv(M a) {
int n = a.size();
M b(n, vector<Z>(n));
rep(i,0,n-1) {
b[i][i] = 1;
}
rep(i,0,n-1) {
int p = i;
rep(j,i,n-1) {
if (a[j][i] != 0) {
p = j;
break;
}
}
swap(a[i], a[p]);
swap(b[i], b[p]);
Z I = 1 / a[i][i];
rep(j,0,n-1) {
a[i][j] *= I;
b[i][j] *= I;
}
rep(j,0,n-1) {
if (i == j) {
continue;
}
Z t = a[j][i];
rep(k,0,n-1) {
a[j][k] -= a[i][k] * t;
b[j][k] -= b[i][k] * t;
}
}
}
return b;
}
vector<Z> operator*(M a, vector<Z> b) {
int n = a.size();
vector<Z> c(n);
rep(i,0,n-1) {
rep(j,0,n-1) {
c[i] += a[i][j] * b[j];
}
}
return c;
}
void solve() {
int n;
cin >> n;
vector<int> l(n), r(n);
rep(i,0,n-1) {
cin >> l[i];
}
rep(i,0,n-1) {
cin >> r[i];
r[i]++;
}
vector<int> p{0};
rep(i,0,n-1) {
p.push_back(l[i]);
p.push_back(r[i]);
}
sort(p.begin(), p.end());
p.resize(unique(p.begin(), p.end()) - p.begin());
const int N = n + 1;
M a(N, vector<Z>(N));
rep(i,0,N-1) {
a[i][0] = 1;
rep(j,1,N-1) {
a[i][j] = a[i][j - 1] * i;
}
}
a = inv(a);
M c(N, vector<Z>(N));
rep(i,0,N-1) {
vector<Z> b(N);
rep(j,0,N-1) {
b[j] = power(Z(j), i);
if (j) {
b[j] += b[j - 1];
}
}
c[i] = a * b;
}
auto calc = [&](int k, int l, int r) -> Z {
Z res{0};
Z x = r - 1;
Z pw = 1;
rep(i,0,N-1) {
res += c[k][i] * pw;
pw *= x;
}
x = l - 1;
pw = 1;
rep(i,0,N-1) {
res -= c[k][i] * pw;
pw *= x;
}
return res;
};
int m = p.size();
Z ans{0};
rep(i,0,m-2) {
vector<Poly<P>> f(16);
auto get = [&](int i, int L, int R) {
if (r[i] <= L) {
return make_pair(Poly({r[i] - l[i]}), Poly({0}));
}
if (R <= l[i]) {
return make_pair(Poly({0}), Poly({r[i] - l[i]}));
}
return make_pair(Poly({1 - l[i], 1}), Poly({r[i] - 1, -1}));
};
rep(s,0,7) {
int t = s | (__builtin_popcount(s) > 1) << 3;
f[t] = {1};
rep(j,0,2) {
auto [f0, f1] = get(j, p[i], p[i + 1]);
if (s >> j & 1) {
f[t] *= f1;
} else {
f[t] *= f0;
}
}
}
rep(j,3,n-1) {
vector<Poly<P>> nf(16);
auto [f0, f1] = get(j, p[i], p[i + 1]);
rep(k,0,15) {
rep(t,0,1) {
int s = (k & 7) >> 1 | (t << 2);
s |= (__builtin_popcount(s) > 1) << 3;
if (t) {
nf[s] += f[k] * f1;
} else {
nf[s] += f[k] * f0;
}
}
}
swap(f, nf);
}
Poly F;
rep(s,0,7) {
F += f[s | 8];
}
for (int j = 0; j < F.size(); j++) {
ans += F[j] * calc(j, p[i], p[i + 1]);
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3876kb
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:
120 60 288 159 180 90 90 280 192 84
result:
wrong answer 1st lines differ - expected: '180', found: '120'