Submission #45737810
Source Code Expand
#include <bits/stdc++.h>
using i64 = long long;
template<class T>
constexpr 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;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % getMod())} {}
static int Mod;
constexpr static int getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_) {
Mod = Mod_;
}
constexpr int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<>
int MInt<0>::Mod = 1;
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 998244353;
using Z = MInt<P>;
std::vector<int> rev;
template<int P>
std::vector<MInt<P>> roots{0, 1};
template<int P>
constexpr 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>
constexpr MInt<P> primitiveRoot = findPrimitiveRoot<P>();
template<>
constexpr MInt<998244353> primitiveRoot<998244353> {31};
template<int P>
constexpr 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>
constexpr 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 = 998244353>
struct Poly : public std::vector<MInt<P>> {
using Value = MInt<P>;
Poly() : std::vector<Value>() {}
explicit constexpr Poly(int n) : std::vector<Value>(n) {}
explicit constexpr Poly(const std::vector<Value> &a) : std::vector<Value>(a) {}
constexpr Poly(const std::initializer_list<Value> &a) : std::vector<Value>(a) {}
template<class InputIt, class = std::_RequireInputIter<InputIt>>
explicit constexpr Poly(InputIt first, InputIt last) : std::vector<Value>(first, last) {}
template<class F>
explicit constexpr Poly(int n, F f) : std::vector<Value>(n) {
for (int i = 0; i < n; i++) {
(*this)[i] = f(i);
}
}
constexpr 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());
}
}
constexpr Poly trunc(int k) const {
Poly f = *this;
f.resize(k);
return f;
}
constexpr 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;
}
constexpr 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;
}
constexpr 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);
}
constexpr 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;
}
constexpr friend Poly operator*(Value a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}
constexpr friend Poly operator*(Poly a, Value b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}
constexpr friend Poly operator/(Poly a, Value b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] /= b;
}
return a;
}
constexpr Poly &operator+=(Poly b) {
return (*this) = (*this) + b;
}
constexpr Poly &operator-=(Poly b) {
return (*this) = (*this) - b;
}
constexpr Poly &operator*=(Poly b) {
return (*this) = (*this) * b;
}
constexpr Poly &operator*=(Value b) {
return (*this) = (*this) * b;
}
constexpr Poly &operator/=(Value b) {
return (*this) = (*this) / b;
}
constexpr 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;
}
constexpr 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;
}
constexpr 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);
}
constexpr Poly log(int m) const {
return (deriv() * inv(m)).integr().trunc(m);
}
constexpr 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);
}
constexpr 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);
}
constexpr 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);
}
constexpr 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));
}
constexpr std::vector<Value> eval(std::vector<Value> x) const {
if (this->size() == 0) {
return std::vector<Value>(x.size(), 0);
}
const int n = std::max(x.size(), this->size());
std::vector<Poly> q(4 * n);
std::vector<Value> 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]).resize(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).resize(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};
template<int P = 998244353>
Poly<P> berlekampMassey(const Poly<P> &s) {
Poly<P> c;
Poly<P> oldC;
int f = -1;
for (int i = 0; i < s.size(); i++) {
auto delta = s[i];
for (int j = 1; j <= c.size(); j++) {
delta -= c[j - 1] * s[i - j];
}
if (delta == 0) {
continue;
}
if (f == -1) {
c.resize(i + 1);
f = i;
} else {
auto d = oldC;
d *= -1;
d.insert(d.begin(), 1);
MInt<P> df1 = 0;
for (int j = 1; j <= d.size(); j++) {
df1 += d[j - 1] * s[f + 1 - j];
}
assert(df1 != 0);
auto coef = delta / df1;
d *= coef;
Poly<P> zeros(i - f - 1);
zeros.insert(zeros.end(), d.begin(), d.end());
d = zeros;
auto temp = c;
c += d;
if (i - temp.size() > f - oldC.size()) {
oldC = temp;
f = i;
}
}
}
c *= -1;
c.insert(c.begin(), 1);
return c;
}
template<int P = 998244353>
MInt<P> linearRecurrence(Poly<P> p, Poly<P> q, i64 n) {
int m = q.size() - 1;
while (n > 0) {
auto newq = q;
for (int i = 1; i <= m; i += 2) {
newq[i] *= -1;
}
auto newp = p * newq;
newq = q * newq;
for (int i = 0; i < m; i++) {
p[i] = newp[i * 2 + n % 2];
}
for (int i = 0; i <= m; i++) {
q[i] = newq[i * 2];
}
n /= 2;
}
return p[0] / q[0];
}
struct Comb {
int n;
std::vector<Z> _fac;
std::vector<Z> _invfac;
std::vector<Z> _inv;
Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
Comb(int n) : Comb() {
init(n);
}
void init(int m) {
m = std::min(m, Z::getMod() - 1);
if (m <= n) return;
_fac.resize(m + 1);
_invfac.resize(m + 1);
_inv.resize(m + 1);
for (int i = n + 1; i <= m; i++) {
_fac[i] = _fac[i - 1] * i;
}
_invfac[m] = _fac[m].inv();
for (int i = m; i > n; i--) {
_invfac[i - 1] = _invfac[i] * i;
_inv[i] = _invfac[i] * _fac[i - 1];
}
n = m;
}
Z fac(int m) {
if (m > n) init(2 * m);
return _fac[m];
}
Z invfac(int m) {
if (m > n) init(2 * m);
return _invfac[m];
}
Z inv(int m) {
if (m > n) init(2 * m);
return _inv[m];
}
Z binom(int n, int m) {
if (n < m || m < 0) return 0;
return fac(n) * invfac(m) * invfac(n - m);
}
} comb;
Poly<P> get(int n, int m) {
if (m == 0) {
return Poly(n + 1);
}
if (m % 2 == 1) {
auto f = get(n, m - 1);
Z p = 1;
for (int i = 0; i <= n; i++) {
f[n - i] += comb.binom(n, i) * p;
p *= m;
}
return f;
}
auto f = get(n, m / 2);
auto fm = f;
for (int i = 0; i <= n; i++) {
fm[i] *= comb.fac(i);
}
Poly pw(n + 1);
pw[0] = 1;
for (int i = 1; i <= n; i++) {
pw[i] = pw[i - 1] * (m / 2);
}
for (int i = 0; i <= n; i++) {
pw[i] *= comb.invfac(i);
}
fm = fm.mulT(pw);
for (int i = 0; i <= n; i++) {
fm[i] *= comb.invfac(i);
}
return f + fm;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M;
std::cin >> N >> M;
Z ans = 0;
ans -= power(Z(M), N) * (M - 1) * N * CInv<2, P>;
auto f = get(N, M - 1);
Z p = 1;
for (int i = 0; i <= N; i++) {
f[i] *= p;
p *= M;
}
std::reverse(f.begin(), f.end());
for (int i = 0; i <= N; i++) {
f[i] *= comb.fac(i);
}
Poly pw(N + 1);
for (int i = 0; i <= N; i++) {
pw[i] = (i % 2 ? -1 : 1) * comb.invfac(i);
}
f = f.mulT(pw);
for (int i = 0; i <= N; i++) {
f[i] *= comb.invfac(i);
}
std::vector<Z> g(N + 1);
for (int i = 0; i < N; i++) {
g[i + 1] = g[i] + comb.binom(N, i);
}
for (int a = 0; a <= N; a++) {
ans += f[a] * std::min(N, 2 * a);
ans -= f[a] * g[std::min(a, N - a)] * comb.fac(a) * comb.fac(N - a) * comb.invfac(N);
}
std::cout << ans << "\n";
return 0;
}
Submission Info
Submission Time
2023-09-20 03:48:29+0900
Task
F - Many Increasing Problems
User
jiangly
Language
C++ 20 (gcc 12.2)
Score
1100
Code Size
16381 Byte
Status
AC
Exec Time
948 ms
Memory
11996 KiB
Compile Error
Main.cpp: In instantiation of ‘constexpr Poly<998244353> operator+(const Poly<998244353>&, const Poly<998244353>&)’:
Main.cpp:563:16: required from here
Main.cpp:230:27: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<MInt<998244353> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
230 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp:233:27: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<MInt<998244353> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
233 | for (int i = 0; i < b.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp: In instantiation of ‘constexpr Poly<998244353> operator*(Poly<998244353>, Poly<998244353>)’:
Main.cpp:384:25: required from ‘constexpr Poly<P> Poly<P>::mulT(Poly<P>) const [with int P = 998244353]’
Main.cpp:559:17: required from here
Main.cpp:268:31: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<MInt<998244353> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
268 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp:269:35: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<MInt<998244353> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
269 | for (int j = 0; j < b.size(); j++) {
| ~~^~~~~~~~~~
Main.cpp: In instantiation of ‘constexpr Poly<P> Poly<P>::shift(int) const [with int P = 998244353]’:
Main.cpp:384:35: required from ‘constexpr Poly<P> Poly<P>::mulT(Poly<P>) const [with int P = 998244353]’
Main.cpp:559:17: required from here
Main.cpp:217:33: warning: comparison of integer expressions of different signedness: ‘std::vector<MInt<998244353> >::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
217 | } else if (this->size() <= -k) {
| ~~~~~~~~~~~~~^~~~~
Main.cpp: In instantiation of ‘constexpr void dft(std::vector<MInt<P> >&) [with int P = 998244353]’:
Main.cpp:277:12: required from ‘constexpr Poly<998244353> operator*(Poly<998244353>, Poly<998244353>)’
Main.cpp:384:25: required from ‘constexpr Poly<P> Poly<P>::mulT(Poly<P>) const [with int P = 998244353]’
Main.cpp:559:17: required from here
Main.cpp:157:25: warning: comparison of integer expressions of different signedness: ‘std::vector<MInt<998244353> >::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
157 | if (roots<P>.size() < n) {
| ~~~~~~~~~~~~~~~~^~~
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
1100 / 1100
Status
Set Name
Test Cases
Sample
example_00.txt, example_01.txt, example_02.txt, example_03.txt
All
example_00.txt, example_01.txt, example_02.txt, example_03.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt
Case Name
Status
Exec Time
Memory
example_00.txt
AC
1 ms
3528 KiB
example_01.txt
AC
1 ms
3468 KiB
example_02.txt
AC
1 ms
3540 KiB
example_03.txt
AC
943 ms
11944 KiB
test_00.txt
AC
90 ms
4376 KiB
test_01.txt
AC
5 ms
3476 KiB
test_02.txt
AC
389 ms
7204 KiB
test_03.txt
AC
399 ms
7740 KiB
test_04.txt
AC
943 ms
11756 KiB
test_05.txt
AC
1 ms
3600 KiB
test_06.txt
AC
944 ms
11876 KiB
test_07.txt
AC
948 ms
11768 KiB
test_08.txt
AC
945 ms
11804 KiB
test_09.txt
AC
944 ms
11804 KiB
test_10.txt
AC
946 ms
11784 KiB
test_11.txt
AC
946 ms
11996 KiB
test_12.txt
AC
947 ms
11872 KiB
test_13.txt
AC
946 ms
11832 KiB
test_14.txt
AC
947 ms
11860 KiB
test_15.txt
AC
946 ms
11896 KiB
test_16.txt
AC
19 ms
6744 KiB
test_17.txt
AC
38 ms
9988 KiB
test_18.txt
AC
39 ms
10160 KiB
test_19.txt
AC
1 ms
3532 KiB
test_20.txt
AC
1 ms
3528 KiB
test_21.txt
AC
1 ms
3452 KiB