QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#105616 | #6411. Classical FFT Problem | maspy | AC ✓ | 328ms | 17172kb | C++23 | 44.1kb | 2023-05-14 15:36:27 | 2023-05-14 15:36:29 |
Judging History
answer
#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name( \
a, vector<vector<vector<type>>>( \
b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)
#define FOR_subset(t, s) \
for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define stoi stoll
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T, typename U>
T ceil(T x, U y) {
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <typename T, typename U>
T SUM(const vector<U> &A) {
T sum = 0;
for (auto &&a: A) sum += a;
return sum;
}
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()
template <typename T>
T POP(deque<T> &que) {
T a = que.front();
que.pop_front();
return a;
}
template <typename T>
T POP(pq<T> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(pqg<T> &que) {
assert(!que.empty());
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
assert(!que.empty());
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (abs(ok - ng) > 1) {
auto x = (ng + ok) / 2;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
}
return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
FOR(iter) {
double x = (ok + ng) / 2;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
}
return (ok + ng) / 2;
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
return (a > b ? a = b, 1 : 0);
}
// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
vc<int> A(S.size());
FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
return A;
}
template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
int N = A.size();
vector<T> B(N + 1);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
if (off == 0) B.erase(B.begin());
return B;
}
// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
vector<int> ids(len(A));
iota(all(ids), 0);
sort(all(ids),
[&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
return ids;
}
// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
vc<T> B(len(I));
FOR(i, len(I)) B[i] = A[I[i]];
return B;
}
#endif
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>
namespace fastio {
#define FASTIO
// クラスが read(), print() を持っているかを判定するメタ関数
struct has_write_impl {
template <class T>
static auto check(T &&x) -> decltype(x.write(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) {
};
struct has_read_impl {
template <class T>
static auto check(T &&x) -> decltype(x.read(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {};
struct Scanner {
FILE *fp;
char line[(1 << 15) + 1];
size_t st = 0, ed = 0;
void reread() {
memmove(line, line + st, ed - st);
ed -= st;
st = 0;
ed += fread(line + ed, 1, (1 << 15) - ed, fp);
line[ed] = '\0';
}
bool succ() {
while (true) {
if (st == ed) {
reread();
if (st == ed) return false;
}
while (st != ed && isspace(line[st])) st++;
if (st != ed) break;
}
if (ed - st <= 50) {
bool sep = false;
for (size_t i = st; i < ed; i++) {
if (isspace(line[i])) {
sep = true;
break;
}
}
if (!sep) reread();
}
return true;
}
template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
while (true) {
size_t sz = 0;
while (st + sz < ed && !isspace(line[st + sz])) sz++;
ref.append(line + st, sz);
st += sz;
if (!sz || st != ed) break;
reread();
}
return true;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
bool neg = false;
if (line[st] == '-') {
neg = true;
st++;
}
ref = T(0);
while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
if (neg) ref = -ref;
return true;
}
template <typename T,
typename enable_if<has_read<T>::value>::type * = nullptr>
inline bool read_single(T &x) {
x.read();
return true;
}
bool read_single(double &ref) {
string s;
if (!read_single(s)) return false;
ref = std::stod(s);
return true;
}
bool read_single(char &ref) {
string s;
if (!read_single(s) || s.size() != 1) return false;
ref = s[0];
return true;
}
template <class T>
bool read_single(vector<T> &ref) {
for (auto &d: ref) {
if (!read_single(d)) return false;
}
return true;
}
template <class T, class U>
bool read_single(pair<T, U> &p) {
return (read_single(p.first) && read_single(p.second));
}
template <size_t N = 0, typename T>
void read_single_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
read_single(x);
read_single_tuple<N + 1>(t);
}
}
template <class... T>
bool read_single(tuple<T...> &tpl) {
read_single_tuple(tpl);
return true;
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
bool f = read_single(h);
assert(f);
read(t...);
}
Scanner(FILE *fp) : fp(fp) {}
};
struct Printer {
Printer(FILE *_fp) : fp(_fp) {}
~Printer() { flush(); }
static constexpr size_t SIZE = 1 << 15;
FILE *fp;
char line[SIZE], small[50];
size_t pos = 0;
void flush() {
fwrite(line, 1, pos, fp);
pos = 0;
}
void write(const char val) {
if (pos == SIZE) flush();
line[pos++] = val;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void write(T val) {
if (pos > (1 << 15) - 50) flush();
if (val == 0) {
write('0');
return;
}
if (val < 0) {
write('-');
val = -val; // todo min
}
size_t len = 0;
while (val) {
small[len++] = char(0x30 | (val % 10));
val /= 10;
}
for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
pos += len;
}
void write(const string s) {
for (char c: s) write(c);
}
void write(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) write(s[i]);
}
void write(const double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
void write(const long double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
template <typename T,
typename enable_if<has_write<T>::value>::type * = nullptr>
inline void write(T x) {
x.write();
}
template <class T>
void write(const vector<T> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
template <class T, class U>
void write(const pair<T, U> val) {
write(val.first);
write(' ');
write(val.second);
}
template <size_t N = 0, typename T>
void write_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { write(' '); }
const auto x = std::get<N>(t);
write(x);
write_tuple<N + 1>(t);
}
}
template <class... T>
bool write(tuple<T...> tpl) {
write_tuple(tpl);
return true;
}
template <class T, size_t S>
void write(const array<T, S> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
void write(i128 val) {
string s;
bool negative = 0;
if (val < 0) {
negative = 1;
val = -val;
}
while (val) {
s += '0' + int(val % 10);
val /= 10;
}
if (negative) s += "-";
reverse(all(s));
if (len(s) == 0) s = "0";
write(s);
}
};
Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);
void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
printer.write(head);
if (sizeof...(Tail)) printer.write(' ');
print(forward<Tail>(tail)...);
}
void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
scanner.read(head);
read(tail...);
}
} // namespace fastio
using fastio::print;
using fastio::flush;
using fastio::read;
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
read(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
read(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
read(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
read(name)
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
read(name)
void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 2 "library/mod/modint_common.hpp"
struct has_mod_impl {
template <class T>
static auto check(T &&x) -> decltype(x.get_mod(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_mod : public decltype(has_mod_impl::check<T>(std::declval<T>())) {};
template <typename mint>
mint inv(int n) {
static const int mod = mint::get_mod();
static vector<mint> dat = {0, 1};
assert(0 <= n);
if (n >= mod) n %= mod;
while (len(dat) <= n) {
int k = len(dat);
int q = (mod + k - 1) / k;
dat.eb(dat[k * q - mod] * mint(q));
}
return dat[n];
}
template <typename mint>
mint fact(int n) {
static const int mod = mint::get_mod();
assert(0 <= n);
if (n >= mod) return 0;
static vector<mint> dat = {1, 1};
while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * mint(len(dat)));
return dat[n];
}
template <typename mint>
mint fact_inv(int n) {
static const int mod = mint::get_mod();
assert(-1 <= n && n < mod);
static vector<mint> dat = {1, 1};
if (n == -1) return mint(0);
while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * inv<mint>(len(dat)));
return dat[n];
}
template <class mint, class... Ts>
mint fact_invs(Ts... xs) {
return (mint(1) * ... * fact_inv<mint>(xs));
}
template <typename mint, class Head, class... Tail>
mint multinomial(Head &&head, Tail &&... tail) {
return fact<mint>(head) * fact_invs<mint>(std::forward<Tail>(tail)...);
}
template <typename mint>
mint C_dense(int n, int k) {
static vvc<mint> C;
static int H = 0, W = 0;
auto calc = [&](int i, int j) -> mint {
if (i == 0) return (j == 0 ? mint(1) : mint(0));
return C[i - 1][j] + (j ? C[i - 1][j - 1] : 0);
};
if (W <= k) {
FOR(i, H) {
C[i].resize(k + 1);
FOR(j, W, k + 1) { C[i][j] = calc(i, j); }
}
W = k + 1;
}
if (H <= n) {
C.resize(n + 1);
FOR(i, H, n + 1) {
C[i].resize(W);
FOR(j, W) { C[i][j] = calc(i, j); }
}
H = n + 1;
}
return C[n][k];
}
template <typename mint, bool large = false, bool dense = false>
mint C(ll n, ll k) {
assert(n >= 0);
if (k < 0 || n < k) return 0;
if (dense) return C_dense<mint>(n, k);
if (!large) return multinomial<mint>(n, k, n - k);
k = min(k, n - k);
mint x(1);
FOR(i, k) x *= mint(n - i);
return x * fact_inv<mint>(k);
}
template <typename mint, bool large = false>
mint C_inv(ll n, ll k) {
assert(n >= 0);
assert(0 <= k && k <= n);
if (!large) return fact_inv<mint>(n) * fact<mint>(k) * fact<mint>(n - k);
return mint(1) / C<mint, 1>(n, k);
}
// [x^d] (1-x) ^ {-n} の計算
template <typename mint, bool large = false, bool dense = false>
mint C_negative(ll n, ll d) {
assert(n >= 0);
if (d < 0) return mint(0);
if (n == 0) { return (d == 0 ? mint(1) : mint(0)); }
return C<mint, large, dense>(n + d - 1, d);
}
#line 3 "library/mod/modint.hpp"
template <int mod>
struct modint {
static_assert(mod < (1 << 30));
int val;
constexpr modint(const ll val = 0) noexcept
: val(val >= 0 ? val % mod : (mod - (-val) % mod) % mod) {}
bool operator<(const modint &other) const {
return val < other.val;
} // To use std::map
modint &operator+=(const modint &p) {
if ((val += p.val) >= mod) val -= mod;
return *this;
}
modint &operator-=(const modint &p) {
if ((val += mod - p.val) >= mod) val -= mod;
return *this;
}
modint &operator*=(const modint &p) {
val = (int)(1LL * val * p.val % mod);
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint(-val); }
modint operator+(const modint &p) const { return modint(*this) += p; }
modint operator-(const modint &p) const { return modint(*this) -= p; }
modint operator*(const modint &p) const { return modint(*this) *= p; }
modint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const modint &p) const { return val == p.val; }
bool operator!=(const modint &p) const { return val != p.val; }
modint inverse() const {
int a = val, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b), swap(u -= t * v, v);
}
return modint(u);
}
modint pow(ll n) const {
assert(n >= 0);
modint ret(1), mul(val);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
#ifdef FASTIO
void write() { fastio::printer.write(val); }
void read() { fastio::scanner.read(val); }
#endif
static constexpr int get_mod() { return mod; }
// (n, r), r は 1 の 2^n 乗根
static constexpr pair<int, int> ntt_info() {
if (mod == 167772161) return {25, 17};
if (mod == 469762049) return {26, 30};
if (mod == 754974721) return {24, 362};
if (mod == 880803841) return {23, 211};
if (mod == 998244353) return {23, 31};
if (mod == 1045430273) return {20, 363};
if (mod == 1051721729) return {20, 330};
if (mod == 1053818881) return {20, 2789};
return {-1, -1};
}
static constexpr bool can_ntt() { return ntt_info().fi != -1; }
};
using modint107 = modint<1000000007>;
using modint998 = modint<998244353>;
#line 2 "library/poly/convolution_all.hpp"
#line 2 "library/mod/mod_inv.hpp"
// long でも大丈夫
ll mod_inv(ll val, ll mod) {
val %= mod;
if (val < 0) val += mod;
ll a = val, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b), swap(u -= t * v, v);
}
if (u < 0) u += mod;
return u;
}
#line 1 "library/poly/convolution_naive.hpp"
template <class T>
vector<T> convolution_naive(const vector<T>& a, const vector<T>& b) {
int n = int(a.size()), m = int(b.size());
vector<T> ans(n + m - 1);
if (n < m) {
FOR(j, m) FOR(i, n) ans[i + j] += a[i] * b[j];
} else {
FOR(i, n) FOR(j, m) ans[i + j] += a[i] * b[j];
}
return ans;
}
#line 2 "library/poly/ntt.hpp"
template <class mint>
void ntt(vector<mint>& a, bool inverse) {
assert(mint::can_ntt());
const int rank2 = mint::ntt_info().fi;
const int mod = mint::get_mod();
static array<mint, 30> root, iroot;
static array<mint, 30> rate2, irate2;
static array<mint, 30> rate3, irate3;
static bool prepared = 0;
if (!prepared) {
prepared = 1;
root[rank2] = mint::ntt_info().se;
iroot[rank2] = mint(1) / root[rank2];
FOR_R(i, rank2) {
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];
}
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];
}
}
int n = int(a.size());
int h = topbit(n);
assert(n == 1 << h);
if (!inverse) {
int len = 0;
while (len < h) {
if (h - len == 1) {
int p = 1 << (h - len - 1);
mint rot = 1;
FOR(s, 1 << len) {
int offset = s << (h - len);
FOR(i, p) {
auto l = a[i + offset];
auto r = a[i + offset + p] * rot;
a[i + offset] = l + r;
a[i + offset + p] = l - r;
}
rot *= rate2[topbit(~s & -~s)];
}
len++;
} else {
int p = 1 << (h - len - 2);
mint rot = 1, imag = 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++) {
u64 mod2 = u64(mod) * mod;
u64 a0 = a[i + offset].val;
u64 a1 = u64(a[i + offset + p].val) * rot.val;
u64 a2 = u64(a[i + offset + 2 * p].val) * rot2.val;
u64 a3 = u64(a[i + offset + 3 * p].val) * rot3.val;
u64 a1na3imag = (a1 + mod2 - a3) % mod * imag.val;
u64 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);
}
rot *= rate3[topbit(~s & -~s)];
}
len += 2;
}
}
} else {
mint coef = mint(1) / mint(len(a));
FOR(i, len(a)) a[i] *= coef;
int len = h;
while (len) {
if (len == 1) {
int p = 1 << (h - len);
mint irot = 1;
FOR(s, 1 << (len - 1)) {
int offset = s << (h - len + 1);
FOR(i, p) {
u64 l = a[i + offset].val;
u64 r = a[i + offset + p].val;
a[i + offset] = l + r;
a[i + offset + p] = (mod + l - r) * irot.val;
}
irot *= irate2[topbit(~s & -~s)];
}
len--;
} else {
int p = 1 << (h - len);
mint irot = 1, iimag = iroot[2];
FOR(s, (1 << (len - 2))) {
mint irot2 = irot * irot;
mint irot3 = irot2 * irot;
int offset = s << (h - len + 2);
for (int i = 0; i < p; i++) {
u64 a0 = a[i + offset + 0 * p].val;
u64 a1 = a[i + offset + 1 * p].val;
u64 a2 = a[i + offset + 2 * p].val;
u64 a3 = a[i + offset + 3 * p].val;
u64 x = (mod + a2 - a3) * iimag.val % mod;
a[i + offset] = a0 + a1 + a2 + a3;
a[i + offset + 1 * p] = (a0 + mod - a1 + x) * irot.val;
a[i + offset + 2 * p] = (a0 + a1 + 2 * mod - a2 - a3) * irot2.val;
a[i + offset + 3 * p] = (a0 + 2 * mod - a1 - x) * irot3.val;
}
irot *= irate3[topbit(~s & -~s)];
}
len -= 2;
}
}
}
}
#line 1 "library/poly/fft.hpp"
namespace CFFT {
using real = double;
struct C {
real x, y;
C() : x(0), y(0) {}
C(real x, real y) : x(x), y(y) {}
inline C operator+(const C& c) const { return C(x + c.x, y + c.y); }
inline C operator-(const C& c) const { return C(x - c.x, y - c.y); }
inline C operator*(const C& c) const {
return C(x * c.x - y * c.y, x * c.y + y * c.x);
}
inline C conj() const { return C(x, -y); }
};
const real PI = acosl(-1);
int base = 1;
vector<C> rts = {{0, 0}, {1, 0}};
vector<int> rev = {0, 1};
void ensure_base(int nbase) {
if (nbase <= base) return;
rev.resize(1 << nbase);
rts.resize(1 << nbase);
for (int i = 0; i < (1 << nbase); i++) {
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
}
while (base < nbase) {
real angle = PI * 2.0 / (1 << (base + 1));
for (int i = 1 << (base - 1); i < (1 << base); i++) {
rts[i << 1] = rts[i];
real angle_i = angle * (2 * i + 1 - (1 << base));
rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i));
}
++base;
}
}
void fft(vector<C>& a, int n) {
assert((n & (n - 1)) == 0);
int zeros = __builtin_ctz(n);
ensure_base(zeros);
int shift = base - zeros;
for (int i = 0; i < n; i++) {
if (i < (rev[i] >> shift)) { swap(a[i], a[rev[i] >> shift]); }
}
for (int k = 1; k < n; k <<= 1) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
C z = a[i + j + k] * rts[j + k];
a[i + j + k] = a[i + j] - z;
a[i + j] = a[i + j] + z;
}
}
}
}
} // namespace CFFT
#line 7 "library/poly/convolution.hpp"
template <class mint>
vector<mint> convolution_ntt(vector<mint> a, vector<mint> b) {
if (a.empty() || b.empty()) return {};
int n = int(a.size()), m = int(b.size());
int sz = 1;
while (sz < n + m - 1) sz *= 2;
// sz = 2^k のときの高速化。分割統治的なやつで損しまくるので。
if ((n + m - 3) <= sz / 2) {
auto a_last = a.back(), b_last = b.back();
a.pop_back(), b.pop_back();
auto c = convolution(a, b);
c.resize(n + m - 1);
c[n + m - 2] = a_last * b_last;
FOR(i, len(a)) c[i + len(b)] += a[i] * b_last;
FOR(i, len(b)) c[i + len(a)] += b[i] * a_last;
return c;
}
a.resize(sz), b.resize(sz);
bool same = a == b;
ntt(a, 0);
if (same) {
b = a;
} else {
ntt(b, 0);
}
FOR(i, sz) a[i] *= b[i];
ntt(a, 1);
a.resize(n + m - 1);
return a;
}
template <typename mint>
vector<mint> convolution_garner(const vector<mint>& a, const vector<mint>& b) {
int n = len(a), m = len(b);
if (!n || !m) return {};
static const long long nttprimes[] = {754974721, 167772161, 469762049};
using mint0 = modint<754974721>;
using mint1 = modint<167772161>;
using mint2 = modint<469762049>;
vc<mint0> a0(n), b0(m);
vc<mint1> a1(n), b1(m);
vc<mint2> a2(n), b2(m);
FOR(i, n) a0[i] = a[i].val, a1[i] = a[i].val, a2[i] = a[i].val;
FOR(i, m) b0[i] = b[i].val, b1[i] = b[i].val, b2[i] = b[i].val;
auto c0 = convolution_ntt<mint0>(a0, b0);
auto c1 = convolution_ntt<mint1>(a1, b1);
auto c2 = convolution_ntt<mint2>(a2, b2);
static const long long m01 = 1LL * nttprimes[0] * nttprimes[1];
static const long long m0_inv_m1 = mint1(nttprimes[0]).inverse().val;
static const long long m01_inv_m2 = mint2(m01).inverse().val;
static const int mod = mint::get_mod();
auto garner = [&](mint0 x0, mint1 x1, mint2 x2) -> mint {
int r0 = x0.val, r1 = x1.val, r2 = x2.val;
int v1 = (m0_inv_m1 * (r1 + nttprimes[1] - r0)) % nttprimes[1];
auto v2 = (mint2(r2) - r0 - mint2(nttprimes[0]) * v1) * mint2(m01_inv_m2);
return mint(r0 + 1LL * nttprimes[0] * v1 + m01 % mod * v2.val);
};
vc<mint> c(len(c0));
FOR(i, len(c)) c[i] = garner(c0[i], c1[i], c2[i]);
return c;
}
template <typename R>
vc<double> convolution_fft(const vc<R>& a, const vc<R>& b) {
using C = CFFT::C;
int need = (int)a.size() + (int)b.size() - 1;
int nbase = 1;
while ((1 << nbase) < need) nbase++;
CFFT::ensure_base(nbase);
int sz = 1 << nbase;
vector<C> fa(sz);
for (int i = 0; i < sz; i++) {
int x = (i < (int)a.size() ? a[i] : 0);
int y = (i < (int)b.size() ? b[i] : 0);
fa[i] = C(x, y);
}
CFFT::fft(fa, sz);
C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0);
for (int i = 0; i <= (sz >> 1); i++) {
int j = (sz - i) & (sz - 1);
C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r;
fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r;
fa[i] = z;
}
for (int i = 0; i < (sz >> 1); i++) {
C A0 = (fa[i] + fa[i + (sz >> 1)]) * t;
C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * CFFT::rts[(sz >> 1) + i];
fa[i] = A0 + A1 * s;
}
CFFT::fft(fa, sz >> 1);
vector<double> ret(need);
for (int i = 0; i < need; i++) {
ret[i] = (i & 1 ? fa[i >> 1].y : fa[i >> 1].x);
}
return ret;
}
vector<ll> convolution(const vector<ll>& a, const vector<ll>& b) {
int n = len(a), m = len(b);
if (!n || !m) return {};
if (min(n, m) <= 60) return convolution_naive(a, b);
ll abs_sum_a = 0, abs_sum_b = 0;
ll LIM = 1e15;
FOR(i, n) abs_sum_a = min(LIM, abs_sum_a + abs(a[i]));
FOR(i, n) abs_sum_b = min(LIM, abs_sum_b + abs(b[i]));
if (i128(abs_sum_a) * abs_sum_b < 1e15) {
vc<double> c = convolution_fft<ll>(a, b);
vc<ll> res(len(c));
FOR(i, len(c)) res[i] = ll(floor(c[i] + .5));
return res;
}
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 const unsigned long long i1 = mod_inv(MOD2 * MOD3, MOD1);
static const unsigned long long i2 = mod_inv(MOD1 * MOD3, MOD2);
static const unsigned long long i3 = mod_inv(MOD1 * MOD2, MOD3);
using mint1 = modint<MOD1>;
using mint2 = modint<MOD2>;
using mint3 = modint<MOD3>;
vc<mint1> a1(n), b1(m);
vc<mint2> a2(n), b2(m);
vc<mint3> a3(n), b3(m);
FOR(i, n) a1[i] = a[i], a2[i] = a[i], a3[i] = a[i];
FOR(i, m) b1[i] = b[i], b2[i] = b[i], b3[i] = b[i];
auto c1 = convolution_ntt<mint1>(a1, b1);
auto c2 = convolution_ntt<mint2>(a2, b2);
auto c3 = convolution_ntt<mint3>(a3, b3);
vc<ll> c(n + m - 1);
FOR(i, n + m - 1) {
u64 x = 0;
x += (c1[i].val * i1) % MOD1 * M2M3;
x += (c2[i].val * i2) % MOD2 * M1M3;
x += (c3[i].val * i3) % MOD3 * M1M2;
ll diff = c1[i].val - ((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;
}
template <typename mint>
vc<mint> convolution(const vc<mint>& a, const vc<mint>& b) {
int n = len(a), m = len(b);
if (!n || !m) return {};
if (mint::can_ntt()) {
if (min(n, m) <= 50) return convolution_naive(a, b);
return convolution_ntt(a, b);
}
if (min(n, m) <= 200) return convolution_naive(a, b);
return convolution_garner(a, b);
}
#line 4 "library/poly/convolution_all.hpp"
template <typename T>
vc<T> convolution_all(vc<vc<T>>& polys) {
if (len(polys) == 0) return {T(1)};
while (1) {
int n = len(polys);
if (n == 1) break;
int m = ceil(n, 2);
FOR(i, m) {
if (2 * i + 1 == n) {
polys[i] = polys[2 * i];
} else {
polys[i] = convolution(polys[2 * i], polys[2 * i + 1]);
}
}
polys.resize(m);
}
return polys[0];
}
#line 2 "library/poly/count_terms.hpp"
template<typename mint>
int count_terms(const vc<mint>& f){
int t = 0;
FOR(i, len(f)) if(f[i] != mint(0)) ++t;
return t;
}
#line 2 "library/poly/integrate.hpp"
template <typename mint>
vc<mint> integrate(const vc<mint>& f) {
vc<mint> g(len(f) + 1);
FOR3(i, 1, len(g)) g[i] = f[i - 1] * inv<mint>(i);
return g;
}
#line 2 "library/poly/differentiate.hpp"
template <typename mint>
vc<mint> differentiate(const vc<mint>& f) {
if (len(f) <= 1) return {};
vc<mint> g(len(f) - 1);
FOR(i, len(g)) g[i] = f[i + 1] * mint(i + 1);
return g;
}
#line 6 "library/poly/fps_exp.hpp"
template <typename mint>
vc<mint> fps_exp_sparse(vc<mint>& f) {
if (len(f) == 0) return {mint(1)};
assert(f[0] == 0);
int N = len(f);
// df を持たせる
vc<pair<int, mint>> dat;
FOR(i, 1, N) if (f[i] != mint(0)) dat.eb(i - 1, mint(i) * f[i]);
vc<mint> F(N);
F[0] = 1;
FOR(n, 1, N) {
mint rhs = 0;
for (auto&& [k, fk]: dat) {
if (k > n - 1) break;
rhs += fk * F[n - 1 - k];
}
F[n] = rhs * inv<mint>(n);
}
return F;
}
template <typename mint>
vc<mint> fps_exp_dense(vc<mint>& h) {
const int n = len(h);
assert(n > 0 && h[0] == mint(0));
if (mint::can_ntt()) {
vc<mint>& f = h;
vc<mint> b = {1, (1 < n ? f[1] : 0)};
vc<mint> c = {1}, z1, z2 = {1, 1};
while (len(b) < n) {
int m = len(b);
auto y = b;
y.resize(2 * m);
ntt(y, 0);
z1 = z2;
vc<mint> z(m);
FOR(i, m) z[i] = y[i] * z1[i];
ntt(z, 1);
FOR(i, m / 2) z[i] = 0;
ntt(z, 0);
FOR(i, m) z[i] *= -z1[i];
ntt(z, 1);
c.insert(c.end(), z.begin() + m / 2, z.end());
z2 = c;
z2.resize(2 * m);
ntt(z2, 0);
vc<mint> x(f.begin(), f.begin() + m);
FOR(i, len(x) - 1) x[i] = x[i + 1] * mint(i + 1);
x.back() = 0;
ntt(x, 0);
FOR(i, m) x[i] *= y[i];
ntt(x, 1);
FOR(i, m - 1) x[i] -= b[i + 1] * mint(i + 1);
x.resize(m + m);
FOR(i, m - 1) x[m + i] = x[i], x[i] = 0;
ntt(x, 0);
FOR(i, m + m) x[i] *= z2[i];
ntt(x, 1);
FOR_R(i, len(x) - 1) x[i + 1] = x[i] * inv<mint>(i + 1);
x[0] = 0;
FOR3(i, m, min(n, m + m)) x[i] += f[i];
FOR(i, m) x[i] = 0;
ntt(x, 0);
FOR(i, m + m) x[i] *= y[i];
ntt(x, 1);
b.insert(b.end(), x.begin() + m, x.end());
}
b.resize(n);
return b;
}
const int L = len(h);
assert(L > 0 && h[0] == mint(0));
int LOG = 0;
while (1 << LOG < L) ++LOG;
h.resize(1 << LOG);
auto dh = differentiate(h);
vc<mint> f = {1}, g = {1};
int m = 1;
vc<mint> p;
FOR(LOG) {
p = convolution(f, g);
p.resize(m);
p = convolution(p, g);
p.resize(m);
g.resize(m);
FOR(i, m) g[i] += g[i] - p[i];
p = {dh.begin(), dh.begin() + m - 1};
p = convolution(f, p);
p.resize(m + m - 1);
FOR(i, m + m - 1) p[i] = -p[i];
FOR(i, m - 1) p[i] += mint(i + 1) * f[i + 1];
p = convolution(p, g);
p.resize(m + m - 1);
FOR(i, m - 1) p[i] += dh[i];
p = integrate(p);
FOR(i, m + m) p[i] = h[i] - p[i];
p[0] += mint(1);
f = convolution(f, p);
f.resize(m + m);
m += m;
}
f.resize(L);
return f;
}
template <typename mint>
vc<mint> fps_exp(vc<mint>& f) {
int n = count_terms(f);
int t = (mint::can_ntt() ? 320 : 3000);
return (n <= t ? fps_exp_sparse<mint>(f) : fps_exp_dense<mint>(f));
}
#line 2 "library/poly/fps_log.hpp"
#line 4 "library/poly/fps_inv.hpp"
template <typename mint>
vc<mint> fps_inv_sparse(const vc<mint>& f) {
int N = len(f);
vc<pair<int, mint>> dat;
FOR(i, 1, N) if (f[i] != mint(0)) dat.eb(i, f[i]);
vc<mint> g(N);
mint g0 = mint(1) / f[0];
g[0] = g0;
FOR(n, 1, N) {
mint rhs = 0;
for (auto&& [k, fk]: dat) {
if (k > n) break;
rhs -= fk * g[n - k];
}
g[n] = rhs * g0;
}
return g;
}
template <typename mint>
vc<mint> fps_inv_dense_ntt(const vc<mint>& F) {
vc<mint> G = {mint(1) / F[0]};
ll N = len(F), n = 1;
G.reserve(N);
while (n < N) {
vc<mint> f(2 * n), g(2 * n);
FOR(i, min(N, 2 * n)) f[i] = F[i];
FOR(i, n) g[i] = G[i];
ntt(f, false), ntt(g, false);
FOR(i, 2 * n) f[i] *= g[i];
ntt(f, true);
FOR(i, n) f[i] = 0;
ntt(f, false);
FOR(i, 2 * n) f[i] *= g[i];
ntt(f, true);
FOR(i, n, min(N, 2 * n)) G.eb(-f[i]);
n *= 2;
}
return G;
}
template <typename mint>
vc<mint> fps_inv_dense(const vc<mint>& F) {
if (mint::can_ntt()) return fps_inv_dense_ntt(F);
const int N = len(F);
vc<mint> R = {mint(1) / F[0]};
vc<mint> p;
int m = 1;
while (m < N) {
p = convolution(R, R);
p.resize(m + m);
vc<mint> f = {F.begin(), F.begin() + min(m + m, N)};
p = convolution(p, f);
R.resize(m + m);
FOR(i, m + m) R[i] = R[i] + R[i] - p[i];
m += m;
}
R.resize(N);
return R;
}
template <typename mint>
vc<mint> fps_inv(const vc<mint>& f) {
assert(f[0] != mint(0));
int n = count_terms(f);
int t = (mint::can_ntt() ? 160 : 820);
return (n <= t ? fps_inv_sparse<mint>(f) : fps_inv_dense<mint>(f));
}
#line 5 "library/poly/fps_log.hpp"
template <typename mint>
vc<mint> fps_log_dense(const vc<mint>& f) {
assert(f[0] == mint(1));
ll N = len(f);
vc<mint> df = f;
FOR(i, N) df[i] *= mint(i);
df.erase(df.begin());
auto f_inv = fps_inv(f);
auto g = convolution(df, f_inv);
g.resize(N - 1);
g.insert(g.begin(), 0);
FOR(i, N) g[i] *= inv<mint>(i);
return g;
}
template <typename mint>
vc<mint> fps_log_sparse(const vc<mint>& f) {
int N = f.size();
vc<pair<int, mint>> dat;
FOR(i, 1, N) if (f[i] != mint(0)) dat.eb(i, f[i]);
vc<mint> F(N);
vc<mint> g(N - 1);
for (int n = 0; n < N - 1; ++n) {
mint rhs = mint(n + 1) * f[n + 1];
for (auto&& [i, fi]: dat) {
if (i > n) break;
rhs -= fi * g[n - i];
}
g[n] = rhs;
F[n + 1] = rhs * inv<mint>(n + 1);
}
return F;
}
template <typename mint>
vc<mint> fps_log(const vc<mint>& f) {
assert(f[0] == mint(1));
int n = count_terms(f);
int t = (mint::can_ntt() ? 200 : 1200);
return (n <= t ? fps_log_sparse<mint>(f) : fps_log_dense<mint>(f));
}
#line 5 "library/poly/fps_pow.hpp"
// fps の k 乗を求める。k >= 0 の前提である。
// 定数項が 1 で、k が mint の場合には、fps_pow_1 を使うこと。
// ・dense な場合: log, exp を使う O(NlogN)
// ・sparse な場合: O(NK)
template <typename mint>
vc<mint> fps_pow(const vc<mint>& f, ll k) {
assert(0 <= k);
int n = len(f);
if (k == 0) {
vc<mint> g(n);
g[0] = mint(1);
return g;
}
int d = n;
FOR_R(i, n) if (f[i] != 0) d = i;
// d * k >= n
if (d >= ceil(n, k)) {
vc<mint> g(n);
return g;
}
ll off = d * k;
mint c = f[d];
mint c_inv = mint(1) / mint(c);
vc<mint> g(n - off);
FOR(i, n - off) g[i] = f[d + i] * c_inv;
g = fps_pow_1(g, mint(k));
vc<mint> h(n);
c = c.pow(k);
FOR(i, len(g)) h[off + i] = g[i] * c;
return h;
}
template <typename mint>
vc<mint> fps_pow_1_sparse(const vc<mint>& f, mint K) {
int N = len(f);
vc<pair<int, mint>> dat;
FOR(i, 1, N) if (f[i] != mint(0)) dat.eb(i, f[i]);
vc<mint> g(N);
g[0] = 1;
FOR(n, N - 1) {
mint& x = g[n + 1];
for (auto&& [d, cf]: dat) {
if (d > n + 1) break;
mint t = cf * g[n - d + 1];
x += t * (K * mint(d) - mint(n - d + 1));
}
x *= inv<mint>(n + 1);
}
return g;
}
template <typename mint>
vc<mint> fps_pow_1_dense(const vc<mint>& f, mint K) {
assert(f[0] == mint(1));
auto log_f = fps_log(f);
FOR(i, len(f)) log_f[i] *= K;
return fps_exp_dense(log_f);
}
template <typename mint>
vc<mint> fps_pow_1(const vc<mint>& f, mint K) {
int n = count_terms(f);
int t = (mint::can_ntt() ? 100 : 1300);
return (n <= t ? fps_pow_1_sparse(f, K) : fps_pow_1_dense(f, K));
}
// f^e, sparse, O(NMK)
template <typename mint>
vvc<mint> fps_pow_1_sparse_2d(vvc<mint> f, mint n) {
assert(f[0][0] == mint(1));
int N = len(f), M = len(f[0]);
vv(mint, dp, N, M);
dp[0] = fps_pow_1_sparse<mint>(f[0], n);
vc<tuple<int, int, mint>> dat;
FOR(i, N) FOR(j, M) {
if ((i > 0 || j > 0) && f[i][j] != mint(0)) dat.eb(i, j, f[i][j]);
}
FOR(i, 1, N) {
FOR(j, M) {
// F = f^n, f dF = n df F
// [x^{i-1}y^j]
mint lhs = 0, rhs = 0;
for (auto&& [a, b, c]: dat) {
if (a < i && b <= j) lhs += dp[i - a][j - b] * mint(i - a);
if (a <= i && b <= j) rhs += dp[i - a][j - b] * c * mint(a);
}
dp[i][j] = (n * rhs - lhs) * inv<mint>(i);
}
}
return dp;
}
#line 2 "library/nt/primetable.hpp"
template <typename T = long long>
vc<T> primetable(int LIM) {
++LIM;
const int S = 32768;
static int done = 2;
static vc<T> primes = {2}, sieve(S + 1);
if (done < LIM) {
done = LIM;
primes = {2}, sieve.assign(S + 1, 0);
const int R = LIM / 2;
primes.reserve(int(LIM / log(LIM) * 1.1));
vc<pair<int, int>> cp;
for (int i = 3; i <= S; i += 2) {
if (!sieve[i]) {
cp.eb(i, i * i / 2);
for (int j = i * i; j <= S; j += 2 * i) sieve[j] = 1;
}
}
for (int L = 1; L <= R; L += S) {
array<bool, S> block{};
for (auto& [p, idx]: cp)
for (int i = idx; i < S + L; idx = (i += p)) block[i - L] = 1;
FOR(i, min(S, R - L)) if (!block[i]) primes.eb((L + i) * 2 + 1);
}
}
int k = LB(primes, LIM + 1);
return {primes.begin(), primes.begin() + k};
}
#line 3 "library/mod/powertable.hpp"
// a^0, ..., a^N
template <typename mint>
vc<mint> powertable_1(mint a, ll N) {
// table of a^i
vc<mint> f(N + 1, 1);
FOR(i, N) f[i + 1] = a * f[i];
return f;
}
// 0^e, ..., N^e
template <typename mint>
vc<mint> powertable_2(ll e, ll N) {
auto primes = primetable(N);
vc<mint> f(N + 1, 1);
f[0] = mint(0).pow(e);
for (auto&& p: primes) {
if (p > N) break;
mint xp = mint(p).pow(e);
ll pp = p;
while (pp <= N) {
ll i = pp;
while (i <= N) {
f[i] *= xp;
i += pp;
}
pp *= p;
}
}
return f;
}
#line 4 "library/seq/famous/stirling_number_2.hpp"
// n 個のもの (labeled) を k グループ (no label) に分ける方法
// label をつけることで、全射の数え上げに利用できる
template <typename mint>
vvc<mint> stirling_number_2_2d(int nmax, int kmax) {
vv(mint, A, nmax + 1, kmax + 1);
A[0][0] = 1;
FOR(i, 1, nmax + 1) {
FOR(j, i + 1) {
if (j > kmax) break;
if (j) A[i][j] += A[i - 1][j - 1];
if (j < i) A[i][j] += A[i - 1][j] * mint(j);
}
}
return A;
}
// n 個のもの (labeled) を k グループ (no label) に分ける方法
// label をつけることで、全射の数え上げに利用できる
template <typename mint>
vc<mint> stirling_number_2_n(int n, int k_max) {
vc<mint> a = powertable_2<mint>(n, k_max + 1);
FOR(i, k_max + 1) a[i] *= fact_inv<mint>(i);
vc<mint> b(k_max + 1);
FOR(i, k_max + 1) b[i] = fact_inv<mint>(i);
FOR(i, 1, k_max + 1, 2) b[i] = -b[i];
auto f = convolution(a, b);
f.resize(k_max + 1);
return f;
}
// n 個のもの (labeled) を k グループ (no label) に分ける方法
// label をつけることで、全射の数え上げに利用できる
template <typename mint>
vc<mint> stirling_number_2_k(int k, int n_max) {
if (k > n_max) { return vc<mint>(n_max + 1); }
int LIM = n_max - k;
vc<mint> f(LIM + 1);
FOR(i, LIM + 1) f[i] = fact_inv<mint>(i + 1);
f = fps_pow(f, k);
mint cf = fact_inv<mint>(k);
vc<mint> res(n_max + 1);
FOR(i, len(f)) res[k + i] = fact<mint>(k + i) * f[i] * cf;
return res;
}
#line 2 "library/seq/famous/surjection.hpp"
// n 元集合からの全射の数え上げ
template <typename mint>
vc<mint> surjection_n(int n, int k_max) {
auto f = stirling_number_2_n<mint>(n, k_max);
FOR(i, k_max + 1) f[i] *= fact<mint>(i);
return f;
}
// k 元集合へのの全射の数え上げ
template <typename mint>
vc<mint> surjection_k(int k, int n_max) {
auto f = stirling_number_2_k<mint>(k, n_max);
FOR(i, n_max + 1) f[i] *= fact<mint>(k);
return f;
}
#line 6 "main.cpp"
using mint = modint998;
using poly = vc<mint>;
void solve() {
LL(N);
VEC(int, A, N);
reverse(all(A));
int S = 0;
FOR(i, N) if (A[i] >= 1 + i) S = 1 + i;
mint ANS = 0;
FOR(2) {
vc<int> B(N + 1);
for (auto&& x: A) B[x]++;
FOR_R(i, N) B[i] += B[i + 1];
B.erase(B.begin());
swap(A, B);
ll need = 0;
FOR(i, S) if (B[i] > S)++ need;
vc<poly> polys;
FOR(i, S) { polys.eb(poly{mint(1), mint(A[i] - need)}); }
poly f = convolution_all(polys);
poly surj = surjection_k<mint>(need, S);
FOR(k, need, S + 1) { ANS += surj[k] * f[S - k]; }
}
ANS -= fact<mint>(S);
print(S, ANS);
}
signed main() {
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3548kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3552kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3564kb
input:
2 1 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #4:
score: 0
Accepted
time: 2ms
memory: 3632kb
input:
2 2 2
output:
2 6
result:
ok 2 number(s): "2 6"
Test #5:
score: 0
Accepted
time: 2ms
memory: 3628kb
input:
3 1 1 1
output:
1 3
result:
ok 2 number(s): "1 3"
Test #6:
score: 0
Accepted
time: 2ms
memory: 3568kb
input:
3 2 2 2
output:
2 9
result:
ok 2 number(s): "2 9"
Test #7:
score: 0
Accepted
time: 2ms
memory: 3616kb
input:
3 3 3 3
output:
3 48
result:
ok 2 number(s): "3 48"
Test #8:
score: 0
Accepted
time: 2ms
memory: 3428kb
input:
5 1 1 3 3 4
output:
3 47
result:
ok 2 number(s): "3 47"
Test #9:
score: 0
Accepted
time: 1ms
memory: 3376kb
input:
10 2 4 5 5 5 5 6 8 8 10
output:
5 864
result:
ok 2 number(s): "5 864"
Test #10:
score: 0
Accepted
time: 2ms
memory: 3428kb
input:
30 6 8 9 9 9 10 13 14 15 15 16 17 17 18 20 22 22 23 23 24 24 25 25 25 27 28 28 29 29 30
output:
17 986189864
result:
ok 2 number(s): "17 986189864"
Test #11:
score: 0
Accepted
time: 1ms
memory: 3440kb
input:
123 1 1 1 2 2 3 3 6 6 7 7 7 8 8 9 9 10 10 10 11 12 12 12 13 14 14 14 14 16 17 17 17 17 17 18 19 20 20 21 21 22 22 22 23 23 23 25 25 26 27 27 28 28 28 28 29 29 30 31 31 31 32 33 33 33 34 35 35 35 36 37 37 38 39 39 39 39 40 41 41 42 42 42 43 44 48 48 50 52 53 55 56 57 57 57 58 65 68 71 74 75 76 76 82 ...
output:
42 287179924
result:
ok 2 number(s): "42 287179924"
Test #12:
score: 0
Accepted
time: 2ms
memory: 3592kb
input:
1234 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 6 6 7 7 7 7 7 7 7 8 8 8 8 9 9 10 10 10 11 11 11 11 11 12 13 13 14 14 15 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 19 19 19 19 19 19 19 20 20 20 21 21 21 21 21 22 22 22 23 23 23 23 23 23 23 23 23 24 24 24 24 24 24 24 24 24 24 25 25 25 25 25 26 26 26 26 ...
output:
239 98119841
result:
ok 2 number(s): "239 98119841"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3740kb
input:
2345 1 1 2 2 2 7 7 9 9 9 9 15 17 19 19 22 23 24 25 29 29 29 30 31 32 33 35 37 39 41 42 42 43 43 44 46 46 46 47 48 48 50 51 51 52 53 53 54 55 56 57 58 58 60 61 63 63 64 65 65 65 66 67 67 67 69 69 69 70 71 72 72 73 73 74 75 75 77 77 79 83 85 86 88 90 90 91 93 94 97 99 104 106 107 108 108 109 109 110 1...
output:
1239 588926916
result:
ok 2 number(s): "1239 588926916"
Test #14:
score: 0
Accepted
time: 4ms
memory: 3696kb
input:
3456 4 7 8 8 9 19 20 21 22 23 23 27 29 29 32 32 33 43 45 50 52 52 55 58 58 58 60 62 66 67 68 69 71 74 74 76 77 79 82 82 87 87 88 91 93 95 96 97 99 102 104 106 107 108 121 121 123 126 127 131 137 138 139 142 145 147 152 156 157 159 161 165 166 170 170 172 174 175 178 182 183 185 186 189 190 195 195 1...
output:
2239 24387925
result:
ok 2 number(s): "2239 24387925"
Test #15:
score: 0
Accepted
time: 2ms
memory: 3964kb
input:
4456 4 7 10 10 22 24 29 33 33 34 35 37 40 41 47 48 55 61 61 65 69 71 76 91 95 99 105 105 105 110 112 113 117 117 120 121 122 123 125 127 130 134 135 138 140 141 142 142 144 150 153 154 157 162 165 169 170 170 174 175 176 178 197 198 198 201 208 211 211 212 214 214 215 217 220 224 224 225 230 231 232...
output:
3239 904395650
result:
ok 2 number(s): "3239 904395650"
Test #16:
score: 0
Accepted
time: 7ms
memory: 4048kb
input:
5000 1 5 7 8 24 28 36 47 50 56 59 64 66 85 89 94 95 95 98 108 110 117 122 155 157 158 163 172 172 179 186 197 198 220 236 251 254 254 256 265 287 288 298 302 306 312 327 336 343 344 345 348 350 360 363 364 382 382 390 399 402 406 412 421 425 435 442 445 450 451 453 478 481 490 491 496 499 500 500 50...
output:
4239 328488156
result:
ok 2 number(s): "4239 328488156"
Test #17:
score: 0
Accepted
time: 4ms
memory: 4080kb
input:
5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 ...
output:
5000 317554850
result:
ok 2 number(s): "5000 317554850"
Test #18:
score: 0
Accepted
time: 6ms
memory: 3916kb
input:
5000 4123 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 ...
output:
4999 609985488
result:
ok 2 number(s): "4999 609985488"
Test #19:
score: 0
Accepted
time: 2ms
memory: 4132kb
input:
5000 1501 1689 3190 3774 4708 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 4995 ...
output:
4995 577669110
result:
ok 2 number(s): "4995 577669110"
Test #20:
score: 0
Accepted
time: 8ms
memory: 4064kb
input:
5000 63 107 213 432 444 500 519 543 591 699 704 825 930 1027 1141 1256 1287 1347 1487 1547 1649 1651 1674 1696 1701 1716 1738 1849 1880 1919 1965 1973 1989 2000 2052 2063 2094 2112 2155 2288 2459 2527 2600 2607 2663 2703 2779 2968 3002 3041 3050 3092 3097 3098 3352 3378 3440 3525 3613 3626 3712 3742...
output:
4913 376487851
result:
ok 2 number(s): "4913 376487851"
Test #21:
score: 0
Accepted
time: 7ms
memory: 4104kb
input:
5000 2 9 17 19 50 63 63 82 83 87 92 101 126 136 172 182 187 201 208 214 222 233 242 256 271 272 284 288 294 300 303 323 353 354 418 430 463 500 501 511 543 550 554 568 569 570 570 577 578 590 654 671 680 695 702 705 716 722 732 736 776 783 785 794 797 808 835 855 859 866 891 896 924 934 942 953 961 ...
output:
4567 930123987
result:
ok 2 number(s): "4567 930123987"
Test #22:
score: 0
Accepted
time: 6ms
memory: 3884kb
input:
5000 9 16 18 19 21 27 44 49 53 63 66 70 84 95 95 101 103 107 107 110 113 113 114 118 126 131 132 135 141 155 162 162 162 168 181 183 184 184 190 191 191 194 195 196 201 203 210 210 211 214 215 219 221 222 232 241 243 250 250 252 253 256 258 258 258 263 271 272 274 282 283 287 292 293 296 308 315 317...
output:
4097 266880018
result:
ok 2 number(s): "4097 266880018"
Test #23:
score: 0
Accepted
time: 5ms
memory: 3848kb
input:
5000 1 5 11 11 13 25 41 41 52 55 60 64 65 65 71 77 90 91 92 99 106 109 112 118 120 128 130 135 136 139 148 151 152 152 163 168 170 172 176 178 184 187 191 195 197 198 204 205 206 225 233 234 235 236 242 247 255 256 258 262 263 263 267 271 271 278 288 289 290 296 299 303 304 305 309 311 318 325 341 3...
output:
4096 441159088
result:
ok 2 number(s): "4096 441159088"
Test #24:
score: 0
Accepted
time: 5ms
memory: 3980kb
input:
5000 1 2 9 10 18 19 21 23 24 38 39 39 48 54 58 60 62 66 85 86 91 97 97 103 103 106 109 112 117 122 124 126 148 148 149 152 152 156 158 166 166 172 185 188 190 199 201 202 203 208 208 208 226 232 238 252 258 262 267 280 281 294 295 302 306 307 308 308 309 309 325 329 329 356 366 366 367 373 381 384 3...
output:
4095 288197876
result:
ok 2 number(s): "4095 288197876"
Test #25:
score: 0
Accepted
time: 6ms
memory: 3892kb
input:
5000 1 8 8 12 13 15 19 20 21 22 25 26 31 31 34 35 35 38 40 41 45 48 51 51 52 54 56 57 58 61 62 62 64 64 64 65 67 68 68 68 69 70 74 76 76 76 78 79 79 80 85 86 89 89 90 91 98 101 102 109 110 114 115 115 115 119 120 122 122 126 129 130 131 131 131 134 136 137 139 140 141 142 144 147 150 150 151 152 154...
output:
3123 952629946
result:
ok 2 number(s): "3123 952629946"
Test #26:
score: 0
Accepted
time: 3ms
memory: 3652kb
input:
5000 1 1 1 1 1 2 2 3 3 4 4 4 4 4 4 4 5 5 5 6 6 6 7 7 7 7 7 8 8 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12 12 12 13 13 13 14 14 15 15 15 16 16 17 17 17 17 18 18 18 18 18 18 18 18 18 19 19 19 20 20 20 21 21 22 22 22 22 22 22 22 23 23 23 23 24 24 24 24 26 26 26 26 27 27 27 28 28 28 29 29 29 29 30 30 3...
output:
1123 702281788
result:
ok 2 number(s): "1123 702281788"
Test #27:
score: 0
Accepted
time: 2ms
memory: 3488kb
input:
5000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4...
output:
123 482123450
result:
ok 2 number(s): "123 482123450"
Test #28:
score: 0
Accepted
time: 1ms
memory: 3592kb
input:
5000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...
output:
42 966786798
result:
ok 2 number(s): "42 966786798"
Test #29:
score: 0
Accepted
time: 2ms
memory: 3480kb
input:
5000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
13 237554682
result:
ok 2 number(s): "13 237554682"
Test #30:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
5000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
7 5040
result:
ok 2 number(s): "7 5040"
Test #31:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
5000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
5 120
result:
ok 2 number(s): "5 120"
Test #32:
score: 0
Accepted
time: 2ms
memory: 3604kb
input:
5000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
3 6
result:
ok 2 number(s): "3 6"
Test #33:
score: 0
Accepted
time: 2ms
memory: 3412kb
input:
5000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
2 2
result:
ok 2 number(s): "2 2"
Test #34:
score: 0
Accepted
time: 2ms
memory: 3660kb
input:
5000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
1 1
result:
ok 2 number(s): "1 1"
Test #35:
score: 0
Accepted
time: 5ms
memory: 3956kb
input:
5000 1 3 3 3 4 4 5 5 5 5 8 8 8 9 9 9 10 13 14 22 22 22 22 24 24 25 27 28 28 28 32 34 35 35 36 36 38 38 40 41 41 42 46 46 46 47 48 48 48 48 50 52 52 53 55 56 56 57 58 59 60 62 62 63 63 65 67 68 70 72 80 82 83 83 84 86 87 88 89 89 90 91 91 91 92 95 95 96 97 97 100 100 100 100 101 102 104 105 105 107 1...
output:
2496 644254912
result:
ok 2 number(s): "2496 644254912"
Test #36:
score: 0
Accepted
time: 3ms
memory: 4028kb
input:
5000 4999 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 ...
output:
4999 648815172
result:
ok 2 number(s): "4999 648815172"
Test #37:
score: 0
Accepted
time: 6ms
memory: 3900kb
input:
5000 4913 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 4999 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 5000 ...
output:
4999 672978716
result:
ok 2 number(s): "4999 672978716"
Test #38:
score: 0
Accepted
time: 7ms
memory: 4080kb
input:
5000 111 598 627 1600 3510 4414 4855 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 4993 499...
output:
4993 778016618
result:
ok 2 number(s): "4993 778016618"
Test #39:
score: 0
Accepted
time: 8ms
memory: 3940kb
input:
5000 31 56 58 60 100 144 151 166 188 192 249 254 254 254 258 259 308 325 337 355 362 374 424 433 438 451 460 491 491 503 507 513 531 537 539 539 544 566 568 596 605 629 635 636 685 693 702 713 726 735 737 744 754 778 780 781 793 801 811 833 838 838 845 868 876 877 897 923 931 935 951 956 968 978 981...
output:
4712 291142969
result:
ok 2 number(s): "4712 291142969"
Test #40:
score: 0
Accepted
time: 11ms
memory: 3932kb
input:
5000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
4712 803514123
result:
ok 2 number(s): "4712 803514123"
Test #41:
score: 0
Accepted
time: 16ms
memory: 4024kb
input:
5000 3 5 6 6 7 7 8 10 10 10 12 12 13 13 13 14 15 16 16 16 16 19 19 20 20 21 23 24 25 26 26 26 27 28 29 30 31 31 32 32 33 33 35 35 37 38 38 39 40 41 41 42 42 42 43 44 46 46 46 48 48 50 51 51 52 52 53 53 53 55 56 57 57 57 59 60 60 62 63 63 64 65 65 67 67 67 69 69 71 71 72 72 72 73 73 74 74 75 76 76 76...
output:
4712 234298773
result:
ok 2 number(s): "4712 234298773"
Test #42:
score: 0
Accepted
time: 9ms
memory: 4192kb
input:
12345 1 2 7 9 9 14 15 18 18 20 23 27 27 27 28 28 29 29 30 32 32 32 33 33 34 34 34 35 35 35 36 37 37 37 38 40 40 43 45 47 47 49 50 52 55 56 58 59 59 59 62 64 67 68 68 69 69 71 72 73 74 77 77 80 81 81 82 83 83 87 88 88 89 89 89 91 91 93 94 94 94 95 96 96 97 98 99 99 100 101 101 102 104 104 106 107 107...
output:
6177 12065098
result:
ok 2 number(s): "6177 12065098"
Test #43:
score: 0
Accepted
time: 26ms
memory: 5392kb
input:
34567 4 4 5 6 6 6 10 11 12 14 14 15 15 16 17 19 19 21 22 28 28 30 31 31 33 33 34 34 34 35 35 36 38 38 38 41 41 42 43 44 44 45 46 49 49 49 50 51 52 53 53 54 54 56 57 57 59 59 60 62 62 64 65 66 68 70 73 74 75 75 76 76 79 80 81 83 83 83 84 86 87 88 90 91 91 91 92 93 93 95 95 95 96 96 96 96 98 99 99 100...
output:
17301 238069256
result:
ok 2 number(s): "17301 238069256"
Test #44:
score: 0
Accepted
time: 51ms
memory: 7824kb
input:
77777 2 4 5 5 7 8 9 10 11 12 12 13 13 13 14 14 14 14 15 15 16 19 19 23 25 27 28 28 29 29 30 32 32 33 34 34 35 35 36 38 39 39 40 41 41 42 42 45 47 50 50 51 51 52 54 54 54 54 56 56 56 57 58 58 59 59 62 63 64 65 66 67 68 68 69 71 71 72 73 76 78 80 81 81 83 83 84 84 84 86 89 89 89 94 95 95 96 97 98 99 9...
output:
38737 713424578
result:
ok 2 number(s): "38737 713424578"
Test #45:
score: 0
Accepted
time: 71ms
memory: 8484kb
input:
100000 2 3 4 4 6 6 7 7 7 11 11 12 13 13 13 16 17 17 18 18 20 21 21 23 23 25 25 25 26 27 28 29 32 32 32 33 33 33 35 35 37 37 39 39 40 43 43 45 47 48 49 50 52 53 53 54 54 55 55 56 56 56 56 57 58 59 61 63 66 66 68 69 71 71 74 74 76 77 80 80 82 82 83 84 84 87 87 87 88 88 89 91 91 92 92 96 96 97 98 99 10...
output:
49978 801069203
result:
ok 2 number(s): "49978 801069203"
Test #46:
score: 0
Accepted
time: 82ms
memory: 9468kb
input:
131071 1 3 4 5 6 7 7 7 10 10 11 11 13 15 15 19 21 22 22 24 25 25 27 29 29 31 32 33 35 38 40 41 43 44 44 45 46 46 46 46 50 51 51 52 53 53 53 55 55 56 58 61 61 61 62 64 65 66 68 68 69 69 69 70 71 72 72 73 73 74 75 76 77 77 77 77 78 79 81 82 82 85 86 87 89 89 92 96 97 98 98 101 101 102 103 104 105 105 ...
output:
65535 798765225
result:
ok 2 number(s): "65535 798765225"
Test #47:
score: 0
Accepted
time: 5ms
memory: 4088kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
1 1
result:
ok 2 number(s): "1 1"
Test #48:
score: 0
Accepted
time: 5ms
memory: 4132kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
2 2
result:
ok 2 number(s): "2 2"
Test #49:
score: 0
Accepted
time: 1ms
memory: 4176kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
5 120
result:
ok 2 number(s): "5 120"
Test #50:
score: 0
Accepted
time: 4ms
memory: 4196kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
14 331032489
result:
ok 2 number(s): "14 331032489"
Test #51:
score: 0
Accepted
time: 6ms
memory: 4100kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
42 966786798
result:
ok 2 number(s): "42 966786798"
Test #52:
score: 0
Accepted
time: 1ms
memory: 4020kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
132 477815844
result:
ok 2 number(s): "132 477815844"
Test #53:
score: 0
Accepted
time: 5ms
memory: 4220kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
429 247699365
result:
ok 2 number(s): "429 247699365"
Test #54:
score: 0
Accepted
time: 6ms
memory: 4300kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...
output:
1234 665664101
result:
ok 2 number(s): "1234 665664101"
Test #55:
score: 0
Accepted
time: 7ms
memory: 4780kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7...
output:
5555 303875205
result:
ok 2 number(s): "5555 303875205"
Test #56:
score: 0
Accepted
time: 17ms
memory: 5236kb
input:
131071 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 12 12 12 12 12 12 12 12 13 13 13 13 13 14 14 14 14 14 15 15 15 15 15 15 15 15 15 ...
output:
12345 967550664
result:
ok 2 number(s): "12345 967550664"
Test #57:
score: 0
Accepted
time: 51ms
memory: 7896kb
input:
131071 2 2 2 2 3 3 4 4 4 5 5 6 6 7 7 7 8 9 9 9 10 10 10 10 10 10 11 11 12 12 13 13 13 14 14 14 16 17 17 18 18 18 19 19 19 20 21 21 22 22 22 23 23 23 24 24 24 24 25 25 25 25 26 26 26 26 26 27 27 28 28 28 29 29 29 30 30 31 32 32 32 33 33 33 34 35 35 35 36 36 37 37 37 38 38 39 40 40 41 41 41 42 42 42 4...
output:
34567 986889809
result:
ok 2 number(s): "34567 986889809"
Test #58:
score: 0
Accepted
time: 81ms
memory: 9316kb
input:
131071 1 4 4 5 5 6 6 7 7 7 7 8 8 9 9 10 10 11 11 11 11 13 13 14 16 18 21 22 24 25 25 26 26 27 27 29 32 33 33 35 36 37 37 37 38 39 39 40 40 40 40 41 42 43 45 45 45 45 45 46 48 50 50 50 50 50 51 52 53 53 55 55 56 57 57 62 64 65 66 67 67 67 68 69 70 70 72 73 73 73 75 77 78 78 78 79 80 80 81 82 82 82 82...
output:
61231 522404009
result:
ok 2 number(s): "61231 522404009"
Test #59:
score: 0
Accepted
time: 112ms
memory: 13180kb
input:
131071 1 1 5 10 11 12 14 15 15 17 18 18 18 21 22 23 24 25 27 27 28 29 29 31 31 34 39 39 41 43 46 47 47 47 49 50 52 53 53 53 58 59 62 64 65 66 66 67 67 68 70 71 71 73 75 77 79 80 80 82 84 85 85 87 87 87 89 89 94 96 99 100 103 103 107 107 108 108 111 113 113 116 116 117 117 123 123 124 125 125 129 131...
output:
77777 113007549
result:
ok 2 number(s): "77777 113007549"
Test #60:
score: 0
Accepted
time: 156ms
memory: 13264kb
input:
131071 2 4 16 26 31 36 38 40 48 50 51 52 58 64 74 75 75 78 82 84 93 93 94 96 101 102 102 104 107 108 118 118 122 127 139 141 145 148 156 168 170 175 181 183 183 186 187 195 198 198 199 199 201 204 216 217 220 227 230 230 231 234 237 239 241 243 244 250 252 254 255 260 261 266 266 269 285 290 293 295...
output:
99230 646212276
result:
ok 2 number(s): "99230 646212276"
Test #61:
score: 0
Accepted
time: 169ms
memory: 13680kb
input:
131071 7 8 13 51 55 66 67 87 88 90 95 102 116 117 119 128 128 134 161 162 168 172 181 190 191 200 201 223 223 231 233 239 247 256 262 272 291 296 307 314 318 327 330 331 333 333 341 358 358 361 365 366 373 375 377 381 391 393 407 414 414 415 423 431 448 453 454 457 463 480 492 501 509 511 513 520 53...
output:
112345 90940517
result:
ok 2 number(s): "112345 90940517"
Test #62:
score: 0
Accepted
time: 179ms
memory: 13512kb
input:
131071 20 33 33 60 88 106 106 119 119 121 138 138 139 169 193 199 200 203 220 226 235 258 260 290 303 308 337 350 374 385 391 396 399 413 421 423 481 485 504 571 579 581 584 584 593 600 602 628 654 672 687 730 773 807 814 837 895 909 950 955 965 976 981 1003 1007 1021 1045 1045 1050 1083 1092 1094 1...
output:
123456 269034268
result:
ok 2 number(s): "123456 269034268"
Test #63:
score: 0
Accepted
time: 192ms
memory: 13600kb
input:
131071 11 312 470 489 561 677 767 843 859 1352 1387 1656 1855 2044 2188 2296 2314 2596 2719 2777 2877 3248 3511 3647 3878 3891 3893 3948 3958 4038 4209 4384 4401 4518 5041 5251 5252 5303 5470 5677 5904 6079 6327 6633 6710 6838 7464 7527 7684 7805 7981 8070 8250 8271 8292 8337 8352 8753 8838 8865 899...
output:
130112 274844737
result:
ok 2 number(s): "130112 274844737"
Test #64:
score: 0
Accepted
time: 186ms
memory: 12988kb
input:
131071 353 963 1037 1200 1809 3963 4980 5480 6035 8063 8252 9138 9293 9556 10428 11293 11635 11749 11947 12743 13788 14763 15351 15415 16081 16403 16713 16903 17789 18148 18421 18421 19670 20914 21053 22581 23483 23632 24640 26377 26551 27456 27461 27622 28409 29535 29605 29691 29842 30579 30674 314...
output:
130851 17367977
result:
ok 2 number(s): "130851 17367977"
Test #65:
score: 0
Accepted
time: 180ms
memory: 13352kb
input:
131071 296 1119 1467 4600 5182 11247 14269 15223 20929 21039 21555 22161 22908 27224 29358 30281 32287 35447 35718 39080 41567 44477 44910 45006 47142 48301 48548 48859 48965 55955 56602 58940 59307 59465 61532 64352 66120 71113 72055 76259 76376 78066 79755 84683 87278 87321 90267 90435 90624 92188...
output:
130999 449823232
result:
ok 2 number(s): "130999 449823232"
Test #66:
score: 0
Accepted
time: 186ms
memory: 13564kb
input:
131071 7556 8071 13415 16058 17088 20870 24304 24830 37901 38580 41995 44508 48161 61543 66311 76101 77252 78293 79557 80639 81548 81584 88739 91485 95407 97742 115137 120544 123887 131042 131042 131042 131042 131042 131042 131042 131042 131042 131042 131042 131042 131042 131042 131042 131042 131042...
output:
131042 314169948
result:
ok 2 number(s): "131042 314169948"
Test #67:
score: 0
Accepted
time: 182ms
memory: 13560kb
input:
131071 8519 20620 27739 33673 36784 66396 76715 97566 121117 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 131062 1...
output:
131062 823790233
result:
ok 2 number(s): "131062 823790233"
Test #68:
score: 0
Accepted
time: 201ms
memory: 13392kb
input:
131071 59094 69170 73139 113253 113446 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 131066 13...
output:
131066 817528150
result:
ok 2 number(s): "131066 817528150"
Test #69:
score: 0
Accepted
time: 290ms
memory: 16888kb
input:
131071 5095 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 131070 1...
output:
131070 933727768
result:
ok 2 number(s): "131070 933727768"
Test #70:
score: 0
Accepted
time: 155ms
memory: 13556kb
input:
131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071...
output:
131071 65855831
result:
ok 2 number(s): "131071 65855831"
Test #71:
score: 0
Accepted
time: 328ms
memory: 17080kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 131013 13...
output:
131013 573172275
result:
ok 2 number(s): "131013 573172275"
Test #72:
score: 0
Accepted
time: 250ms
memory: 17172kb
input:
131071 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 131014 13...
output:
131013 800326197
result:
ok 2 number(s): "131013 800326197"
Test #73:
score: 0
Accepted
time: 257ms
memory: 16196kb
input:
131071 1164 3245 6104 9441 18127 18279 22080 25551 27108 28084 34700 36632 41439 41579 43394 44379 47563 48309 48809 50014 50709 58528 61531 67043 67732 70522 70725 73883 74032 75314 79813 82469 83714 84951 88237 88708 90402 91928 93357 94494 94908 95962 96018 99678 100110 102755 102886 103902 10562...
output:
131013 195940380
result:
ok 2 number(s): "131013 195940380"
Test #74:
score: 0
Accepted
time: 174ms
memory: 14056kb
input:
131071 5858 8601 15570 18840 19352 20927 21563 22382 24578 25143 30291 33672 34919 41238 45265 45438 55818 56787 65752 66625 66702 67349 69212 73422 76562 82206 83311 83376 83400 83671 84813 86888 89148 92992 93042 94841 95231 96056 96372 97107 97695 98630 100494 105980 109254 109707 109945 111181 1...
output:
131013 676303795
result:
ok 2 number(s): "131013 676303795"
Test #75:
score: 0
Accepted
time: 163ms
memory: 13676kb
input:
131071 131070 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071 131071...
output:
131070 712490707
result:
ok 2 number(s): "131070 712490707"