QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#729995 | #9572. Bingo | ucup-team4435# | AC ✓ | 118ms | 12932kb | C++20 | 34.9kb | 2024-11-09 18:14:14 | 2024-11-09 18:14:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define len(a) int((a).size())
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...)
#define dprint(...)
#define debug if constexpr (false)
#endif // LOCAL
/*
! WARNING: MOD must be prime.
! WARNING: MOD must be less than 2^30.
* Use .get() to get the stored value.
*/
template<uint32_t mod>
class montgomery {
static_assert(mod < uint32_t(1) << 30, "mod < 2^30");
using mint = montgomery<mod>;
private:
uint32_t value;
static constexpr uint32_t inv_neg_mod = []() {
uint32_t x = mod;
for (int i = 0; i < 4; ++i) {
x *= uint32_t(2) - mod * x;
}
return -x;
}();
static_assert(mod * inv_neg_mod == -1);
static constexpr uint32_t neg_mod = (-uint64_t(mod)) % mod;
static uint32_t reduce(const uint64_t &value) {
return (value + uint64_t(uint32_t(value) * inv_neg_mod) * mod) >> 32;
}
inline static const mint ONE = mint(1);
public:
montgomery() : value(0) {}
montgomery(const mint &x) : value(x.value) {}
template<typename T, typename U = std::enable_if_t<std::is_integral<T>::value>>
montgomery(const T &x) : value(!x ? 0 : reduce(int64_t(x % int32_t(mod) + int32_t(mod)) * neg_mod)) {}
static constexpr uint32_t get_mod() {
return mod;
}
uint32_t get() const {
auto real_value = reduce(value);
return real_value < mod ? real_value : real_value - mod;
}
template<typename T>
mint power(T degree) const {
degree = (degree % int32_t(mod - 1) + int32_t(mod - 1)) % int32_t(mod - 1);
mint prod = 1, a = *this;
for (; degree > 0; degree >>= 1, a *= a)
if (degree & 1)
prod *= a;
return prod;
}
mint inv() const {
return power(-1);
}
mint& operator=(const mint &x) {
value = x.value;
return *this;
}
mint& operator+=(const mint &x) {
if (int32_t(value += x.value - (mod << 1)) < 0) {
value += mod << 1;
}
return *this;
}
mint& operator-=(const mint &x) {
if (int32_t(value -= x.value) < 0) {
value += mod << 1;
}
return *this;
}
mint& operator*=(const mint &x) {
value = reduce(uint64_t(value) * x.value);
return *this;
}
mint& operator/=(const mint &x) {
return *this *= x.inv();
}
friend mint operator+(const mint &x, const mint &y) {
return mint(x) += y;
}
friend mint operator-(const mint &x, const mint &y) {
return mint(x) -= y;
}
friend mint operator*(const mint &x, const mint &y) {
return mint(x) *= y;
}
friend mint operator/(const mint &x, const mint &y) {
return mint(x) /= y;
}
mint& operator++() {
return *this += ONE;
}
mint& operator--() {
return *this -= ONE;
}
mint operator++(int) {
mint prev = *this;
*this += ONE;
return prev;
}
mint operator--(int) {
mint prev = *this;
*this -= ONE;
return prev;
}
mint operator-() const {
return mint(0) - *this;
}
bool operator==(const mint &x) const {
return get() == x.get();
}
bool operator!=(const mint &x) const {
return get() != x.get();
}
bool operator<(const mint &x) const {
return get() < x.get();
}
template<typename T>
explicit operator T() {
return get();
}
friend std::istream& operator>>(std::istream &in, mint &x) {
std::string s;
in >> s;
x = 0;
bool neg = s[0] == '-';
for (const auto c : s)
if (c != '-')
x = x * 10 + (c - '0');
if (neg)
x *= -1;
return in;
}
friend std::ostream& operator<<(std::ostream &out, const mint &x) {
return out << x.get();
}
static int32_t primitive_root() {
if constexpr (mod == 1'000'000'007)
return 5;
if constexpr (mod == 998'244'353)
return 3;
if constexpr (mod == 786433)
return 10;
static int root = -1;
if (root != -1)
return root;
std::vector<int> primes;
int value = mod - 1;
for (int i = 2; i * i <= value; i++)
if (value % i == 0) {
primes.push_back(i);
while (value % i == 0)
value /= i;
}
if (value != 1)
primes.push_back(value);
for (int r = 2;; r++) {
bool ok = true;
for (auto p : primes)
if ((mint(r).power((mod - 1) / p)).get() == 1) {
ok = false;
break;
}
if (ok)
return root = r;
}
}
};
// constexpr uint32_t MOD = 1'000'000'007;
constexpr uint32_t MOD = 998'244'353;
using mint = montgomery<MOD>;
/*
! WARNING: (MOD - 1) must be divisible by (2n), where n is max degree of polynomials.
* Include static_modular_int or montgomery (faster) to use it.
* Don't need to care about precomputing primitive root.
* Use it like std::vector<mint> with extra methods.
*/
template<typename mint>
class polynom_t : public std::vector<mint> {
public:
static constexpr int RANK = __builtin_ctz(mint::get_mod() - 1);
static_assert(RANK >= 15, "MOD doesn't seem fft-friendly.");
using value_type = mint;
using std::vector<mint>::empty;
using std::vector<mint>::back;
using std::vector<mint>::pop_back;
using std::vector<mint>::size;
using std::vector<mint>::clear;
using std::vector<mint>::begin;
using std::vector<mint>::end;
private:
static constexpr int EVAL_BRUTE_SIZE = 1 << 4;
static constexpr int MUL_MIN_CUT = 20;
static constexpr int MUL_MAX_CUT = 1 << 6;
static constexpr int DIV_N_CUT = 1 << 7;
static constexpr int DIV_M_CUT = 1 << 6;
static constexpr int INV_BRUTE_FORCE_SIZE = 1 << 3;
class fft_precalc {
private:
std::vector<mint> inv_{0, 1};
public:
mint root;
// roots[x] = root.power((MOD - 1) / 2^x)
// inv_roots[x] = roots[x].inv()
std::array<mint, RANK> roots, inv_roots;
// inv_l[x] = 2^{-x}
std::array<mint, RANK> inv_l;
// r_transition[x] = root.power((MOD - 1) * (3 - 2^{x + 1}) / 2^{x + 2})
// inv_r_transition[x] = r_transition[x].inv()
std::array<mint, RANK> r_transition, inv_r_transition;
fft_precalc() {
root = mint::primitive_root();
for (int x = 0; x < RANK; x++) {
roots[x] = mint(root).power((mint::get_mod() - 1) >> x);
inv_roots[x] = roots[x].inv();
inv_l[x] = mint(1 << x).inv();
r_transition[x] = root.power(((mint::get_mod() - 1) >> (x + 2)) * (3 + (1 << (x + 1))));
inv_r_transition[x] = r_transition[x].inv();
}
}
mint inv(int x) {
for (int i = inv_.size(); i <= x; i++) {
inv_.push_back(-inv_[mint::get_mod() % i] * mint(mint::get_mod() / i));
}
return inv_[x];
}
};
inline static fft_precalc fft_data;
public:
/*
* Let r be the primitive root of the given MOD.
* Let rev(i) be the reversed i as a binary mask of size log(n).
* Let R[i] = r.power((MOD - 1) / a.size() * rev(i)).
* It replaces a[i] with a.eval(R[i]).
*/
static void fft(polynom_t<mint> &a) {
assert(!a.empty());
int n = a.size(), l = __builtin_ctz(n);
for (int len = 0; len < l; len++) {
int m = n >> (len + 1);
mint r = 1;
for (int i = 0; i < (1 << len); i++) {
// rot = r^((MOD - 1) / n * rev(2 * i))
int id = i << (l - len);
for (int j = 0; j < m; j++) {
auto u = a[id + j];
auto v = a[id + j + m] * r;
a[id + j] = u + v;
a[id + j + m] = u - v;
}
if (i + 1 < (1 << len)) {
r *= fft_data.r_transition[__builtin_ctz(~i)];
}
}
}
}
// inv_fft(fft(a)) = a
static void inv_fft(polynom_t<mint> &a) {
assert(!a.empty());
int n = a.size(), l = __builtin_ctz(n);
for (int len = l - 1; len >= 0; len--) {
int m = n >> (len + 1);
mint ir = 1;
for (int i = 0; i < (1 << len); i++) {
int id = i << (l - len);
for (int j = 0; j < m; j++) {
auto u = a[id + j];
auto v = a[id + j + m];
a[id + j] = u + v;
a[id + j + m] = (u - v) * ir;
}
if (i + 1 < (1 << len)) {
ir *= fft_data.inv_r_transition[__builtin_ctz(~i)];
}
}
}
for (auto &value : a) {
value *= fft_data.inv_l[l];
}
}
private:
// Completes fft assuming that a is the right half of the polynomial after the first iteration of the fft.
static void right_half_fft(polynom_t<mint> &a) {
assert(!a.empty());
int n = a.size(), l = __builtin_ctz(n);
for (int len = 0; len < l; len++) {
int m = n >> (len + 1);
mint r = fft_data.roots[len + 2];
for (int i = 0; i < (1 << len); i++) {
int id = i << (l - len);
for (int j = 0; j < m; j++) {
auto u = a[id + j];
auto v = a[id + j + m] * r;
a[id + j] = u + v;
a[id + j + m] = u - v;
}
if (i + 1 < (1 << len)) {
r *= fft_data.r_transition[__builtin_ctz(~i)];
}
}
}
}
// inv_right_half_fft(right_half_fft(a)) = a
static void inv_right_half_fft(polynom_t<mint> &a) {
assert(!a.empty());
int n = a.size(), l = __builtin_ctz(n);
for (int len = l - 1; len >= 0; len--) {
int m = n >> (len + 1);
mint ir = fft_data.inv_roots[len + 2];
for (int i = 0; i < (1 << len); i++) {
int id = i << (l - len);
for (int j = 0; j < m; j++) {
auto u = a[id + j];
auto v = a[id + j + m];
a[id + j] = u + v;
a[id + j + m] = (u - v) * ir;
}
if (i + 1 < (1 << len)) {
ir *= fft_data.inv_r_transition[__builtin_ctz(~i)];
}
}
}
for (auto &value : a) {
value *= fft_data.inv_l[l];
}
}
class multipoint_evaluation_tree {
private:
int n_points, l;
std::vector<polynom_t<mint>> segtree;
public:
multipoint_evaluation_tree(int n, const std::vector<mint> &points) : n_points(points.size()), l(1) {
while ((1 << l) < n) {
l++;
}
polynom_t<mint> aux;
aux.reserve(1 << l);
segtree.assign(l + 1, polynom_t<mint>(1 << (l + 1)));
for (int i = 0; i < (1 << l); i++) {
aux = {i < n_points ? -points[i] : 0, 1};
fft(aux);
segtree[0][i << 1] = aux[0];
segtree[0][i << 1 | 1] = aux[1];
}
for (int len = 0; len < l; len++) {
aux.resize(1 << (len + 1));
for (int i = 0; i < (1 << (l + 1)); i += (1 << (len + 2))) {
for (int j = 0; j < static_cast<int>(aux.size()); j++) {
aux[j] = segtree[len][i + j] * segtree[len][i + j + aux.size()];
}
if (len + 1 < l) {
std::copy(aux.begin(), aux.end(), segtree[len + 1].begin() + i);
inv_fft(aux);
aux[0] -= 2;
right_half_fft(aux);
std::copy(aux.begin(), aux.end(), segtree[len + 1].begin() + i + aux.size());
} else {
inv_fft(aux);
aux[0]--;
std::copy(aux.begin(), aux.end(), segtree[len + 1].begin() + i);
segtree[len + 1][1 << (len + 1)]++;
}
}
}
}
std::vector<mint> evaluate(polynom_t<mint> f) const {
if (static_cast<int>(f.size()) > (1 << l)) {
f %= segtree[l];
}
assert(static_cast<int>(f.size()) <= (1 << l));
f.resize(1 << (l + 1));
std::rotate(f.begin(), std::prev(f.end()), f.end());
fft(f);
auto g = segtree[l];
std::reverse(g.begin(), g.begin() + 1 + (1 << l));
g.resize(1 << l);
g = g.inv(1 << l);
std::reverse(g.begin(), g.end());
g.resize(1 << (l + 1));
fft(g);
for (int i = 0; i < (1 << (l + 1)); i++) {
g[i] *= f[i];
}
polynom_t<mint> aux;
aux.reserve(1 << l);
for (int len = l - 1; len >= 0; len--) {
aux.resize(1 << (len + 1));
for (int i = 0; i < (1 << (l + 1)); i += (1 << (len + 2))) {
for (int j = 0; j < static_cast<int>(aux.size()); j++) {
aux[j] = g[i + j + (1 << (len + 1))];
}
inv_right_half_fft(aux);
fft(aux);
std::copy(aux.begin(), aux.end(), g.begin() + i + (1 << (len + 1)));
for (int j = 0; j < (1 << (len + 1)); j++) {
auto x = g[i + j] - g[i + j + (1 << (len + 1))];
g[i + j + (1 << (len + 1))] = x * segtree[len][i + j];
g[i + j] = x * segtree[len][i + j + (1 << (len + 1))];
}
}
}
std::vector<mint> eval(n_points);
for (int i = 0; i < n_points; i++) {
eval[i] = (g[2 * i] - g[2 * i + 1]) * fft_data.inv_l[l + 1];
}
return eval;
}
polynom_t<mint> interpolate(const std::vector<mint> &y) const {
auto prod = segtree[l];
prod.erase(prod.begin(), prod.begin() + ((1 << l) - n_points));
auto values = evaluate(prod.derivative());
polynom_t<mint> aux;
aux.reserve(1 << l);
polynom_t<mint> result(1 << (l + 1));
for (int i = 0; i < (1 << l); i++) {
aux = {i < n_points ? y[i] / values[i] : 0, 0};
fft(aux);
result[i << 1] = aux[0];
result[i << 1 | 1] = aux[1];
}
polynom_t<mint> next(1 << (l + 1));
for (int len = 0; len < l; len++) {
aux.resize(1 << (len + 1));
for (int i = 0; i < (1 << (l + 1)); i += (1 << (len + 2))) {
for (int j = 0; j < (1 << (len + 1)); j++) {
aux[j] = segtree[len][i + j] * result[i + j + aux.size()]
+ segtree[len][i + j + aux.size()] * result[i + j];
}
if (len + 1 < l) {
std::copy(aux.begin(), aux.end(), next.begin() + i);
inv_fft(aux);
right_half_fft(aux);
std::copy(aux.begin(), aux.end(), next.begin() + i + (1 << (len + 1)));
} else {
inv_fft(aux);
std::copy(aux.begin(), aux.end(), next.begin());
}
}
result.swap(next);
}
result.erase(result.begin(), result.begin() + ((1 << l) - n_points));
return result.resize(n_points);
}
};
public:
polynom_t() : std::vector<mint>() {}
polynom_t(size_t n, mint value = 0) : std::vector<mint>(n, value) {}
polynom_t(std::initializer_list<mint> values) : std::vector<mint>(values.begin(), values.end()) {}
template<typename Iterator>
polynom_t(Iterator first, Iterator last) : std::vector<mint>(first, last) {}
polynom_t<mint>& resize(int n) {
std::vector<mint>::resize(n);
return *this;
}
// Removes extra zeroes.
void normalize() {
while (!empty() && back() == mint(0)) {
pop_back();
}
}
// Returns -1 if all coefficients are zeroes (not O(1)!).
int degree() const {
int deg = static_cast<int>(size()) - 1;
while (deg >= 0 && (*this)[deg] == mint(0)) {
deg--;
}
return deg;
}
mint eval(const mint &x) const {
mint power = 1, value = 0;
for (int i = 0; i < static_cast<int>(size()); i++, power *= x) {
value += (*this)[i] * power;
}
return value;
}
// Calculates eval at the given points.
// O(n log^2).
std::vector<mint> multipoint_evaluation(const std::vector<mint> &points) const {
if (std::min(size(), points.size()) <= EVAL_BRUTE_SIZE) {
std::vector<mint> eval(points.size());
for (int i = 0; i < static_cast<int>(points.size()); i++) {
eval[i] = this->eval(points[i]);
}
return eval;
}
return multipoint_evaluation_tree(std::max(size(), points.size()), points).evaluate(*this);
}
// Interpolates polynomial f, such that f(x[i]) = y[i].
// O(n log^2).
static polynom_t<mint> interpolate(const std::vector<mint> &x, const std::vector<mint> &y) {
assert(x.size() == y.size());
return multipoint_evaluation_tree(x.size(), x).interpolate(y);
}
polynom_t<mint> operator-() const {
return polynom_t(*this) * mint(-1);
}
polynom_t<mint>& operator+=(const polynom_t<mint> &another) {
if (size() < another.size()) {
resize(another.size());
}
for (int i = 0; i < static_cast<int>(another.size()); i++) {
(*this)[i] += another[i];
}
return *this;
}
polynom_t<mint>& operator-=(const polynom_t<mint> &another) {
if (size() < another.size()) {
resize(another.size());
}
for (int i = 0; i < static_cast<int>(another.size()); i++) {
(*this)[i] -= another[i];
}
return *this;
}
polynom_t<mint>& operator*=(const polynom_t<mint> &another) {
if (empty() || another.empty()) {
clear();
return *this;
}
if (std::min(size(), another.size()) <= MUL_MIN_CUT
|| std::max(size(), another.size()) <= MUL_MAX_CUT) {
polynom_t<mint> product(int(size() + another.size()) - 1);
for (int i = 0; i < static_cast<int>(size()); i++) {
for (int j = 0; j < static_cast<int>(another.size()); j++) {
product[i + j] += (*this)[i] * another[j];
}
}
product.swap(*this);
return *this;
}
int real_size = static_cast<int>(size() + another.size()) - 1, n = 1;
while (n < real_size) {
n <<= 1;
}
if ((*this) == another) {
resize(n);
fft(*this);
for (int i = 0; i < n; i++) {
(*this)[i] *= (*this)[i];
}
} else {
resize(n);
polynom_t b = another;
b.resize(n);
fft(*this), fft(b);
for (int i = 0; i < n; i++) {
(*this)[i] *= b[i];
}
}
inv_fft(*this);
return resize(real_size);
}
// Division with remainder.
// O(n log).
polynom_t<mint>& operator/=(const polynom_t<mint> &another) {
polynom_t<mint> a(*this), b(another);
a.normalize(), b.normalize();
assert(!b.empty());
int n = a.size(), m = b.size();
if (n < m) {
return *this = {};
}
if (n <= DIV_N_CUT || m <= DIV_M_CUT) {
polynom_t<mint> quotient(n - m + 1);
mint inv_b = mint(1) / b.back();
for (int i = n - 1; i >= m - 1; i--) {
int pos = i - m + 1;
quotient[pos] = a[i] * inv_b;
for (int j = 0; j < m; j++) {
a[pos + j] -= b[j] * quotient[pos];
}
}
quotient.normalize();
return *this = quotient;
}
std::reverse(a.begin(), a.end());
std::reverse(b.begin(), b.end());
polynom_t<mint> quotient = a * b.inv(n - m + 1);
quotient.resize(n - m + 1);
std::reverse(quotient.begin(), quotient.end());
quotient.normalize();
return *this = quotient;
}
// O(n log).
polynom_t<mint>& operator%=(const polynom_t<mint> &another) {
*this -= (*this) / another * another;
normalize();
return *this;
}
// Returns derivative.
// O(n).
polynom_t<mint> derivative() const {
polynom_t<mint> der(std::max(0, static_cast<int>(size()) - 1));
for (int i = 0; i < static_cast<int>(der.size()); i++) {
der[i] = mint(i + 1) * (*this)[i + 1];
}
return der;
}
// Returns integral.
// O(n).
polynom_t<mint> integral(const mint &constant = mint(0)) const {
polynom_t<mint> in(size() + 1);
in[0] = constant;
for (int i = 1; i < static_cast<int>(in.size()); i++) {
in[i] = (*this)[i - 1] * fft_data.inv(i);
}
return in;
}
// Returns p^{-1} modulo x^{degree}.
// O(n log).
polynom_t<mint> inv(int degree) const {
assert(!empty() && (*this)[0] != mint(0) && "polynom is not invertable");
int result_size = 1;
while (result_size < degree) {
result_size <<= 1;
}
polynom_t<mint> inv(result_size);
inv[0] = (*this)[0].inv();
int brute_calculation = std::min(degree, INV_BRUTE_FORCE_SIZE);
mint start_inv = (*this)[0].inv();
polynom_t<mint> have(brute_calculation);
for (int i = 0; i < brute_calculation; i++) {
inv[i] = ((i == 0 ? 1 : 0) - have[i]) * start_inv;
int steps = std::min<int>(size(), have.size() - i);
for (int j = 0; j < steps; j++) {
have[i + j] += inv[i] * (*this)[j];
}
}
polynom_t<mint> this_copy, inv_copy;
this_copy.reserve(result_size);
inv_copy.reserve(result_size);
for (int power = brute_calculation; power < degree; power <<= 1) {
this_copy.resize(power << 1);
std::fill(this_copy.begin() + std::min<int>(size(), power << 1), this_copy.end(), 0);
std::copy(begin(), begin() + std::min<int>(size(), power << 1), this_copy.begin());
inv_copy.resize(power << 1);
std::copy(inv.begin(), inv.begin() + power, inv_copy.begin());
fft(this_copy);
fft(inv_copy);
for (int i = 0; i < (power << 1); i++) {
this_copy[i] *= inv_copy[i];
}
inv_fft(this_copy);
std::fill(this_copy.begin(), this_copy.begin() + power, 0);
fft(this_copy);
for (int i = 0; i < (power << 1); i++) {
this_copy[i] *= inv_copy[i];
}
inv_fft(this_copy);
for (int i = power; i < (power << 1); i++) {
inv[i] = -this_copy[i];
}
}
return inv.resize(degree);
}
// Returns log(p) modulo x^{degree}.
// O(n log).
polynom_t<mint> log(int degree) const {
assert(!empty() && (*this)[0] == mint(1) && "log is not defined");
return (derivative().resize(std::min<int>(degree, size())) * inv(degree)).resize(degree).integral(mint(0)).resize(degree);
}
// Returns exp(p) modulo x^{degree}.
// O(n log).
polynom_t<mint> exp(int degree) const {
assert(!empty() && (*this)[0] == mint(0) && "exp is not defined");
int result_size = 1;
while (result_size < degree) {
result_size <<= 1;
}
polynom_t<mint> exp, exp_fft, inv_exp, inv_exp_fft;
exp = exp_fft = inv_exp = inv_exp_fft = {1};
polynom_t<mint> h(1), q(1);
auto diff = derivative().resize(result_size);
for (int power = 1; power < degree; power <<= 1) {
exp_fft = polynom_t<mint>(exp).resize(power << 1);
fft(exp_fft);
for (int i = 0; i < power; i++) {
h[i] = exp_fft[i] * inv_exp_fft[i];
}
inv_fft(h);
std::fill(h.begin(), h.begin() + ((power + 1) >> 1), 0);
fft(h);
for (int i = 0; i < power; i++) {
h[i] *= -inv_exp_fft[i];
}
inv_fft(h);
for (int i = (power + 1) / 2; i < power; i++) {
inv_exp.push_back(h[i]);
}
inv_exp_fft = polynom_t<mint>(inv_exp).resize(power << 1);
fft(inv_exp_fft);
h.assign(power, 0);
std::copy(diff.begin(), diff.begin() + power - 1, h.begin());
fft(h);
for (int i = 0; i < power; i++) {
q[i] = exp_fft[i] * h[i];
}
inv_fft(q);
h.resize(power << 1);
for (int i = 1; i < power; i++) {
h[i] = exp[i] * i;
}
for (int i = 0; i < power; i++) {
h[i + 1] -= q[i];
}
h[0] = -q[power - 1];
fft(h);
q.resize(power << 1);
for (int i = 0; i < (power << 1); i++) {
q[i] = inv_exp_fft[i] * h[i];
}
inv_fft(q);
if (static_cast<int>(size()) >= power) {
h.assign(begin() + power, begin() + std::min<int>(power << 1, size()));
} else {
h.clear();
}
h.resize(power);
for (int i = power - 1; i >= 0; i--) {
h[i] -= q[i] * fft_data.inv(i + power);
}
h.resize(power << 1);
fft(h);
for (int i = 0; i < (power << 1); i++) {
q[i] = exp_fft[i] * h[i];
}
inv_fft(q);
exp.resize(power << 1);
for (int i = 0; i < power; i++) {
exp[power + i] = q[i];
}
}
return exp.resize(degree);
}
// Returns p^{d} modulo x^{degree}.
// O(n log).
polynom_t<mint> power(int64_t d, int degree) const {
if (!d || !degree) {
return polynom_t<mint>{1}.resize(degree);
}
int pos = 0;
while (pos < static_cast<int>(size()) && (*this)[pos] == mint(0)) {
pos++;
}
if (pos == static_cast<int>(size()) || pos >= (degree + d - 1) / d) {
return polynom_t<mint>(degree);
}
int coeffs_left = degree - d * pos;
polynom_t<mint> result = ((polynom_t<mint>(begin() + pos, end()) / (*this)[pos]).log(coeffs_left)
* mint(d)).exp(coeffs_left) * (*this)[pos].power(d);
result.resize(degree);
for (int i = degree - 1; i - (degree - coeffs_left) >= 0; i--) {
result[i] = result[i - (degree - coeffs_left)];
}
std::fill(result.begin(), result.end() - coeffs_left, mint(0));
return result;
}
// Returns f(x + c).
// O(n log).
polynom_t<mint> change_of_variable(mint c) const {
int n = size();
polynom_t<mint> p(n), q(n);
mint fact = 1, ifact = 1, power = 1;
for (int i = 0; i < n; i++) {
p[n - 1 - i] = (*this)[i] * fact;
q[i] = power * ifact;
fact *= i + 1;
ifact *= fft_data.inv(i + 1);
power *= c;
}
auto result = p * q;
result.resize(n);
std::reverse(result.begin(), result.end());
ifact = 1;
for (int i = 0; i < n; i++) {
result[i] *= ifact;
ifact *= fft_data.inv(i + 1);
}
return result;
}
template<typename V>
std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>&> operator*=(const V &value) {
for (auto &x : *this) {
x *= value;
}
return *this;
}
template<typename V>
std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>&> operator/=(const V &value) {
for (auto &x : *this) {
x /= value;
}
return *this;
}
friend std::ostream& operator<<(std::ostream &out, const polynom_t<mint> &p) {
for (int i = 0; i < static_cast<int>(p.size()); i++) {
if (i) out << ' ';
out << p[i];
}
return out;
}
friend polynom_t<mint> operator+(const polynom_t<mint> &p, const polynom_t<mint> &q) {
return polynom_t(p) += q;
}
friend polynom_t<mint> operator-(const polynom_t<mint> &p, const polynom_t<mint> &q) {
return polynom_t(p) -= q;
}
friend polynom_t<mint> operator*(const polynom_t<mint> &p, const polynom_t<mint> &q) {
return polynom_t(p) *= q;
}
friend polynom_t<mint> operator/(const polynom_t<mint> &p, const polynom_t<mint> &q) {
return polynom_t(p) /= q;
}
friend polynom_t<mint> operator%(const polynom_t<mint> &p, const polynom_t<mint> &q) {
return polynom_t(p) %= q;
}
template<typename V>
friend std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>>
operator*(const V &value, const polynom_t<mint> &p) {
return polynom_t<mint>(p) *= value;
}
template<typename V>
friend std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>>
operator*(const polynom_t<mint> &p, const V &value) {
return polynom_t<mint>(p) *= value;
}
template<typename V>
friend std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>>
operator/(const V &value, const polynom_t<mint> &p) {
return polynom_t<mint>(p) /= value;
}
template<typename V>
friend std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>>
operator/(const polynom_t<mint> &p, const V &value) {
return polynom_t<mint>(p) /= value;
}
};
using polynom = polynom_t<mint>;
/*
! WARNING: MOD must be prime.
* Define modular int class above it.
* No need to run any init function, it dynamically resizes the data.
*/
namespace combinatorics {
std::vector<mint> fact_, ifact_, inv_;
void resize_data(int size) {
if (fact_.empty()) {
fact_ = {mint(1), mint(1)};
ifact_ = {mint(1), mint(1)};
inv_ = {mint(0), mint(1)};
}
for (int pos = fact_.size(); pos <= size; pos++) {
fact_.push_back(fact_.back() * mint(pos));
inv_.push_back(-inv_[MOD % pos] * mint(MOD / pos));
ifact_.push_back(ifact_.back() * inv_[pos]);
}
}
struct combinatorics_info {
std::vector<mint> &data;
combinatorics_info(std::vector<mint> &data) : data(data) {}
mint operator[](int pos) {
if (pos >= static_cast<int>(data.size())) {
resize_data(pos);
}
return data[pos];
}
} fact(fact_), ifact(ifact_), inv(inv_);
// From n choose k.
// O(max(n)) in total.
mint choose(int n, int k) {
if (n < k || k < 0 || n < 0) {
return mint(0);
}
return fact[n] * ifact[k] * ifact[n - k];
}
// From n choose k.
// O(min(k, n - k)).
mint choose_slow(int64_t n, int64_t k) {
if (n < k || k < 0 || n < 0) {
return mint(0);
}
k = std::min(k, n - k);
mint result = 1;
for (int i = k; i >= 1; i--) {
result *= (n - i + 1);
result *= inv[i];
}
return result;
}
// Number of balanced bracket sequences with n open and m closing brackets.
mint catalan(int n, int m) {
if (m > n || m < 0) {
return mint(0);
}
return choose(n + m, m) - choose(n + m, m - 1);
}
// Number of balanced bracket sequences with n open and closing brackets.
mint catalan(int n) {
return catalan(n, n);
}
} // namespace combinatorics
using namespace combinatorics;
void solve(int /* test_num */) {
int n, m;
cin >> n >> m;
vector<int> a(n * m);
for (auto &x : a) {
cin >> x;
}
sort(all(a));
auto f = [&](int i, int j) {
return m * i + n * j - i * j;
};
polynom A(n * m + 1);
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i + j > 0) {
mint coeff = ((i + j - 1) % 2 == 0 ? 1 : -1);
A[f(i, j)] += coeff * choose(n, i) * choose(m, j) * fact[n * m - f(i, j)];
}
}
}
polynom B(n * m + 1);
for (int i = 0; i < len(B); i++) {
B[i] = ifact[i];
}
auto C = A * B;
mint ans = 0, prev = 0;
for (int i = 0, j = 0; i < len(a); i = j) {
while (j < len(a) && a[i] == a[j]) {
j++;
}
int ones = j, zeros = len(a) - ones;
// mint ways = 0;
// for (auto [i, j] : cells) {
// mint val = choose(n, i) * choose(m, j) * choose(n * m - f(i, j), ones - f(i, j));
// if ((i + j - 1) % 2 == 0) {
// ways += val;
// } else {
// ways -= val;
// }
// }
// ways *= fact[ones] * fact[zeros];
mint ways = C[ones] * ifact[n * m - ones] * fact[ones] * fact[zeros];
ans += a[i] * (ways - prev);
prev = ways;
}
cout << ans << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tests;
cin >> tests;
for (int test_num = 1; test_num <= tests; test_num++) {
solve(test_num);
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3560kb
input:
4 2 2 1 3 2 4 3 1 10 10 10 1 3 20 10 30 3 4 1 1 4 5 1 4 1 9 1 9 8 10
output:
56 60 60 855346687
result:
ok 4 number(s): "56 60 60 855346687"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3536kb
input:
1 2 2 0 0 998244352 998244352
output:
998244345
result:
ok 1 number(s): "998244345"
Test #3:
score: 0
Accepted
time: 45ms
memory: 3972kb
input:
900 1 1 810487041 1 2 569006976 247513378 1 3 424212910 256484544 661426830 1 4 701056586 563296095 702740883 723333858 1 5 725786515 738465053 821758167 170452477 34260723 1 6 204184507 619884535 208921865 898995024 768400582 369477346 1 7 225635227 321139203 724076812 439129905 405005469 369864252...
output:
810487041 495026756 540662911 541929691 118309348 270925149 575366228 709974238 761347712 304011276 14811741 366145628 638305530 240546928 484276475 603344008 926633861 161768904 239961447 329781933 315752584 578075668 259941994 600147169 402221164 890998500 154285994 181862417 47930994 273729619 64...
result:
ok 900 numbers
Test #4:
score: 0
Accepted
time: 68ms
memory: 3880kb
input:
400 1 995 548100968 635656907 177366910 971271357 314579375 529572241 948721678 455918644 95745688 164857981 499083775 827896554 496889261 111294651 646048809 758286431 163045934 917399362 189372614 267754648 966443706 921589740 228089960 473153545 482816423 37567957 495730380 864445591 568695110 78...
output:
954668084 677509135 636173666 415373646 477286237 209886549 398423120 24466622 672440292 390142124 498517438 305197486 239833057 500821845 475519894 347179487 974036742 810602822 75196204 48378743 393961176 290898056 957916898 434124418 663457674 225283495 704304053 338701802 670053839 137083082 165...
result:
ok 400 numbers
Test #5:
score: 0
Accepted
time: 87ms
memory: 3940kb
input:
40 92 99 14480275 12892621 932457558 584861415 926346518 101583802 498448003 884757899 463949215 661256632 872663851 651132350 565253214 18404795 810166895 145370572 123351313 298382303 777283720 775900024 613503856 817112784 713304484 541301622 595768594 550989875 960159831 571815058 777864097 3608...
output:
614712898 16225927 313765200 824491114 60971514 769510634 58341639 808667102 527187053 319496150 267177120 409701892 245708713 115397703 928197397 533118123 931076329 448328887 672878477 180728812 385639338 504093069 846218180 981546177 906805965 315620628 863877552 348963788 781585156 982673320 275...
result:
ok 40 numbers
Test #6:
score: 0
Accepted
time: 68ms
memory: 3880kb
input:
40 86 92 479103936 690362573 387313968 428679987 770097821 67859949 744428797 919332570 530162857 389639443 851979342 310332074 863845681 155743453 442066584 996725021 385646576 447381083 64960590 818019444 260564007 16381359 36238584 609449698 12466296 532193395 262308857 279184524 454814687 400578...
output:
147127348 995441625 947321329 200561175 846810174 626764591 235790337 30932003 384829067 254218916 20342301 451884441 634808121 241161955 246093492 515701050 978130791 502129313 3431507 775910032 464454612 153447682 53092548 316439192 101505498 40191013 225025922 133184210 209384134 330521977 360716...
result:
ok 40 numbers
Test #7:
score: 0
Accepted
time: 115ms
memory: 12764kb
input:
2 447 447 790583748 764745604 779691526 67598288 308196334 738524513 685610494 336125979 294155123 651917356 898366384 420012139 529304950 133567869 630219750 62853597 606184670 383809162 43962071 826608376 652871696 860138865 675639996 444122802 823442992 841633845 125418467 211407031 726738308 984...
output:
506347658 891054810
result:
ok 2 number(s): "506347658 891054810"
Test #8:
score: 0
Accepted
time: 116ms
memory: 12760kb
input:
2 100 2000 414412015 610256524 548060717 581032168 761297097 50124687 831351681 906381893 842125801 82512258 734351512 844649420 370666628 791011946 232557748 968208094 238084359 933173727 306257334 509581201 774756848 370039888 322700146 641635730 8423279 909781921 238370638 28574480 725141803 9941...
output:
380238486 147107381
result:
ok 2 number(s): "380238486 147107381"
Test #9:
score: 0
Accepted
time: 113ms
memory: 12800kb
input:
2 2000 100 432504867 225538929 546658423 260257767 682179463 892187612 142884587 872658039 89862243 117086929 104310686 342803717 47992235 148221787 695186286 875318238 264248632 320257869 568552597 54600213 364423602 412159309 666014765 235168890 795627687 977929998 351322809 9778000 723545298 1693...
output:
807761546 460321345
result:
ok 2 number(s): "807761546 460321345"
Test #10:
score: 0
Accepted
time: 116ms
memory: 12916kb
input:
2 10 20000 450597719 675029617 315027614 635737834 439025757 505777670 590615658 142679716 637832921 847916068 472514213 71186529 723562195 273447466 297524284 782428382 428366719 869622434 528857976 735817391 650344824 152288845 779100871 130691934 584587742 513859676 996493379 687235989 189730396 ...
output:
579362183 459093435
result:
ok 2 number(s): "579362183 459093435"
Test #11:
score: 0
Accepted
time: 116ms
memory: 12932kb
input:
2 20000 10 770680455 822530420 615615204 314963433 892126521 815622197 900392916 410945746 187559247 278510970 538727855 101559225 98897919 326911775 760152822 689538526 60266407 256706575 791153240 418790216 772229976 194408266 426161021 328204862 71557913 976272337 111201197 504403438 188133891 58...
output:
30164141 385139412
result:
ok 2 number(s): "30164141 385139412"
Test #12:
score: 0
Accepted
time: 111ms
memory: 12756kb
input:
2 100000 2 224212357 458006968 163025536 269449920 699657932 932776912 420937536 166351734 685658904 344666962 946460500 884461444 228370491 174980092 646368291 854381092 617669653 366836379 717071379 463349902 749408189 163205331 665200568 666647060 230069001 195048922 357469436 37819596 212080713 ...
output:
188269415 372357321
result:
ok 2 number(s): "188269415 372357321"
Test #13:
score: 0
Accepted
time: 118ms
memory: 12920kb
input:
2 2 100000 242305209 73289374 463613125 946919872 154514343 546366969 34460325 132627880 629649815 379241632 14429790 612844256 207685982 530434285 412742360 761491236 249569341 450174989 677376758 146322726 339074943 507314636 10270834 864159988 715283525 729222953 772411491 19023116 374520280 8624...
output:
178386797 319825470
result:
ok 2 number(s): "178386797 319825470"
Test #14:
score: 0
Accepted
time: 117ms
memory: 12748kb
input:
2 1 200000 562387945 522780061 928236786 626145471 377386592 856211496 180201513 702883794 179376140 808080887 382633317 110998553 883255942 655659964 711334827 668601380 413687428 303285085 939672021 525550020 460960094 549434056 957565221 759683032 202253696 797371030 885363662 532445034 674913659...
output:
499141558 710898596
result:
ok 2 number(s): "499141558 710898596"
Extra Test:
score: 0
Extra Test Passed