QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#894914 | #3084. Count Min Ratio | ohwphil | AC ✓ | 521ms | 68976kb | C++17 | 32.0kb | 2025-02-11 22:41:44 | 2025-02-11 22:41:45 |
Judging History
answer
#include <bits/stdc++.h>
#include <cassert>
#include <numeric>
#include <type_traits>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#include <utility>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0) x += m;
return x;
}
struct barrett {
unsigned int _m;
unsigned long long im;
explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
unsigned int umod() const { return _m; }
unsigned int mul(unsigned int a, unsigned int b) const {
unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned long long y = x * _m;
return (unsigned int)(z - y + (z < y ? _m : 0));
}
};
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
constexpr bool is_prime_constexpr(int n) {
if (n <= 1) return false;
if (n == 2 || n == 7 || n == 61) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
while (d % 2 == 0) d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
long long s = b, t = a;
long long m0 = 0, m1 = 1;
while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 < 0) m0 += b / s;
return {s, m0};
}
constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (long long)(i)*i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
divs[cnt++] = x;
}
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);
unsigned long long floor_sum_unsigned(unsigned long long n,
unsigned long long m,
unsigned long long a,
unsigned long long b) {
unsigned long long ans = 0;
while (true) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}
unsigned long long y_max = a * n + b;
if (y_max < m) break;
n = (unsigned long long)(y_max / m);
b = (unsigned long long)(y_max % m);
std::swap(m, a);
}
return ans;
}
} // namespace internal
} // namespace atcoder
#include <cassert>
#include <numeric>
#include <type_traits>
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <class T> using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
namespace atcoder {
namespace internal {
struct modint_base {};
struct static_modint_base : modint_base {};
template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;
} // namespace internal
template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
using mint = static_modint;
public:
static constexpr int mod() { return m; }
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
static_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
static_modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
static_modint(T v) {
_v = (unsigned int)(v % umod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
if (prime) {
assert(_v);
return pow(umod() - 2);
} else {
auto eg = internal::inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static constexpr unsigned int umod() { return m; }
static constexpr bool prime = internal::is_prime<m>;
};
template <int id> struct dynamic_modint : internal::modint_base {
using mint = dynamic_modint;
public:
static int mod() { return (int)(bt.umod()); }
static void set_mod(int m) {
assert(1 <= m);
bt = internal::barrett(m);
}
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
dynamic_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
dynamic_modint(T v) {
long long x = (long long)(v % (long long)(mod()));
if (x < 0) x += mod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
dynamic_modint(T v) {
_v = (unsigned int)(v % mod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator*=(const mint& rhs) {
_v = bt.mul(_v, rhs._v);
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
auto eg = internal::inv_gcd(_v, mod());
assert(eg.first == 1);
return eg.second;
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;
namespace internal {
template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;
template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;
template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};
template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;
} // namespace internal
} // namespace atcoder
#include <algorithm>
#include <array>
#include <cassert>
#include <type_traits>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#if __cplusplus >= 202002L
#include <bit>
#endif
namespace atcoder {
namespace internal {
#if __cplusplus >= 202002L
using std::bit_ceil;
#else
unsigned int bit_ceil(unsigned int n) {
unsigned int x = 1;
while (x < (unsigned int)(n)) x *= 2;
return x;
}
#endif
int countr_zero(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
constexpr int countr_zero_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
} // namespace internal
} // namespace atcoder
namespace atcoder {
namespace internal {
template <class mint,
int g = internal::primitive_root<mint::mod()>,
internal::is_static_modint_t<mint>* = nullptr>
struct fft_info {
static constexpr int rank2 = countr_zero_constexpr(mint::mod() - 1);
std::array<mint, rank2 + 1> root; // root[i]^(2^i) == 1
std::array<mint, rank2 + 1> iroot; // root[i] * iroot[i] == 1
std::array<mint, std::max(0, rank2 - 2 + 1)> rate2;
std::array<mint, std::max(0, rank2 - 2 + 1)> irate2;
std::array<mint, std::max(0, rank2 - 3 + 1)> rate3;
std::array<mint, std::max(0, rank2 - 3 + 1)> irate3;
fft_info() {
root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2);
iroot[rank2] = root[rank2].inv();
for (int i = rank2 - 1; i >= 0; i--) {
root[i] = root[i + 1] * root[i + 1];
iroot[i] = iroot[i + 1] * iroot[i + 1];
}
{
mint prod = 1, iprod = 1;
for (int i = 0; i <= rank2 - 2; i++) {
rate2[i] = root[i + 2] * prod;
irate2[i] = iroot[i + 2] * iprod;
prod *= iroot[i + 2];
iprod *= root[i + 2];
}
}
{
mint prod = 1, iprod = 1;
for (int i = 0; i <= rank2 - 3; i++) {
rate3[i] = root[i + 3] * prod;
irate3[i] = iroot[i + 3] * iprod;
prod *= iroot[i + 3];
iprod *= root[i + 3];
}
}
}
};
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly(std::vector<mint>& a) {
int n = int(a.size());
int h = internal::countr_zero((unsigned int)n);
static const fft_info<mint> info;
int len = 0; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
while (len < h) {
if (h - len == 1) {
int p = 1 << (h - len - 1);
mint rot = 1;
for (int s = 0; s < (1 << len); s++) {
int offset = s << (h - len);
for (int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p] * rot;
a[i + offset] = l + r;
a[i + offset + p] = l - r;
}
if (s + 1 != (1 << len))
rot *= info.rate2[countr_zero(~(unsigned int)(s))];
}
len++;
} else {
int p = 1 << (h - len - 2);
mint rot = 1, imag = info.root[2];
for (int s = 0; s < (1 << len); s++) {
mint rot2 = rot * rot;
mint rot3 = rot2 * rot;
int offset = s << (h - len);
for (int i = 0; i < p; i++) {
auto mod2 = 1ULL * mint::mod() * mint::mod();
auto a0 = 1ULL * a[i + offset].val();
auto a1 = 1ULL * a[i + offset + p].val() * rot.val();
auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val();
auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val();
auto a1na3imag =
1ULL * mint(a1 + mod2 - a3).val() * imag.val();
auto na2 = mod2 - a2;
a[i + offset] = a0 + a2 + a1 + a3;
a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3));
a[i + offset + 2 * p] = a0 + na2 + a1na3imag;
a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag);
}
if (s + 1 != (1 << len))
rot *= info.rate3[countr_zero(~(unsigned int)(s))];
}
len += 2;
}
}
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly_inv(std::vector<mint>& a) {
int n = int(a.size());
int h = internal::countr_zero((unsigned int)n);
static const fft_info<mint> info;
int len = h; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
while (len) {
if (len == 1) {
int p = 1 << (h - len);
mint irot = 1;
for (int s = 0; s < (1 << (len - 1)); s++) {
int offset = s << (h - len + 1);
for (int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p];
a[i + offset] = l + r;
a[i + offset + p] =
(unsigned long long)(mint::mod() + l.val() - r.val()) *
irot.val();
;
}
if (s + 1 != (1 << (len - 1)))
irot *= info.irate2[countr_zero(~(unsigned int)(s))];
}
len--;
} else {
int p = 1 << (h - len);
mint irot = 1, iimag = info.iroot[2];
for (int s = 0; s < (1 << (len - 2)); s++) {
mint irot2 = irot * irot;
mint irot3 = irot2 * irot;
int offset = s << (h - len + 2);
for (int i = 0; i < p; i++) {
auto a0 = 1ULL * a[i + offset + 0 * p].val();
auto a1 = 1ULL * a[i + offset + 1 * p].val();
auto a2 = 1ULL * a[i + offset + 2 * p].val();
auto a3 = 1ULL * a[i + offset + 3 * p].val();
auto a2na3iimag =
1ULL *
mint((mint::mod() + a2 - a3) * iimag.val()).val();
a[i + offset] = a0 + a1 + a2 + a3;
a[i + offset + 1 * p] =
(a0 + (mint::mod() - a1) + a2na3iimag) * irot.val();
a[i + offset + 2 * p] =
(a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) *
irot2.val();
a[i + offset + 3 * p] =
(a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) *
irot3.val();
}
if (s + 1 != (1 << (len - 2)))
irot *= info.irate3[countr_zero(~(unsigned int)(s))];
}
len -= 2;
}
}
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_naive(const std::vector<mint>& a,
const std::vector<mint>& b) {
int n = int(a.size()), m = int(b.size());
std::vector<mint> ans(n + m - 1);
if (n < m) {
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
ans[i + j] += a[i] * b[j];
}
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i + j] += a[i] * b[j];
}
}
}
return ans;
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_fft(std::vector<mint> a, std::vector<mint> b) {
int n = int(a.size()), m = int(b.size());
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
a.resize(z);
internal::butterfly(a);
b.resize(z);
internal::butterfly(b);
for (int i = 0; i < z; i++) {
a[i] *= b[i];
}
internal::butterfly_inv(a);
a.resize(n + m - 1);
mint iz = mint(z).inv();
for (int i = 0; i < n + m - 1; i++) a[i] *= iz;
return a;
}
} // namespace internal
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(std::vector<mint>&& a, std::vector<mint>&& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
if (std::min(n, m) <= 60) return convolution_naive(std::move(a), std::move(b));
return internal::convolution_fft(std::move(a), std::move(b));
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(const std::vector<mint>& a,
const std::vector<mint>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
if (std::min(n, m) <= 60) return convolution_naive(a, b);
return internal::convolution_fft(a, b);
}
template <unsigned int mod = 998244353,
class T,
std::enable_if_t<internal::is_integral<T>::value>* = nullptr>
std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
using mint = static_modint<mod>;
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
std::vector<mint> a2(n), b2(m);
for (int i = 0; i < n; i++) {
a2[i] = mint(a[i]);
}
for (int i = 0; i < m; i++) {
b2[i] = mint(b[i]);
}
auto c2 = convolution(std::move(a2), std::move(b2));
std::vector<T> c(n + m - 1);
for (int i = 0; i < n + m - 1; i++) {
c[i] = c2[i].val();
}
return c;
}
std::vector<long long> convolution_ll(const std::vector<long long>& a,
const std::vector<long long>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
static constexpr unsigned long long MOD1 = 754974721; // 2^24
static constexpr unsigned long long MOD2 = 167772161; // 2^25
static constexpr unsigned long long MOD3 = 469762049; // 2^26
static constexpr unsigned long long M2M3 = MOD2 * MOD3;
static constexpr unsigned long long M1M3 = MOD1 * MOD3;
static constexpr unsigned long long M1M2 = MOD1 * MOD2;
static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;
static constexpr unsigned long long i1 =
internal::inv_gcd(MOD2 * MOD3, MOD1).second;
static constexpr unsigned long long i2 =
internal::inv_gcd(MOD1 * MOD3, MOD2).second;
static constexpr unsigned long long i3 =
internal::inv_gcd(MOD1 * MOD2, MOD3).second;
static constexpr int MAX_AB_BIT = 24;
static_assert(MOD1 % (1ull << MAX_AB_BIT) == 1, "MOD1 isn't enough to support an array length of 2^24.");
static_assert(MOD2 % (1ull << MAX_AB_BIT) == 1, "MOD2 isn't enough to support an array length of 2^24.");
static_assert(MOD3 % (1ull << MAX_AB_BIT) == 1, "MOD3 isn't enough to support an array length of 2^24.");
assert(n + m - 1 <= (1 << MAX_AB_BIT));
auto c1 = convolution<MOD1>(a, b);
auto c2 = convolution<MOD2>(a, b);
auto c3 = convolution<MOD3>(a, b);
std::vector<long long> c(n + m - 1);
for (int i = 0; i < n + m - 1; i++) {
unsigned long long x = 0;
x += (c1[i] * i1) % MOD1 * M2M3;
x += (c2[i] * i2) % MOD2 * M1M3;
x += (c3[i] * i3) % MOD3 * M1M2;
long long diff =
c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1));
if (diff < 0) diff += MOD1;
static constexpr unsigned long long offset[5] = {
0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
x -= offset[diff % 5];
c[i] = x;
}
return c;
}
} // namespace atcoder
using namespace std;
using namespace atcoder;
// Use ACL's modint with the desired modulus
using mint = modint998244353;
template<typename T>
struct Poly {
vector<T> a;
Poly() : a() {}
Poly(T a0) : a(1, a0) { normalize(); }
Poly(const vector<T>& a) : a(a) { normalize(); }
void normalize() { while (!a.empty() && a.back() == T(0)) a.pop_back(); }
int size() const { return a.size(); }
int deg() const { return size() - 1; }
void push_back(const T& x) { a.push_back(x); }
T get(int idx) const { return 0 <= idx && idx < size() ? a[idx] : T(0); }
T& operator [] (int idx) { return a[idx]; }
Poly reversed() const { auto b = a; reverse(b.begin(), b.end()); return b; }
Poly trim(int sz) const { return vector<T>(a.begin(), a.begin() + min(sz, size())); }
Poly inv(int n) const {
Poly q(T(1) / a[0]);
for (int i = 1; i < n; i <<= 1) {
Poly p = Poly(2) - q * trim(i * 2);
q = (p * q).trim(i * 2);
}
return q.trim(n);
}
Poly operator *= (const T& x) { for (auto& i : a) i *= x; normalize(); return *this; }
Poly operator /= (const T& x) { return *this *= T(1) / x; }
Poly operator += (const Poly& t) {
a.resize(max(size(), t.size()));
for (int i = 0; i < t.size(); i++) a[i] += t.a[i];
normalize(); return *this;
}
Poly operator -= (const Poly& t) {
a.resize(max(size(), t.size()));
for (int i = 0; i < t.size(); i++) a[i] -= t.a[i];
normalize(); return *this;
}
Poly operator *= (const Poly& t) {
a = convolution(a, t.a); // Use ACL's convolution
normalize();
return *this;
}
Poly operator /= (const Poly& t) {
if (deg() < t.deg()) return *this = Poly();
int sz = deg() - t.deg() + 1;
Poly ra = reversed().trim(sz), rb = t.reversed().trim(sz).inv(sz);
*this = (ra * rb).trim(sz);
for (int i = sz - size(); i; i--) a.push_back(T(0));
reverse(a.begin(), a.end()); normalize();
return *this;
}
Poly operator %= (const Poly& t) {
if (deg() < t.deg()) return *this;
Poly tmp = *this; tmp /= t; tmp *= t;
*this -= tmp; normalize();
return *this;
}
Poly operator + (const Poly& t) const { return Poly(*this) += t; }
Poly operator - (const Poly& t) const { return Poly(*this) -= t; }
Poly operator * (const Poly& t) const { return Poly(*this) *= t; }
Poly operator / (const Poly& t) const { return Poly(*this) /= t; }
Poly operator % (const Poly& t) const { return Poly(*this) %= t; }
Poly operator * (const T x) const { return Poly(*this) *= x; }
Poly operator / (const T x) const { return Poly(*this) /= x; }
T eval(T x) const {
T res = 0;
for (int i = deg(); i >= 0; i--) res = res * x + a[i];
return res;
}
Poly derivative() const {
vector<T> res;
for (int i = 1; i < size(); i++) res.push_back(T(i) * a[i]);
return res;
}
Poly integral() const {
vector<T> res{ T(0) };
for (int i = 0; i < size(); i++) res.push_back(a[i] / T(i + 1));
return res;
}
Poly ln(int n) const {
assert(size() > 0 && a[0] == T(1));
return (derivative() * inv(n)).integral().trim(n);
}
Poly exp(int n) const {
if (size() == 0) return Poly(1);
assert(size() > 0 && a[0] == T(0));
Poly res(1);
for (int i = 1; i < n; i <<= 1) {
auto t = Poly(1) + trim(i * 2) - res.ln(i * 2);
res = (res * t).trim(i * 2);
}
return res.trim(n);
}
Poly pow(long long n, int k) const {
// compute f(x)^n mod x^k
Poly acc(1), t = *this;
t = t.trim(k);
for (; n; n >>= 1) {
if (n & 1) acc = (acc * t).trim(k);
t = (t * t).trim(k);
}
return acc;
}
};
using ModInt = modint998244353;
using ll = long long;
ll R, B;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> R >> B;
vector<ModInt> factorial(B + 3, 1), inv_factorial(B + 3, 1);
for (int i = 1; i <= B + 2; i++) {
factorial[i] = factorial[i - 1] * i;
}
inv_factorial[B + 2] = factorial[B + 2].inv();
for (int i = B + 2; i > 0; i--) {
inv_factorial[i - 1] = inv_factorial[i] * i;
}
vector<ModInt> binoms(B + 1);
binoms[0] = 1;
for (int i = 1; i <= B; ++i) {
binoms[i] = binoms[i - 1] * ModInt(R + B + 2 - i) * inv_factorial[i] * factorial[i - 1];
}
ll N = R / B;
vector<ModInt> exp_np1x(B + 2), exp_x(B + 2);
ModInt curr_Np1pow = ModInt(N + 1);
for (int i = 0; i <= B + 1; i++) {
exp_x[i] = inv_factorial[i + 1];
exp_np1x[i] = inv_factorial[i + 1] * curr_Np1pow;
curr_Np1pow *= ModInt(N + 1);
}
Poly<ModInt> exp_x_poly(exp_x), exp_np1x_poly(exp_np1x);
Poly<ModInt> coeffs = exp_np1x_poly * exp_x_poly.inv(B + 2);
coeffs = coeffs.trim(B + 2);
if (coeffs.size()) {
coeffs[0] -= 1;
}
for (int i = 1; i <= B + 1; i++) {
if (i >= coeffs.size()) break;
coeffs[i] *= factorial[i];
}
ModInt ans = 0;
for (int i = 0; i <= B; i++) {
ans += ModInt(R + 1) * binoms[i] * coeffs.get(B - i);
ans -= ModInt(B) * binoms[i] * coeffs.get(B + 1 - i);
}
cout << ans.val() << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 123ms
memory: 19852kb
input:
683378734395758315 236468
output:
977900314
result:
ok single line: '977900314'
Test #2:
score: 0
Accepted
time: 247ms
memory: 29360kb
input:
979090400467196153 358547
output:
272249768
result:
ok single line: '272249768'
Test #3:
score: 0
Accepted
time: 514ms
memory: 67988kb
input:
989388577384252441 976371
output:
177173344
result:
ok single line: '177173344'
Test #4:
score: 0
Accepted
time: 124ms
memory: 20008kb
input:
518112700506783575 241956
output:
326187036
result:
ok single line: '326187036'
Test #5:
score: 0
Accepted
time: 503ms
memory: 57660kb
input:
142225746666542457 762622
output:
169474298
result:
ok single line: '169474298'
Test #6:
score: 0
Accepted
time: 498ms
memory: 51188kb
input:
76682402966725409 589690
output:
742679145
result:
ok single line: '742679145'
Test #7:
score: 0
Accepted
time: 244ms
memory: 33020kb
input:
893084247022057423 436807
output:
792093980
result:
ok single line: '792093980'
Test #8:
score: 0
Accepted
time: 490ms
memory: 52204kb
input:
677057357948041903 621912
output:
566088487
result:
ok single line: '566088487'
Test #9:
score: 0
Accepted
time: 514ms
memory: 67376kb
input:
585185490292220209 960011
output:
395797373
result:
ok single line: '395797373'
Test #10:
score: 0
Accepted
time: 514ms
memory: 63676kb
input:
527128913915411756 864691
output:
626521264
result:
ok single line: '626521264'
Test #11:
score: 0
Accepted
time: 27ms
memory: 7104kb
input:
480275827542304693 60780
output:
208136653
result:
ok single line: '208136653'
Test #12:
score: 0
Accepted
time: 505ms
memory: 58480kb
input:
38401586484842791 782406
output:
445127574
result:
ok single line: '445127574'
Test #13:
score: 0
Accepted
time: 243ms
memory: 30912kb
input:
274389276438594725 392173
output:
336742756
result:
ok single line: '336742756'
Test #14:
score: 0
Accepted
time: 120ms
memory: 20276kb
input:
537410974978749538 246828
output:
8231958
result:
ok single line: '8231958'
Test #15:
score: 0
Accepted
time: 235ms
memory: 24900kb
input:
651479222208992250 265311
output:
147181253
result:
ok single line: '147181253'
Test #16:
score: 0
Accepted
time: 118ms
memory: 16800kb
input:
242868802681182462 189137
output:
572588508
result:
ok single line: '572588508'
Test #17:
score: 0
Accepted
time: 498ms
memory: 50388kb
input:
408214018246265459 560647
output:
686897443
result:
ok single line: '686897443'
Test #18:
score: 0
Accepted
time: 496ms
memory: 51468kb
input:
44121777270056545 599614
output:
319069814
result:
ok single line: '319069814'
Test #19:
score: 0
Accepted
time: 240ms
memory: 29700kb
input:
610290521186765100 365864
output:
5012106
result:
ok single line: '5012106'
Test #20:
score: 0
Accepted
time: 57ms
memory: 8972kb
input:
907257322745121966 65555
output:
330906285
result:
ok single line: '330906285'
Test #21:
score: 0
Accepted
time: 56ms
memory: 11976kb
input:
995630373529096980 128131
output:
363172456
result:
ok single line: '363172456'
Test #22:
score: 0
Accepted
time: 25ms
memory: 6456kb
input:
478531585013359623 45327
output:
489805295
result:
ok single line: '489805295'
Test #23:
score: 0
Accepted
time: 507ms
memory: 57692kb
input:
289220686690167539 762823
output:
931142647
result:
ok single line: '931142647'
Test #24:
score: 0
Accepted
time: 489ms
memory: 50484kb
input:
956128385648436543 564939
output:
327546179
result:
ok single line: '327546179'
Test #25:
score: 0
Accepted
time: 498ms
memory: 55844kb
input:
268987821611568675 714871
output:
681256386
result:
ok single line: '681256386'
Test #26:
score: 0
Accepted
time: 234ms
memory: 28636kb
input:
41530899487998973 344498
output:
450296001
result:
ok single line: '450296001'
Test #27:
score: 0
Accepted
time: 501ms
memory: 58088kb
input:
443735410834630537 772631
output:
684658654
result:
ok single line: '684658654'
Test #28:
score: 0
Accepted
time: 507ms
memory: 62316kb
input:
951396959824268572 830986
output:
65374615
result:
ok single line: '65374615'
Test #29:
score: 0
Accepted
time: 246ms
memory: 35384kb
input:
727656794203823997 488995
output:
445963467
result:
ok single line: '445963467'
Test #30:
score: 0
Accepted
time: 500ms
memory: 51692kb
input:
711635850174145854 608033
output:
385604773
result:
ok single line: '385604773'
Test #31:
score: 0
Accepted
time: 508ms
memory: 61548kb
input:
958593486141334376 811645
output:
716353950
result:
ok single line: '716353950'
Test #32:
score: 0
Accepted
time: 117ms
memory: 17588kb
input:
876169953712419552 212745
output:
741644191
result:
ok single line: '741644191'
Test #33:
score: 0
Accepted
time: 239ms
memory: 30320kb
input:
637173299850560942 377037
output:
643853630
result:
ok single line: '643853630'
Test #34:
score: 0
Accepted
time: 519ms
memory: 67456kb
input:
845819889032839962 960360
output:
300421637
result:
ok single line: '300421637'
Test #35:
score: 0
Accepted
time: 235ms
memory: 28312kb
input:
858157800928362508 336636
output:
317266260
result:
ok single line: '317266260'
Test #36:
score: 0
Accepted
time: 510ms
memory: 65616kb
input:
621886439901466850 913233
output:
62587572
result:
ok single line: '62587572'
Test #37:
score: 0
Accepted
time: 244ms
memory: 37056kb
input:
19793600863167987 523130
output:
454747537
result:
ok single line: '454747537'
Test #38:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
800097659409521417 2735
output:
498367926
result:
ok single line: '498367926'
Test #39:
score: 0
Accepted
time: 495ms
memory: 50100kb
input:
96718659537798620 549821
output:
104440627
result:
ok single line: '104440627'
Test #40:
score: 0
Accepted
time: 509ms
memory: 65532kb
input:
512381071437748587 910502
output:
768449929
result:
ok single line: '768449929'
Test #41:
score: 0
Accepted
time: 502ms
memory: 61624kb
input:
678834243213393 813374
output:
374395787
result:
ok single line: '374395787'
Test #42:
score: 0
Accepted
time: 491ms
memory: 49984kb
input:
33363233875471439 545561
output:
610128530
result:
ok single line: '610128530'
Test #43:
score: 0
Accepted
time: 116ms
memory: 16400kb
input:
179544154312981642 175883
output:
187932725
result:
ok single line: '187932725'
Test #44:
score: 0
Accepted
time: 513ms
memory: 68976kb
input:
696386454207742585 999005
output:
127047882
result:
ok single line: '127047882'
Test #45:
score: 0
Accepted
time: 508ms
memory: 62212kb
input:
610761556176970731 826320
output:
126494438
result:
ok single line: '126494438'
Test #46:
score: 0
Accepted
time: 53ms
memory: 9056kb
input:
282015621243753190 69441
output:
196294929
result:
ok single line: '196294929'
Test #47:
score: 0
Accepted
time: 242ms
memory: 31240kb
input:
84137964725034228 399086
output:
362319233
result:
ok single line: '362319233'
Test #48:
score: 0
Accepted
time: 505ms
memory: 56888kb
input:
397211021773266000 742250
output:
792606046
result:
ok single line: '792606046'
Test #49:
score: 0
Accepted
time: 115ms
memory: 15984kb
input:
544716383493341413 138950
output:
789809654
result:
ok single line: '789809654'
Test #50:
score: 0
Accepted
time: 245ms
memory: 35644kb
input:
169266483579812352 492569
output:
896396610
result:
ok single line: '896396610'
Test #51:
score: 0
Accepted
time: 500ms
memory: 54872kb
input:
999961057870786440 690363
output:
721544361
result:
ok single line: '721544361'
Test #52:
score: 0
Accepted
time: 509ms
memory: 65768kb
input:
278829653591349043 916814
output:
836880873
result:
ok single line: '836880873'
Test #53:
score: 0
Accepted
time: 495ms
memory: 51104kb
input:
210453150703882029 586356
output:
90787821
result:
ok single line: '90787821'
Test #54:
score: 0
Accepted
time: 512ms
memory: 68220kb
input:
184805922528049560 979882
output:
15187856
result:
ok single line: '15187856'
Test #55:
score: 0
Accepted
time: 242ms
memory: 32440kb
input:
564625421256646746 426949
output:
500082355
result:
ok single line: '500082355'
Test #56:
score: 0
Accepted
time: 501ms
memory: 54428kb
input:
732940561153051407 681732
output:
564670938
result:
ok single line: '564670938'
Test #57:
score: 0
Accepted
time: 494ms
memory: 51692kb
input:
116171740890278055 607634
output:
401012094
result:
ok single line: '401012094'
Test #58:
score: 0
Accepted
time: 237ms
memory: 29508kb
input:
805967230765387304 361535
output:
981172185
result:
ok single line: '981172185'
Test #59:
score: 0
Accepted
time: 54ms
memory: 10228kb
input:
442286081725079290 102140
output:
945328644
result:
ok single line: '945328644'
Test #60:
score: 0
Accepted
time: 236ms
memory: 27852kb
input:
593100620083622655 328979
output:
26517397
result:
ok single line: '26517397'
Test #61:
score: 0
Accepted
time: 505ms
memory: 65992kb
input:
369228649216946434 923981
output:
967449549
result:
ok single line: '967449549'
Test #62:
score: 0
Accepted
time: 245ms
memory: 36944kb
input:
867097686907691811 520338
output:
311001250
result:
ok single line: '311001250'
Test #63:
score: 0
Accepted
time: 241ms
memory: 28664kb
input:
970618223153396863 346325
output:
920370317
result:
ok single line: '920370317'
Test #64:
score: 0
Accepted
time: 512ms
memory: 65760kb
input:
938112313632104905 916660
output:
801817210
result:
ok single line: '801817210'
Test #65:
score: 0
Accepted
time: 248ms
memory: 34348kb
input:
445439139973740572 466762
output:
218747502
result:
ok single line: '218747502'
Test #66:
score: 0
Accepted
time: 242ms
memory: 31240kb
input:
642177853459241805 399086
output:
961758742
result:
ok single line: '961758742'
Test #67:
score: 0
Accepted
time: 4ms
memory: 4380kb
input:
315967632725600222 12109
output:
244881715
result:
ok single line: '244881715'
Test #68:
score: 0
Accepted
time: 233ms
memory: 28980kb
input:
290180569836470426 350636
output:
82758012
result:
ok single line: '82758012'
Test #69:
score: 0
Accepted
time: 117ms
memory: 17116kb
input:
219861379687179110 198893
output:
906025950
result:
ok single line: '906025950'
Test #70:
score: 0
Accepted
time: 122ms
memory: 17284kb
input:
314286634957673330 204928
output:
611659493
result:
ok single line: '611659493'
Test #71:
score: 0
Accepted
time: 116ms
memory: 16400kb
input:
141833982443113896 173643
output:
25787351
result:
ok single line: '25787351'
Test #72:
score: 0
Accepted
time: 521ms
memory: 68648kb
input:
656293290102168163 990715
output:
375890945
result:
ok single line: '375890945'
Test #73:
score: 0
Accepted
time: 13ms
memory: 4984kb
input:
795787394303445639 26120
output:
703297448
result:
ok single line: '703297448'
Test #74:
score: 0
Accepted
time: 238ms
memory: 25612kb
input:
404516778708210183 278693
output:
754235281
result:
ok single line: '754235281'
Test #75:
score: 0
Accepted
time: 513ms
memory: 67252kb
input:
815752358214716192 954803
output:
624809890
result:
ok single line: '624809890'
Test #76:
score: 0
Accepted
time: 501ms
memory: 56088kb
input:
6033256690046915 721561
output:
15093794
result:
ok single line: '15093794'
Test #77:
score: 0
Accepted
time: 504ms
memory: 58452kb
input:
229123183682627129 781800
output:
614606849
result:
ok single line: '614606849'
Test #78:
score: 0
Accepted
time: 503ms
memory: 56608kb
input:
45500860931655609 734639
output:
414234307
result:
ok single line: '414234307'
Test #79:
score: 0
Accepted
time: 499ms
memory: 55808kb
input:
411143153580195786 717525
output:
869882027
result:
ok single line: '869882027'
Test #80:
score: 0
Accepted
time: 240ms
memory: 28984kb
input:
837981964676080593 350729
output:
971717274
result:
ok single line: '971717274'
Test #81:
score: 0
Accepted
time: 499ms
memory: 54768kb
input:
104580223797803755 687802
output:
89986728
result:
ok single line: '89986728'
Test #82:
score: 0
Accepted
time: 245ms
memory: 36000kb
input:
43929905834825307 500678
output:
41001420
result:
ok single line: '41001420'
Test #83:
score: 0
Accepted
time: 59ms
memory: 11540kb
input:
659029063626506522 115342
output:
549415725
result:
ok single line: '549415725'
Test #84:
score: 0
Accepted
time: 513ms
memory: 64972kb
input:
975527438656796764 896996
output:
335298357
result:
ok single line: '335298357'
Test #85:
score: 0
Accepted
time: 28ms
memory: 6528kb
input:
41903412710012308 44339
output:
459070475
result:
ok single line: '459070475'
Test #86:
score: 0
Accepted
time: 244ms
memory: 36360kb
input:
199728788856097743 506539
output:
716828027
result:
ok single line: '716828027'
Test #87:
score: 0
Accepted
time: 502ms
memory: 54808kb
input:
421541678045878439 688271
output:
94569400
result:
ok single line: '94569400'
Test #88:
score: 0
Accepted
time: 246ms
memory: 35648kb
input:
791034501231194792 493676
output:
22349450
result:
ok single line: '22349450'
Test #89:
score: 0
Accepted
time: 514ms
memory: 68052kb
input:
514029786760975442 975431
output:
920960941
result:
ok single line: '920960941'
Test #90:
score: 0
Accepted
time: 245ms
memory: 34876kb
input:
615289178558364244 476243
output:
924051936
result:
ok single line: '924051936'
Test #91:
score: 0
Accepted
time: 498ms
memory: 53936kb
input:
402536439938086115 666553
output:
414913820
result:
ok single line: '414913820'
Test #92:
score: 0
Accepted
time: 244ms
memory: 34636kb
input:
407450809830779802 471059
output:
898345043
result:
ok single line: '898345043'
Test #93:
score: 0
Accepted
time: 510ms
memory: 62360kb
input:
54203387312776409 831213
output:
395210033
result:
ok single line: '395210033'
Test #94:
score: 0
Accepted
time: 12ms
memory: 5180kb
input:
989065424860678110 31896
output:
217306385
result:
ok single line: '217306385'
Test #95:
score: 0
Accepted
time: 238ms
memory: 28396kb
input:
867310479829046576 338035
output:
429174354
result:
ok single line: '429174354'
Test #96:
score: 0
Accepted
time: 238ms
memory: 24856kb
input:
749559649873639195 262837
output:
582660834
result:
ok single line: '582660834'
Test #97:
score: 0
Accepted
time: 512ms
memory: 65408kb
input:
353931598778126361 908992
output:
102642620
result:
ok single line: '102642620'
Test #98:
score: 0
Accepted
time: 239ms
memory: 34216kb
input:
294477693266176644 462263
output:
694707454
result:
ok single line: '694707454'
Test #99:
score: 0
Accepted
time: 500ms
memory: 62492kb
input:
428019413841133868 832595
output:
114075344
result:
ok single line: '114075344'
Test #100:
score: 0
Accepted
time: 120ms
memory: 15668kb
input:
660115115462352421 146580
output:
390845669
result:
ok single line: '390845669'