QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#719973 | #8932. Bingo | maspy | AC ✓ | 797ms | 17228kb | C++23 | 42.6kb | 2024-11-07 10:06:07 | 2024-11-07 10:06:08 |
Judging History
answer
#line 1 "/home/maspy/compro/library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
// https://codeforces.com/blog/entry/96344
#pragma GCC optimize("Ofast,unroll-loops")
// いまの CF だとこれ入れると動かない?
// #pragma GCC target("avx2,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
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); }
int popcnt_sgn(int x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
// (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>
T floor(T a, T b) {
return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <typename T, typename U>
T SUM(const vector<U> &A) {
T sm = 0;
for (auto &&a: A) sm += a;
return sm;
}
#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) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
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;
(check(x) ? ok : ng) = 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;
(check(x) ? ok : ng) = 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;
}
template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &... others) {
vc<T> &res = first;
(res.insert(res.end(), others.begin(), others.end()), ...);
}
#endif
#line 1 "/home/maspy/compro/library/other/io.hpp"
#define FASTIO
#include <unistd.h>
// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;
struct Pre {
char num[10000][4];
constexpr Pre() : num() {
for (int i = 0; i < 10000; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i][j] = n % 10 | '0';
n /= 10;
}
}
}
} constexpr pre;
inline void load() {
memcpy(ibuf, ibuf + pil, pir - pil);
pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
pil = 0;
if (pir < SZ) ibuf[pir++] = '\n';
}
inline void flush() {
fwrite(obuf, 1, por, stdout);
por = 0;
}
void rd(char &c) {
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (isspace(c));
}
void rd(string &x) {
x.clear();
char c;
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (isspace(c));
do {
x += c;
if (pil == pir) load();
c = ibuf[pil++];
} while (!isspace(c));
}
template <typename T>
void rd_real(T &x) {
string s;
rd(s);
x = stod(s);
}
template <typename T>
void rd_integer(T &x) {
if (pil + 100 > pir) load();
char c;
do
c = ibuf[pil++];
while (c < '-');
bool minus = 0;
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (c == '-') { minus = 1, c = ibuf[pil++]; }
}
x = 0;
while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (minus) x = -x;
}
}
void rd(int &x) { rd_integer(x); }
void rd(ll &x) { rd_integer(x); }
void rd(i128 &x) { rd_integer(x); }
void rd(u32 &x) { rd_integer(x); }
void rd(u64 &x) { rd_integer(x); }
void rd(u128 &x) { rd_integer(x); }
void rd(double &x) { rd_real(x); }
void rd(long double &x) { rd_real(x); }
void rd(f128 &x) { rd_real(x); }
template <class T, class U>
void rd(pair<T, U> &p) {
return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
rd(x);
rd_tuple<N + 1>(t);
}
}
template <class... T>
void rd(tuple<T...> &tpl) {
rd_tuple(tpl);
}
template <size_t N = 0, typename T>
void rd(array<T, N> &x) {
for (auto &d: x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
for (auto &d: x) rd(d);
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
rd(h), read(t...);
}
void wt(const char c) {
if (por == SZ) flush();
obuf[por++] = c;
}
void wt(const string s) {
for (char c: s) wt(c);
}
void wt(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) wt(s[i]);
}
template <typename T>
void wt_integer(T x) {
if (por > SZ - 100) flush();
if (x < 0) { obuf[por++] = '-', x = -x; }
int outi;
for (outi = 96; x >= 10000; outi -= 4) {
memcpy(out + outi, pre.num[x % 10000], 4);
x /= 10000;
}
if (x >= 1000) {
memcpy(obuf + por, pre.num[x], 4);
por += 4;
} else if (x >= 100) {
memcpy(obuf + por, pre.num[x] + 1, 3);
por += 3;
} else if (x >= 10) {
int q = (x * 103) >> 10;
obuf[por] = q | '0';
obuf[por + 1] = (x - q * 10) | '0';
por += 2;
} else
obuf[por++] = x | '0';
memcpy(obuf + por, out + outi + 4, 96 - outi);
por += 96 - outi;
}
template <typename T>
void wt_real(T x) {
ostringstream oss;
oss << fixed << setprecision(15) << double(x);
string s = oss.str();
wt(s);
}
void wt(int x) { wt_integer(x); }
void wt(ll x) { wt_integer(x); }
void wt(i128 x) { wt_integer(x); }
void wt(u32 x) { wt_integer(x); }
void wt(u64 x) { wt_integer(x); }
void wt(u128 x) { wt_integer(x); }
void wt(double x) { wt_real(x); }
void wt(long double x) { wt_real(x); }
void wt(f128 x) { wt_real(x); }
template <class T, class U>
void wt(const pair<T, U> val) {
wt(val.first);
wt(' ');
wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { wt(' '); }
const auto x = std::get<N>(t);
wt(x);
wt_tuple<N + 1>(t);
}
}
template <class... T>
void wt(tuple<T...> tpl) {
wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
template <class T>
void wt(const vector<T> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
wt(head);
if (sizeof...(Tail)) wt(' ');
print(forward<Tail>(tail)...);
}
// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::read;
using fastio::print;
using fastio::flush;
#if defined(LOCAL)
#define SHOW(...) SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define SHOW1(x) print(#x, "=", (x)), flush()
#define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), flush()
#define SHOW6(x, y, z, w, v, u) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), #u, "=", (u)), flush()
#else
#define SHOW(...)
#endif
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define U32(...) \
u32 __VA_ARGS__; \
read(__VA_ARGS__)
#define U64(...) \
u64 __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 3 "main.cpp"
#line 2 "/home/maspy/compro/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::raw(q));
}
return dat[n];
}
template <typename mint>
mint fact(int n) {
static const int mod = mint::get_mod();
assert(0 <= n && n < mod);
static vector<mint> dat = {1, 1};
while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * mint::raw(len(dat)));
return dat[n];
}
template <typename mint>
mint fact_inv(int n) {
static vector<mint> dat = {1, 1};
if (n < 0) 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 constexpr (dense) return C_dense<mint>(n, k);
if constexpr (!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 "/home/maspy/compro/library/mod/modint.hpp"
template <int mod>
struct modint {
static constexpr u32 umod = u32(mod);
static_assert(umod < u32(1) << 31);
u32 val;
static modint raw(u32 v) {
modint x;
x.val = v;
return x;
}
constexpr modint() : val(0) {}
constexpr modint(u32 x) : val(x % umod) {}
constexpr modint(u64 x) : val(x % umod) {}
constexpr modint(u128 x) : val(x % umod) {}
constexpr modint(int x) : val((x %= mod) < 0 ? x + mod : x){};
constexpr modint(ll x) : val((x %= mod) < 0 ? x + mod : x){};
constexpr modint(i128 x) : val((x %= mod) < 0 ? x + mod : x){};
bool operator<(const modint &other) const { return val < other.val; }
modint &operator+=(const modint &p) {
if ((val += p.val) >= umod) val -= umod;
return *this;
}
modint &operator-=(const modint &p) {
if ((val += umod - p.val) >= umod) val -= umod;
return *this;
}
modint &operator*=(const modint &p) {
val = u64(val) * p.val % umod;
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint::raw(val ? mod - val : u32(0)); }
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;
}
static constexpr int get_mod() { return mod; }
// (n, r), r は 1 の 2^n 乗根
static constexpr pair<int, int> ntt_info() {
if (mod == 120586241) return {20, 74066978};
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 == 943718401) return {22, 663003469};
if (mod == 998244353) return {23, 31};
if (mod == 1004535809) return {21, 582313106};
if (mod == 1012924417) return {21, 368093570};
return {-1, -1};
}
static constexpr bool can_ntt() { return ntt_info().fi != -1; }
};
#ifdef FASTIO
template <int mod>
void rd(modint<mod> &x) {
fastio::rd(x.val);
x.val %= mod;
// assert(0 <= x.val && x.val < mod);
}
template <int mod>
void wt(modint<mod> x) {
fastio::wt(x.val);
}
#endif
using modint107 = modint<1000000007>;
using modint998 = modint<998244353>;
#line 2 "/home/maspy/compro/library/mod/mod_inv.hpp"
// long でも大丈夫
// (val * x - 1) が mod の倍数になるようにする
// 特に mod=0 なら x=0 が満たす
ll mod_inv(ll val, ll mod) {
if (mod == 0) return 0;
mod = abs(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 2 "/home/maspy/compro/library/mod/crt3.hpp"
constexpr u32 mod_pow_constexpr(u64 a, u64 n, u32 mod) {
a %= mod;
u64 res = 1;
FOR(32) {
if (n & 1) res = res * a % mod;
a = a * a % mod, n /= 2;
}
return res;
}
template <typename T, u32 p0, u32 p1>
T CRT2(u64 a0, u64 a1) {
static_assert(p0 < p1);
static constexpr u64 x0_1 = mod_pow_constexpr(p0, p1 - 2, p1);
u64 c = (a1 - a0 + p1) * x0_1 % p1;
return a0 + c * p0;
}
template <typename T, u32 p0, u32 p1, u32 p2>
T CRT3(u64 a0, u64 a1, u64 a2) {
static_assert(p0 < p1 && p1 < p2);
static constexpr u64 x1 = mod_pow_constexpr(p0, p1 - 2, p1);
static constexpr u64 x2 = mod_pow_constexpr(u64(p0) * p1 % p2, p2 - 2, p2);
static constexpr u64 p01 = u64(p0) * p1;
u64 c = (a1 - a0 + p1) * x1 % p1;
u64 ans_1 = a0 + c * p0;
c = (a2 - ans_1 % p2 + p2) * x2 % p2;
return T(ans_1) + T(c) * T(p01);
}
template <typename T, u32 p0, u32 p1, u32 p2, u32 p3>
T CRT4(u64 a0, u64 a1, u64 a2, u64 a3) {
static_assert(p0 < p1 && p1 < p2 && p2 < p3);
static constexpr u64 x1 = mod_pow_constexpr(p0, p1 - 2, p1);
static constexpr u64 x2 = mod_pow_constexpr(u64(p0) * p1 % p2, p2 - 2, p2);
static constexpr u64 x3 = mod_pow_constexpr(u64(p0) * p1 % p3 * p2 % p3, p3 - 2, p3);
static constexpr u64 p01 = u64(p0) * p1;
u64 c = (a1 - a0 + p1) * x1 % p1;
u64 ans_1 = a0 + c * p0;
c = (a2 - ans_1 % p2 + p2) * x2 % p2;
u128 ans_2 = ans_1 + c * static_cast<u128>(p01);
c = (a3 - ans_2 % p3 + p3) * x3 % p3;
return T(ans_2) + T(c) * T(p01) * T(p2);
}
template <typename T, u32 p0, u32 p1, u32 p2, u32 p3, u32 p4>
T CRT5(u64 a0, u64 a1, u64 a2, u64 a3, u64 a4) {
static_assert(p0 < p1 && p1 < p2 && p2 < p3 && p3 < p4);
static constexpr u64 x1 = mod_pow_constexpr(p0, p1 - 2, p1);
static constexpr u64 x2 = mod_pow_constexpr(u64(p0) * p1 % p2, p2 - 2, p2);
static constexpr u64 x3 = mod_pow_constexpr(u64(p0) * p1 % p3 * p2 % p3, p3 - 2, p3);
static constexpr u64 x4 = mod_pow_constexpr(u64(p0) * p1 % p4 * p2 % p4 * p3 % p4, p4 - 2, p4);
static constexpr u64 p01 = u64(p0) * p1;
static constexpr u64 p23 = u64(p2) * p3;
u64 c = (a1 - a0 + p1) * x1 % p1;
u64 ans_1 = a0 + c * p0;
c = (a2 - ans_1 % p2 + p2) * x2 % p2;
u128 ans_2 = ans_1 + c * static_cast<u128>(p01);
c = static_cast<u64>(a3 - ans_2 % p3 + p3) * x3 % p3;
u128 ans_3 = ans_2 + static_cast<u128>(c * p2) * p01;
c = static_cast<u64>(a4 - ans_3 % p4 + p4) * x4 % p4;
return T(ans_3) + T(c) * T(p01) * T(p23);
}
#line 2 "/home/maspy/compro/library/poly/convolution_naive.hpp"
template <class T, typename enable_if<!has_mod<T>::value>::type* = nullptr>
vc<T> convolution_naive(const vc<T>& a, const vc<T>& b) {
int n = int(a.size()), m = int(b.size());
if (n > m) return convolution_naive<T>(b, a);
if (n == 0) return {};
vector<T> ans(n + m - 1);
FOR(i, n) FOR(j, m) ans[i + j] += a[i] * b[j];
return ans;
}
template <class T, typename enable_if<has_mod<T>::value>::type* = nullptr>
vc<T> convolution_naive(const vc<T>& a, const vc<T>& b) {
int n = int(a.size()), m = int(b.size());
if (n > m) return convolution_naive<T>(b, a);
if (n == 0) return {};
vc<T> ans(n + m - 1);
if (n <= 16 && (T::get_mod() < (1 << 30))) {
for (int k = 0; k < n + m - 1; ++k) {
int s = max(0, k - m + 1);
int t = min(n, k + 1);
u64 sm = 0;
for (int i = s; i < t; ++i) { sm += u64(a[i].val) * (b[k - i].val); }
ans[k] = sm;
}
} else {
for (int k = 0; k < n + m - 1; ++k) {
int s = max(0, k - m + 1);
int t = min(n, k + 1);
u128 sm = 0;
for (int i = s; i < t; ++i) { sm += u64(a[i].val) * (b[k - i].val); }
ans[k] = T::raw(sm % T::get_mod());
}
}
return ans;
}
#line 2 "/home/maspy/compro/library/poly/convolution_karatsuba.hpp"
// 任意の環でできる
template <typename T>
vc<T> convolution_karatsuba(const vc<T>& f, const vc<T>& g) {
const int thresh = 30;
if (min(len(f), len(g)) <= thresh) return convolution_naive(f, g);
int n = max(len(f), len(g));
int m = ceil(n, 2);
vc<T> f1, f2, g1, g2;
if (len(f) < m) f1 = f;
if (len(f) >= m) f1 = {f.begin(), f.begin() + m};
if (len(f) >= m) f2 = {f.begin() + m, f.end()};
if (len(g) < m) g1 = g;
if (len(g) >= m) g1 = {g.begin(), g.begin() + m};
if (len(g) >= m) g2 = {g.begin() + m, g.end()};
vc<T> a = convolution_karatsuba(f1, g1);
vc<T> b = convolution_karatsuba(f2, g2);
FOR(i, len(f2)) f1[i] += f2[i];
FOR(i, len(g2)) g1[i] += g2[i];
vc<T> c = convolution_karatsuba(f1, g1);
vc<T> F(len(f) + len(g) - 1);
assert(2 * m + len(b) <= len(F));
FOR(i, len(a)) F[i] += a[i], c[i] -= a[i];
FOR(i, len(b)) F[2 * m + i] += b[i], c[i] -= b[i];
if (c.back() == T(0)) c.pop_back();
FOR(i, len(c)) if (c[i] != T(0)) F[m + i] += c[i];
return F;
}
#line 2 "/home/maspy/compro/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;
assert(rank2 != -1 && len(a) <= (1 << max(0, rank2)));
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 8 "/home/maspy/compro/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 constexpr int p0 = 167772161;
static constexpr int p1 = 469762049;
static constexpr int p2 = 754974721;
using mint0 = modint<p0>;
using mint1 = modint<p1>;
using mint2 = modint<p2>;
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);
vc<mint> c(len(c0));
FOR(i, n + m - 1) { c[i] = CRT3<mint, p0, p1, p2>(c0[i].val, c1[i].val, c2[i].val); }
return c;
}
vector<ll> convolution(vector<ll> a, vector<ll> b) {
int n = len(a), m = len(b);
if (!n || !m) return {};
if (min(n, m) <= 2500) return convolution_naive(a, b);
ll mi_a = MIN(a), mi_b = MIN(b);
for (auto& x: a) x -= mi_a;
for (auto& x: b) x -= mi_b;
assert(MAX(a) * MAX(b) <= 1e18);
auto Ac = cumsum<ll>(a), Bc = cumsum<ll>(b);
vi res(n + m - 1);
for (int k = 0; k < n + m - 1; ++k) {
int s = max(0, k - m + 1);
int t = min(n, k + 1);
res[k] += (t - s) * mi_a * mi_b;
res[k] += mi_a * (Bc[k - s + 1] - Bc[k - t + 1]);
res[k] += mi_b * (Ac[t] - Ac[s]);
}
static constexpr u32 MOD1 = 1004535809;
static constexpr u32 MOD2 = 1012924417;
using mint1 = modint<MOD1>;
using mint2 = modint<MOD2>;
vc<mint1> a1(n), b1(m);
vc<mint2> a2(n), b2(m);
FOR(i, n) a1[i] = a[i], a2[i] = a[i];
FOR(i, m) b1[i] = b[i], b2[i] = b[i];
auto c1 = convolution_ntt<mint1>(a1, b1);
auto c2 = convolution_ntt<mint2>(a2, b2);
FOR(i, n + m - 1) { res[i] += CRT2<u64, MOD1, MOD2>(c1[i].val, c2[i].val); }
return res;
}
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_karatsuba<mint>(a, b);
return convolution_ntt(a, b);
}
if (min(n, m) <= 200) return convolution_karatsuba<mint>(a, b);
return convolution_garner(a, b);
}
#line 2 "/home/maspy/compro/library/nt/digit_sum.hpp"
int digit_sum(u64 x) {
const int K = 100'000;
static vc<int> dp(K);
if (dp[1] == 0) { FOR(x, 1, K) dp[x] = dp[x / 10] + (x % 10); }
int res = 0;
while (x) {
res += dp[x % K];
x /= K;
}
return res;
}
#line 3 "/home/maspy/compro/library/bigint/base.hpp"
// 10^9 ずつ区切って
struct BigInteger {
static constexpr int TEN[]
= {1, 10, 100, 1000, 10000,
100000, 1000000, 10000000, 100000000, 1000000000};
static constexpr int LOG = 9;
static constexpr int MOD = TEN[LOG];
using bint = BigInteger;
int sgn;
vc<int> dat;
BigInteger() : sgn(0) {}
BigInteger(i128 val) {
if (val == 0) {
sgn = 0;
return;
}
sgn = 1;
if (val != 0) {
if (val < 0) sgn = -1, val = -val;
while (val > 0) { dat.eb(val % MOD), val /= MOD; }
}
}
BigInteger(string s) {
assert(!s.empty());
sgn = 1;
if (s[0] == '-') {
sgn = -1;
s.erase(s.begin());
assert(!s.empty());
}
if (s[0] == '0') {
sgn = 0;
return;
}
reverse(all(s));
int n = len(s);
int m = ceil(n, LOG);
dat.assign(m, 0);
FOR(i, n) { dat[i / LOG] += TEN[i % LOG] * (s[i] - '0'); }
}
bint &operator=(const bint &p) {
sgn = p.sgn, dat = p.dat;
return *this;
}
bool operator<(const bint &p) const {
if (sgn != p.sgn) { return sgn < p.sgn; }
if (sgn == 0) return false;
if (len(dat) != len(p.dat)) {
if (sgn == 1) return len(dat) < len(p.dat);
if (sgn == -1) return len(dat) > len(p.dat);
}
FOR_R(i, len(dat)) {
if (dat[i] == p.dat[i]) continue;
if (sgn == 1) return dat[i] < p.dat[i];
if (sgn == -1) return dat[i] > p.dat[i];
}
return false;
}
bool operator>(const bint &p) const { return p < *this; }
bool operator<=(const bint &p) const { return !(*this > p); }
bool operator>=(const bint &p) const { return !(*this < p); }
bint &operator+=(const bint p) {
if (sgn == 0) { return *this = p; }
if (p.sgn == 0) return *this;
if (sgn != p.sgn) {
*this -= (-p);
return *this;
}
int n = max(len(dat), len(p.dat));
dat.resize(n + 1);
FOR(i, n) {
if (i < len(p.dat)) dat[i] += p.dat[i];
if (dat[i] >= MOD) dat[i] -= MOD, dat[i + 1] += 1;
}
while (len(dat) && dat.back() == 0) dat.pop_back();
if (dat.empty()) sgn = 0;
return *this;
}
bint &operator-=(const bint p) {
if (p.sgn == 0) return *this;
if (sgn == 0) return *this = (-p);
if (sgn != p.sgn) {
*this += (-p);
return *this;
}
if ((sgn == 1 && *this < p) || (sgn == -1 && *this > p)) {
*this = p - *this;
sgn = -sgn;
return *this;
}
FOR(i, len(p.dat)) { dat[i] -= p.dat[i]; }
FOR(i, len(dat) - 1) {
if (dat[i] < 0) dat[i] += MOD, dat[i + 1] -= 1;
}
while (len(dat) && dat.back() == 0) { dat.pop_back(); }
if (dat.empty()) sgn = 0;
return *this;
}
bint &operator*=(const bint &p) {
sgn *= p.sgn;
if (sgn == 0) {
dat.clear();
} else {
dat = convolve(dat, p.dat);
}
return *this;
}
// bint &operator/=(const bint &p) { return *this; }
bint operator-() const {
bint p = *this;
p.sgn *= -1;
return p;
}
bint operator+(const bint &p) const { return bint(*this) += p; }
bint operator-(const bint &p) const { return bint(*this) -= p; }
bint operator*(const bint &p) const { return bint(*this) *= p; }
// bint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const bint &p) const {
return (sgn == p.sgn && dat == p.dat);
}
bool operator!=(const bint &p) const { return !((*this) == p); }
vc<int> convolve(const vc<int> &a, const vc<int> &b) {
int n = len(a), m = len(b);
if (!n || !m) return {};
if (min(n, m) <= 500) {
vc<int> c(n + m - 1);
u128 x = 0;
FOR(k, n + m - 1) {
int s = max<int>(0, k + 1 - m), t = min<int>(k, n - 1);
FOR(i, s, t + 1) { x += u64(a[i]) * b[k - i]; }
c[k] = x % MOD, x = x / MOD;
}
while (x > 0) { c.eb(x % MOD), x = x / MOD; }
return c;
}
static constexpr int p0 = 167772161;
static constexpr int p1 = 469762049;
static constexpr int p2 = 754974721;
using mint0 = modint<p0>;
using mint1 = modint<p1>;
using mint2 = modint<p2>;
vc<mint0> a0(all(a)), b0(all(b));
vc<mint1> a1(all(a)), b1(all(b));
vc<mint2> a2(all(a)), b2(all(b));
auto c0 = convolution_ntt<mint0>(a0, b0);
auto c1 = convolution_ntt<mint1>(a1, b1);
auto c2 = convolution_ntt<mint2>(a2, b2);
vc<int> c(len(c0));
u128 x = 0;
FOR(i, n + m - 1) {
x += CRT3<u128, p0, p1, p2>(c0[i].val, c1[i].val, c2[i].val);
c[i] = x % MOD, x = x / MOD;
}
while (x) { c.eb(x % MOD), x = x / MOD; }
return c;
}
string to_string() {
if (dat.empty()) return "0";
string s;
for (int x: dat) {
FOR(LOG) {
s += '0' + (x % 10);
x = x / 10;
}
}
while (s.back() == '0') s.pop_back();
if (sgn == -1) s += '-';
reverse(all(s));
return s;
}
// https://codeforces.com/contest/504/problem/D
string to_binary_string() {
assert(sgn >= 0);
vc<u32> A(all(dat));
string ANS;
while (1) {
while (len(A) && A.back() == u32(0)) POP(A);
if (A.empty()) break;
u64 rem = 0;
FOR_R(i, len(A)) {
rem = rem * MOD + A[i];
A[i] = rem >> 32;
rem &= u32(-1);
}
FOR(i, 32) { ANS += '0' + (rem >> i & 1); }
}
while (len(ANS) && ANS.back() == '0') ANS.pop_back();
reverse(all(ANS));
if (ANS.empty()) ANS += '0';
return ANS;
}
// https://codeforces.com/contest/759/problem/E
pair<bint, int> divmod(int p) {
vc<int> after;
ll rm = 0;
FOR_R(i, len(dat)) {
rm = rm * MOD + dat[i];
after.eb(rm / p);
rm = rm % p;
}
reverse(all(after));
while (len(after) && after.back() == 0) POP(after);
bint q;
q.sgn = sgn;
q.dat = after;
rm *= sgn;
if (rm < 0) {
rm += p;
q -= 1;
}
return {q, rm};
}
// https://codeforces.com/problemset/problem/582/D
vc<int> base_p_representation(int p) {
vc<u32> A(all(dat));
vc<int> res;
while (1) {
while (len(A) && A.back() == u32(0)) POP(A);
if (A.empty()) break;
u64 rm = 0;
FOR_R(i, len(A)) {
rm = rm * MOD + A[i];
A[i] = rm / p;
rm %= p;
}
res.eb(rm);
}
reverse(all(res));
return res;
}
// overflow 無視して計算
ll to_ll() {
ll x = 0;
FOR_R(i, len(dat)) x = MOD * x + dat[i];
return sgn * x;
}
// https://codeforces.com/contest/986/problem/D
bint pow(ll n) {
assert(n >= 0);
auto dfs = [&](auto &dfs, ll n) -> bint {
if (n == 1) return (*this);
bint x = dfs(dfs, n / 2);
x *= x;
if (n & 1) x *= (*this);
return x;
};
if (n == 0) return bint(1);
return dfs(dfs, n);
}
// https://codeforces.com/contest/986/problem/D
double log10() {
assert(!dat.empty() && sgn == 1);
if (len(dat) <= 3) {
double x = 0;
FOR_R(i, len(dat)) x = MOD * x + dat[i];
return std::log10(x);
}
double x = 0;
FOR(i, 4) x = MOD * x + dat[len(dat) - 1 - i];
x = std::log10(x);
x += double(LOG) * (len(dat) - 4);
return x;
}
int digit_sum() {
int ans = 0;
for (auto &x: dat) ans += ::digit_sum(x); // global にある digit_sum
return ans;
}
};
#ifdef FASTIO
void wt(BigInteger x) { fastio::wt(x.to_string()); }
void rd(BigInteger &x) {
string s;
fastio::rd(s);
x = BigInteger(s);
}
#endif
#line 1 "/home/maspy/compro/library/string/trie.hpp"
// sigma が小さい
// 一般の n 頂点の木構造で O(n) 時間で動く
// https://atcoder.jp/contests/xmascontest2015noon/tasks/xmascontest2015_d
template <int sigma>
struct Trie {
struct Node {
array<int, sigma> ch;
array<int, sigma> nxt; // suffix link -> add c
int parent;
int suffix_link;
};
int n_node;
vc<Node> nodes;
vc<int> words;
vc<int> BFS; // BFS 順
Trie() {
n_node = 0;
new_node();
}
Node& operator[](int i) { return nodes[i]; }
template <typename STRING>
int add(STRING S, int off) {
int v = 0;
for (auto&& s: S) { v = add_single(v, s, off); }
words.eb(v);
return v;
}
int add_single(int v, int c, int off) {
c -= off;
assert(0 <= c && c < sigma);
if (nodes[v].ch[c] != -1) return nodes[v].ch[c];
nodes[v].ch[c] = new_node();
nodes.back().parent = v;
return nodes[v].ch[c];
}
void calc_suffix_link() {
BFS.resize(n_node);
int p = 0, q = 0;
BFS[q++] = 0;
fill(all(nodes[0].nxt), 0);
while (p < q) {
int v = BFS[p++];
if (v) nodes[v].nxt = nodes[nodes[v].suffix_link].nxt;
FOR(s, sigma) {
int w = nodes[v].ch[s];
if (w == -1) continue;
nodes[w].suffix_link = nodes[v].nxt[s];
nodes[v].nxt[s] = w;
BFS[q++] = w;
}
}
}
vc<int> calc_count() {
vc<int> count(n_node);
for (auto&& x: words) count[x]++;
for (auto&& v: BFS)
if (v) { count[v] += count[nodes[v].suffix_link]; }
return count;
}
private:
int new_node() {
Node c;
fill(all(c.ch), -1);
fill(all(c.nxt), -1);
c.parent = -1;
c.suffix_link = -1;
nodes.eb(c);
return n_node++;
}
};
#line 6 "main.cpp"
void solve() {
STR(S);
vc<int> A = s_to_vi(S, '0');
LL(M);
ll ANS = 0;
{
ll x = 0;
for (auto& a: A) x = (10 * x + a) % M;
ANS = (x == 0 ? M : M - x);
}
// 文字列として含む方
string T = to_string(M);
Trie<10> trie;
trie.add(T, '0');
trie.calc_suffix_link();
int n = trie.n_node;
vv(int, TO, n, 10);
FOR(v, n) { FOR(x, 10) TO[v][x] = (v == n - 1 ? n - 1 : trie[v].nxt[x]); }
// 等しい, 真に大きい場合
// x + c が条件を満たすような c
FOR(15) A.insert(A.begin(), 0);
vi dp1(n, infty<int>);
vi dp2(n, infty<int>);
dp1[0] = 0;
dp2[0] = 1;
for (auto& a: A) {
vi newdp1(n, infty<int>);
vi newdp2(n, infty<int>);
FOR(i, n) {
FOR(x, 10) {
int j = TO[i][x];
if (x < a) { chmin(newdp2[j], dp2[i] * 10 + x - a); }
if (x == a) {
chmin(newdp1[j], dp1[i] * 10 + x - a);
chmin(newdp2[j], dp2[i] * 10 + x - a);
}
if (x > a) {
chmin(newdp2[j], dp1[i] * 10 + x - a);
chmin(newdp2[j], dp2[i] * 10 + x - a);
}
}
}
swap(dp1, newdp1);
swap(dp2, newdp2);
}
chmin(ANS, dp2[n - 1]);
BigInteger ans(S);
ans += BigInteger(ANS);
print(ans.to_string());
}
signed main() {
INT(T);
FOR(T) solve();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3712kb
input:
6 7 3 12 3 9 10 249 51 1369 37 2 1
output:
9 13 10 251 1370 3
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 272ms
memory: 4040kb
input:
100000 3196282243 28 7614814237 33 2814581084 97 1075124401 58 7822266214 100 1767317768 31 7189709841 75 9061337538 69 6552679231 38 9946082148 18 5497675062 54 7787300351 65 4310767261 68 4811341953 100 3265496130 31 8294404054 62 2845521744 90 1114254672 26 6442013672 13 3744046866 40 3289624367 ...
output:
3196282244 7614814251 2814581097 1075124424 7822266300 1767317769 7189709850 9061337569 6552679238 9946082160 5497675063 7787300365 4310767268 4811342000 3265496131 8294404062 2845521790 1114254674 6442013673 3744046867 3289624375 6477935360 1292587551 5504674689 2898829180 7882736025 2846033387 923...
result:
ok 100000 lines
Test #3:
score: 0
Accepted
time: 404ms
memory: 4240kb
input:
100000 81390699571930639918 18 48435143897560239761 20 51628960043353404809 75 47871552664477358704 12 59273263135375104780 37 76916933870890715781 71 23716799616473386311 99 68152894841119747794 73 87132912926681514765 23 14152130046962902029 76 46737628796812988809 24 40572731276115804589 44 26281...
output:
81390699571930639920 48435143897560239780 51628960043353404825 47871552664477358712 59273263135375104781 76916933870890715782 23716799616473386312 68152894841119747819 87132912926681514784 14152130046962902060 46737628796812988824 40572731276115804612 26281826064684565884 31440839508345860244 322404...
result:
ok 100000 lines
Test #4:
score: 0
Accepted
time: 287ms
memory: 3932kb
input:
3000 7455619882273084107817302538515753878442453229160411398764628307708900718973255504130291326067024207515690930909093270962092669445260372092520050302437090344417275699335988483584829739890137897388569114922455183012826259500289116227868472807384706143668392682061768042886347879057062217228560088...
output:
745561988227308410781730253851575387844245322916041139876462830770890071897325550413029132606702420751569093090909327096209266944526037209252005030243709034441727569933598848358482973989013789738856911492245518301282625950028911622786847280738470614366839268206176804288634787905706221722856008829738...
result:
ok 3000 lines
Test #5:
score: 0
Accepted
time: 268ms
memory: 5032kb
input:
30 744510720233756810275704474604569745531035132640480367036243214564743016462196305795005354253050610470663589619681077004760475741831076832118956334526511556189341001350810877421229216943948759000423729628987372479322027768925371301094347427331321252113757179047865411408129452046409100799804797802...
output:
744510720233756810275704474604569745531035132640480367036243214564743016462196305795005354253050610470663589619681077004760475741831076832118956334526511556189341001350810877421229216943948759000423729628987372479322027768925371301094347427331321252113757179047865411408129452046409100799804797802630...
result:
ok 30 lines
Test #6:
score: 0
Accepted
time: 565ms
memory: 5164kb
input:
30 105809806761073271256887955844712146828376005200803410278562148742151831107043468856832007772397456498455138267388212588031778017994162605374749926516470689670500596864238694115641474868867283410369346289694466106606968664652794746652928273585660548259964572696052192486090603389036781439603103550...
output:
105809806761073271256887955844712146828376005200803410278562148742151831107043468856832007772397456498455138267388212588031778017994162605374749926516470689670500596864238694115641474868867283410369346289694466106606968664652794746652928273585660548259964572696052192486090603389036781439603103550889...
result:
ok 30 lines
Test #7:
score: 0
Accepted
time: 570ms
memory: 16400kb
input:
3 6662543589767466036246495639712784214474800991502397969575900509392047845463534421346617412312874112216857204224202275839595569716788606196210901109061789975871578517491994493982488930577332027254701120487517960025453194037512904252125592964194448410502834649100626540616447074805775399798416968996...
output:
666254358976746603624649563971278421447480099150239796957590050939204784546353442134661741231287411221685720422420227583959556971678860619621090110906178997587157851749199449398248893057733202725470112048751796002545319403751290425212559296419444841050283464910062654061644707480577539979841696899669...
result:
ok 3 lines
Test #8:
score: 0
Accepted
time: 322ms
memory: 16820kb
input:
3 2256815269292565600156592193265637162224642022040502404316754847487816065925979403817785481924286639616366305614486960621565248701689067791722502719550041520364571182248705842386853601004910941840817347333021115646730530046766144383475985584378580286873533357460940983992705079090280123003684728456...
output:
225681526929256560015659219326563716222464202204050240431675484748781606592597940381778548192428663961636630561448696062156524870168906779172250271955004152036457118224870584238685360100491094184081734733302111564673053004676614438347598558437858028687353335746094098399270507909028012300368472845644...
result:
ok 3 lines
Test #9:
score: 0
Accepted
time: 330ms
memory: 4252kb
input:
3000 1207038022435933497172867897608582834259296434513291920701948658102620971426545864576004516739100232550813726497575096647995838994831563955620812388750705335658097405810012745415674568116125791908722845971700778242654229220680637480729636188583094437004798976145084978037723467998680024921907308...
output:
120703802243593349717286789760858283425929643451329192070194865810262097142654586457600451673910023255081372649757509664799583899483156395562081238875070533565809740581001274541567456811612579190872284597170077824265422922068063748072963618858309443700479897614508497803772346799868002492190730846477...
result:
ok 3000 lines
Test #10:
score: 0
Accepted
time: 385ms
memory: 4220kb
input:
3000 5187892603368075393366934145761537065733813260653168421673419435040503882271162798390450740194183522231455416476478191713916592902549286209613912969893791259298651451942049981218891583442405056389087471219061688102180898906527834095792495015886014196868276353417569226991138737552057896779213092...
output:
518789260336807539336693414576153706573381326065316842167341943504050388227116279839045074019418352223145541647647819171391659290254928620961391296989379125929865145194204998121889158344240505638908747121906168810218089890652783409579249501588601419686827635341756922699113873755205789677921309299202...
result:
ok 3000 lines
Test #11:
score: 0
Accepted
time: 270ms
memory: 4196kb
input:
100000 6826989967 99 7841681495 15 3418122299 23 1701647194 72 9299999983 93 1881999956 82 1123299980 33 3771371885 19 1309999986 31 3877999989 78 9357331671 75 7627599957 76 1678299927 83 8841256998 57 8260576238 25 1799999994 18 3154099999 41 9276743997 44 4591431162 68 3499999996 35 8003134976 35...
output:
6826989968 7841681500 3418122300 1701647200 9299999993 1881999958 1123300000 3771371900 1310000000 3878000000 9357331675 7627599958 1678299983 8841257000 8260576250 1800000000 3154100000 9276744000 4591431168 3500000000 8003135000 5504020000 1255390000 4126500000 6300000000 7478000000 5763230000 666...
result:
ok 100000 lines
Test #12:
score: 0
Accepted
time: 532ms
memory: 3912kb
input:
100000 4044835167 404504 8453174053 180343 2345170166 345322 8666517296 666590 8889410862 895103 6539631279 398778 8763575235 742233 3347416626 347507 6020459808 204689 2518651157 186574 1970898496 970950 3644468912 446818 2284133423 265848 4297464212 297490 9673618882 687317 2262910438 26292 460442...
output:
4045040000 8453180343 2345322000 8666590000 8889510300 6539877800 8763742233 3347507000 6020468900 2518657400 1970950000 3644681800 2284166016 4297490000 9673687317 2262920000 4604460456 5602450000 9441133300 9662607060 1137808896 5763236104 1265770000 9709569000 2270400726 8750103140 4985572109 607...
result:
ok 100000 lines
Test #13:
score: 0
Accepted
time: 797ms
memory: 4272kb
input:
30000 2258423372953702192256514103021059999999999999999999999999999999999999999999999999999999999942889028 410302106 9236851817519460666717196537240869999999999999999999999999999999999999999999999999999999999883401708 653724087 528305359636938134447446654161626435106860486355385047059638284973309516...
output:
2258423372953702192256514103021060000000000000000000000000000000000000000000000000000000000000000000 9236851817519460666717196537240870000000000000000000000000000000000000000000000000000000000000000000 52830535963693813444744665416162643510686048635538504705963828497330951691000000000000000000000000...
result:
ok 30000 lines
Test #14:
score: 0
Accepted
time: 578ms
memory: 4208kb
input:
3000 5922323879507505766800528767037919597502198414242047957236861868104971055619344440437107671176937451514382066899903609033675738466227459315946342972297607721778919880762618920008986787526795904399466415526677955192892547202914540028866220838677781672961400903843675918899999999999999999999999999...
output:
592232387950750576680052876703791959750219841424204795723686186810497105561934444043710767117693745151438206689990360903367573846622745931594634297229760772177891988076261892000898678752679590439946641552667795519289254720291454002886622083867778167296140090384367591890000000000000000000000000000000...
result:
ok 3000 lines
Test #15:
score: 0
Accepted
time: 546ms
memory: 4016kb
input:
300 79503684358998967099842390859918474661077381309414120162088118845509995775769599862634805068256914519241333788469171358889239210321345976686972530426806265499919511256567835592757432091301073156228966702566684249419578352892680950286666144449266128886167409392158527498584296552596909871547436747...
output:
795036843589989670998423908599184746610773813094141201620881188455099957757695998626348050682569145192413337884691713588892392103213459766869725304268062654999195112565678355927574320913010731562289667025666842494195783528926809502866661444492661288861674093921585274985842965525969098715474367473579...
result:
ok 300 lines
Test #16:
score: 0
Accepted
time: 540ms
memory: 5100kb
input:
30 963989556914230908082271728546945777135479223654091867288007126245836594537638569203178109698540536134315332558020645906558121951862746368709679951030471570045529976669887496328009080338319231117849873217415406534659827460233181034192437276876779118787074109399261707873162971159516672507806779438...
output:
963989556914230908082271728546945777135479223654091867288007126245836594537638569203178109698540536134315332558020645906558121951862746368709679951030471570045529976669887496328009080338319231117849873217415406534659827460233181034192437276876779118787074109399261707873162971159516672507806779438881...
result:
ok 30 lines
Test #17:
score: 0
Accepted
time: 565ms
memory: 16904kb
input:
3 5251523982592970506228422825473158460046709412102650259324461772573819982478268554863488566358166621796735028520826739449136398485099429491505550466187845809470304336668405076216439622257295011604734741380276770594862747289699140439236774183654203024248651235112580263413687129799928450508420430094...
output:
525152398259297050622842282547315846004670941210265025932446177257381998247826855486348856635816662179673502852082673944913639848509942949150555046618784580947030433666840507621643962225729501160473474138027677059486274728969914043923677418365420302424865123511258026341368712979992845050842043009466...
result:
ok 3 lines
Test #18:
score: 0
Accepted
time: 256ms
memory: 5444kb
input:
30 627919418377410357943555763628655192389689054924973950611879805274209177346126760861588844600423459519850583010250101760720518969321844870291790270120612460050314573390014064010258558280381655682905366739760793312813232203778926302125707670987782450886207303011675839927569831089665011115588292404...
output:
627919418377410357943555763628655192389689054924973950611879805274209177346126760861588844600423459519850583010250101760720518969321844870291790270120612460050314573390014064010258558280381655682905366739760793312813232203778926302125707670987782450886207303011675839927569831089665011115588292404149...
result:
ok 30 lines
Test #19:
score: 0
Accepted
time: 239ms
memory: 5284kb
input:
30 764080955258682719471086701870262765599374530101516574475330551922255442091459958688289946340233580617988205812251968123424819960658592846247598815326003118826452825551875685355929397031899182527979539632844954405219059237546143875107808300965485161976399348689613744946767210379782917080998523510...
output:
764080955258682719471086701870262765599374530101516574475330551922255442091459958688289946340233580617988205812251968123424819960658592846247598815326003118826452825551875685355929397031899182527979539632844954405219059237546143875107808300965485161976399348689613744946767210379782917080998523510627...
result:
ok 30 lines
Test #20:
score: 0
Accepted
time: 244ms
memory: 16760kb
input:
3 6028978024074413159342407738765076385763911038536831317696787959027528197366178750790544749013673661567582902671539726842479448599455873905708255453884865704773086705053235875806914264836730112475075045703811837740246613047422355645457818355939368587060246878107326418339579284201653817689773671953...
output:
602897802407441315934240773876507638576391103853683131769678795902752819736617875079054474901367366156758290267153972684247944859945587390570825545388486570477308670505323587580691426483673011247507504570381183774024661304742235564545781835593936858706024687810732641833957928420165381768977367195319...
result:
ok 3 lines
Test #21:
score: 0
Accepted
time: 254ms
memory: 16888kb
input:
3 5021181897690139043423621132705730237575386907971979814912425232029109374214407872841234899499833146294421742332452796878337581201268864699781904270882612294625097518085924677142481018311453949030110398429573396190105839011124187467223158307238425121693199936952649840784178844249496013886360158949...
output:
502118189769013904342362113270573023757538690797197981491242523202910937421440787284123489949983314629442174233245279687833758120126886469978190427088261229462509751808592467714248101831145394903011039842957339619010583901112418746722315830723842512169319993695264984078417884424949601388636015894947...
result:
ok 3 lines
Test #22:
score: 0
Accepted
time: 238ms
memory: 3868kb
input:
100000 983 89 38 751 34 201 841 39 453 873 674 65 560 216 914 644 670 345 622 518 583 938 137 719 359 78 448 860 741 907 236 629 893 549 844 999 63 345 320 66 990 723 215 467 665 24 682 756 853 604 262 160 153 30 278 31 232 631 495 360 347 519 259 906 541 992 167 33 661 233 908 720 55 332 17 153 595...
output:
989 751 201 858 873 715 648 1288 690 1036 938 719 378 860 907 629 1098 999 345 330 1446 467 672 756 1208 320 180 279 631 720 519 906 992 198 699 1440 332 153 752 206 1534 1188 977 1142 1232 741 754 482 891 504 376 921 852 658 974 700 266 792 500 897 976 570 1258 869 120 578 616 1274 1240 860 453 229...
result:
ok 100000 lines
Test #23:
score: 0
Accepted
time: 429ms
memory: 4196kb
input:
100000 145493 709877 915888 170549 680394 2504 24665 758139 242644 365499 994789 535590 518365 297247 852392 834337 480961 945750 485671 126953 262033 11673 199902 473744 91856 743475 779195 394619 60537 616388 570144 756645 285494 510464 298148 932570 110058 627373 686867 469501 82153 559208 231338...
output:
709877 1023294 681088 758139 365499 1071180 594494 1668674 945750 507812 268479 473744 743475 789238 616388 756645 510464 932570 627373 939002 559208 272262 957378 749236 980046 1307232 883500 695250 1351682 495376 391899 1403758 674864 399172 826044 486136 797852 368134 1341368 476373 998655 234605...
result:
ok 100000 lines
Test #24:
score: 0
Accepted
time: 665ms
memory: 4220kb
input:
100000 22102638 716270982 114744220 553198855 952648509 973042933 12679271 411855162 766516692 376478639 797071538 701616593 832850102 567088052 350649206 231532599 950645496 638081144 879396229 769653386 7765699 641178981 664333826 683883038 953508821 193902679 470742245 399344120 571075073 4774108...
output:
716270982 553198855 973042933 411855162 1129435917 1403233186 1134176104 463065198 1276162288 1539306772 641178981 683883038 969513395 798688240 954821692 885018278 759825152 720908055 304133428 841067258 1086439989 172971588 1018041974 1194071806 1186808712 1520671194 1640903698 406899456 841206665...
result:
ok 100000 lines
Test #25:
score: 0
Accepted
time: 264ms
memory: 3968kb
input:
100000 223876976 28 826504481 32 636375005 4 96535351 65 198842419 24 831169545 73 576953641 82 935299711 20 387543800 23 179825475 79 473820636 86 461889861 8 534725270 34 388928056 62 64278765 63 549355282 3 708135976 13 290272912 29 411662229 49 516874672 54 115846918 82 627529269 63 23910223 33 ...
output:
223876996 826504512 636375008 96535352 198842420 831169573 576953682 935299720 387543813 179825476 473820686 461889862 534725271 388928062 64278774 549355283 708135977 290272913 411662230 516874716 115846976 627529329 23910233 590830376 319867594 585259345 740335580 846844351 652565836 303759440 212...
result:
ok 100000 lines
Test #26:
score: 0
Accepted
time: 457ms
memory: 3948kb
input:
100000 95 410302106 52 820706056 67 87747886 43 380552276 40 627914953 23 374682702 8 733202611 86 386357954 3 550477660 64 659482338 63 766955734 79 625128385 13 406562646 8 777253591 37 653724087 100 107364480 58 953592822 68 213009532 67 302548407 41 282013297 95 228056767 92 407864420 18 1265642...
output:
410302106 820706056 87747886 380552276 627914953 374682702 733202611 386357954 550477660 659482338 766955734 625128385 406562646 777253591 653724087 107364480 953592822 213009532 302548407 282013297 228056767 407864420 126564216 867640132 89634672 168202079 693460954 910218987 330951691 513234559 11...
result:
ok 100000 lines
Test #27:
score: 0
Accepted
time: 219ms
memory: 3908kb
input:
99856 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 6...
output:
2 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 ...
result:
ok 99856 lines
Test #28:
score: 0
Accepted
time: 448ms
memory: 17228kb
input:
3 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
result:
ok 3 lines
Test #29:
score: 0
Accepted
time: 472ms
memory: 16128kb
input:
3 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
result:
ok 3 lines
Test #30:
score: 0
Accepted
time: 172ms
memory: 4052kb
input:
24573 1 1000000000 2 1000000000 3 1000000000 4 1000000000 5 1000000000 6 1000000000 7 1000000000 8 1000000000 9 1000000000 10 1000000000 11 1000000000 12 1000000000 13 1000000000 14 1000000000 15 1000000000 16 1000000000 17 1000000000 18 1000000000 19 1000000000 20 1000000000 21 1000000000 22 100000...
output:
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 100...
result:
ok 24573 lines
Test #31:
score: 0
Accepted
time: 447ms
memory: 16184kb
input:
3 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
result:
ok 3 lines
Test #32:
score: 0
Accepted
time: 508ms
memory: 16964kb
input:
3 8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888...
output:
888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888...
result:
ok 3 lines
Extra Test:
score: 0
Extra Test Passed