QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#419756 | #8715. 放苹果 | wsyear | AC ✓ | 229ms | 24676kb | C++17 | 8.3kb | 2024-05-24 10:57:07 | 2024-05-24 10:57:07 |
Judging History
answer
#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 = 998244353;
using Z = mod_int<P>;
namespace Poly_space {
std::vector<int> rev;
std::vector<Z> roots{0, 1};
void dft(std::vector<Z> &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 ? 1 << k : 0);
}
}
for (int i = 0; i < n; i++)
if (rev[i] < i) std::swap(a[i], a[rev[i]]);
if (int(roots.size()) < n) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e = Z(3).pow((P - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); i++)
roots[2 * i] = roots[i], roots[2 * i + 1] = roots[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++) {
Z u = a[i + j], v = a[i + j + k] * roots[k + j];
a[i + j] = u + v, a[i + j + k] = u - v;
}
}
void idft(std::vector<Z> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
Z inv = (1 - P) / n;
for (int i = 0; i < n; i++) a[i] *= inv;
}
struct Poly {
std::vector<Z> a;
Poly() {}
Poly(const std::vector<Z> &a) : a(a) {}
Poly(const std::initializer_list<Z> &a) : a(a) {}
int size() const { return a.size(); }
void resize(int n) { a.resize(n); }
Z operator[](int idx) const {
if (idx < 0 || idx >= size()) return 0;
return a[idx];
}
Z &operator[](int idx) { return a[idx]; }
Poly mulxk(int k) const {
auto b = a;
b.insert(b.begin(), k, 0);
return Poly(b);
}
Poly modxk(int k) const {
k = std::min(k, size());
return Poly(std::vector<Z>(a.begin(), a.begin() + k));
}
Poly divxk(int k) const {
if (size() <= k) return Poly();
return Poly(std::vector<Z>(a.begin() + k, a.end()));
}
friend Poly operator+(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) res[i] = a[i] + b[i];
return Poly(res);
}
friend Poly operator-(const Poly &a, const Poly &b) {
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) res[i] = a[i] - b[i];
return Poly(res);
}
friend Poly operator*(Poly a, Poly b) {
if (a.size() == 0 || b.size() == 0) return Poly();
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot) sz *= 2;
a.a.resize(sz), b.a.resize(sz), dft(a.a), dft(b.a);
for (int i = 0; i < sz; ++i) a.a[i] = a[i] * b[i];
idft(a.a), a.resize(tot);
return a;
}
friend Poly operator*(Z a, Poly b) {
for (int i = 0; i < int(b.size()); i++) b[i] *= a;
return b;
}
friend Poly operator*(Poly a, Z b) {
for (int i = 0; i < int(a.size()); i++) a[i] *= b;
return a;
}
Poly &operator+=(Poly b) { return (*this) = (*this) + b; }
Poly &operator-=(Poly b) { return (*this) = (*this) - b; }
Poly &operator*=(Poly b) { return (*this) = (*this) * b; }
Poly deriv() const {
if (a.empty()) return Poly();
std::vector<Z> res(size() - 1);
for (int i = 0; i < size() - 1; ++i) res[i] = (i + 1) * a[i + 1];
return Poly(res);
}
Poly integr() const {
std::vector<Z> res(size() + 1);
for (int i = 0; i < size(); ++i) res[i + 1] = a[i] / (i + 1);
return Poly(res);
}
Poly inv(int m) const {
Poly x{a[0].inv()};
int k = 1;
while (k < m) k *= 2, x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
return x.modxk(m);
}
Poly log(int m) const {
return (deriv() * inv(m)).integr().modxk(m);
}
Poly exp(int m) const {
Poly x{1};
int k = 1;
while (k < m) k *= 2, x = (x * (Poly{1} - x.log(k) + modxk(k))).modxk(k);
return x.modxk(m);
}
Poly sqrt(int m) const {
Poly x{1};
int k = 1;
while (k < m) k *= 2, x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((P + 1) / 2);
return x.modxk(m);
}
Poly mulT(Poly b) const {
if (b.size() == 0) return Poly();
int n = b.size();
std::reverse(b.a.begin(), b.a.end());
return ((*this) * b).divxk(n - 1);
}
std::vector<Z> eval(std::vector<Z> x) const {
if (size() == 0) return std::vector<Z>(x.size(), 0);
const int n = std::max(int(x.size()), size());
std::vector<Poly> q(4 * n);
std::vector<Z> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) q[p] = Poly{1, -x[l]};
else {
int m = (l + r) / 2;
build(2 * p, l, m), build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
if (r - l == 1) {
if (l < int(ans.size())) ans[l] = num[0];
} else {
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).modxk(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};
}
using namespace Poly_space;
const int maxn = 200010;
const Z iv2 = (P + 1) >> 1;
Z fac[maxn], ivf[maxn], h[maxn], b[maxn];
int n, m;
Z binom(int x, int y) {
if (x < 0 || y < 0 || x < y) return 0;
return fac[x] * ivf[y] * ivf[x - y];
}
int coe(int x) {
return (x & 1) ? P - 1 : 1;
}
int main() {
Poly F, G, H;
cin.tie(nullptr) -> ios::sync_with_stdio(false);
cin >> n >> m;
fac[0] = 1;
rep (i, 1, n + 1) fac[i] = fac[i - 1] * i;
ivf[n + 1] = fac[n + 1].inv();
per (i, n + 1, 1) ivf[i - 1] = ivf[i] * i;
rep (i, 0, n) F.a.emplace_back(ivf[i + 1]);
F = F.inv(n + 2);
b[0] = 1;
rep (i, 1, n) b[i] = F[i] * fac[i];
F.a.clear(), G.a.clear();
rep (i, 0, n) F.a.emplace_back(ivf[i] * b[i]);
rep (i, 1, n + 1) G.a.emplace_back(ivf[i] * Z(m).pow(i));
H = F * G;
rep (p, 0, n) h[p] = H[p] * fac[p];
Z ans = 0;
F.a.clear(), G.a.clear();
rep (i, 0, n) F.a.emplace_back(h[n - i] * Z(m).pow(i) * ivf[i]);
rep (i, 0, n) G.a.emplace_back(ivf[i] * coe(i));
H = F * G;
rep (p, 0, n) ans += H[n - p] * binom(n, p) * fac[n - p] * min(p, n - p);
cout << ans.val() << '\n';
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6668kb
input:
2 3
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 1ms
memory: 6656kb
input:
3 3
output:
36
result:
ok 1 number(s): "36"
Test #3:
score: 0
Accepted
time: 1ms
memory: 6740kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 6640kb
input:
1 2
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 1ms
memory: 6652kb
input:
1 3
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 0ms
memory: 6736kb
input:
2 1
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 1ms
memory: 6744kb
input:
3 1
output:
0
result:
ok 1 number(s): "0"
Test #8:
score: 0
Accepted
time: 4ms
memory: 6784kb
input:
3719 101
output:
78994090
result:
ok 1 number(s): "78994090"
Test #9:
score: 0
Accepted
time: 3ms
memory: 7012kb
input:
2189 1022
output:
149789741
result:
ok 1 number(s): "149789741"
Test #10:
score: 0
Accepted
time: 0ms
memory: 7048kb
input:
2910 382012013
output:
926541722
result:
ok 1 number(s): "926541722"
Test #11:
score: 0
Accepted
time: 194ms
memory: 20712kb
input:
131072 3837829
output:
487765455
result:
ok 1 number(s): "487765455"
Test #12:
score: 0
Accepted
time: 229ms
memory: 24412kb
input:
183092 100000000
output:
231786691
result:
ok 1 number(s): "231786691"
Test #13:
score: 0
Accepted
time: 220ms
memory: 24676kb
input:
197291 937201572
output:
337054675
result:
ok 1 number(s): "337054675"
Test #14:
score: 0
Accepted
time: 220ms
memory: 24152kb
input:
200000 328194672
output:
420979346
result:
ok 1 number(s): "420979346"
Test #15:
score: 0
Accepted
time: 221ms
memory: 24544kb
input:
200000 1000000000
output:
961552572
result:
ok 1 number(s): "961552572"
Extra Test:
score: 0
Extra Test Passed