QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#349163 | #8340. 3 Sum | ucup-team180# | TL | 325ms | 15224kb | C++20 | 82.1kb | 2024-03-09 23:52:41 | 2024-03-09 23:52:41 |
Judging History
answer
#pragma region Macros
#ifdef noimi
#pragma comment(linker, "/stack:256000000")
#include "my_template.hpp"
#else
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <immintrin.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>
#ifdef noimi
#define oj_local(a, b) b
#else
#define oj_local(a, b) a
#endif
#define LOCAL if(oj_local(0, 1))
#define OJ if(oj_local(1, 0))
using namespace std;
using ll = long long;
using ull = unsigned long long int;
using i128 = __int128_t;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vc<T>>;
template <typename T> using vvvc = vector<vvc<T>>;
using vi = vc<int>;
using vl = vc<ll>;
using vpi = vc<pii>;
using vpl = vc<pll>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T> int si(const T &x) { return x.size(); }
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); }
vi iota(int n) {
vi a(n);
return iota(a.begin(), a.end(), 0), a;
}
template <typename T> vi iota(const vector<T> &a, bool greater = false) {
vi res(a.size());
iota(res.begin(), res.end(), 0);
sort(res.begin(), res.end(), [&](int i, int j) {
if(greater) return a[i] > a[j];
return a[i] < a[j];
});
return res;
}
// macros
#define overload5(a, b, c, d, e, name, ...) name
#define overload4(a, b, c, d, name, ...) name
#define endl '\n'
#define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf)
#define REP1(i, n) for(ll i = 0; i < (n); ++i)
#define REP2(i, a, b) for(ll i = (a); i < (b); ++i)
#define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf)
#define per1(i, n) for(ll i = (n)-1; i >= 0; --i)
#define per2(i, a, b) for(ll i = (a)-1; i >= b; --i)
#define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c))
#define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__)
#define fore0(a) rep(a.size())
#define fore1(i, a) for(auto &&i : a)
#define fore2(a, b, v) for(auto &&[a, b] : v)
#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)
#define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v)
#define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__)
#define setbits(j, n) for(ll iiiii = (n), j = lowbit(iiiii); iiiii; iiiii ^= 1 << j, j = lowbit(iiiii))
#define perm(v) for(bool permrepflag = true; (permrepflag ? exchange(permrepflag, false) : next_permutation(all(v)));)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define drop(s) cout << #s << endl, exit(0)
#define si(c) (int)(c).size()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))
#define rng(v, l, r) v.begin() + (l), v.begin() + (r)
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define SORT(v) sort(all(v))
#define REV(v) reverse(all(v))
#define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end())
template <typename T = ll, typename S> T SUM(const S &v) { return accumulate(all(v), T(0)); }
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#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__))))
constexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}};
constexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
namespace yesno_impl {
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
const string firstsecond[2] = {"second", "first"};
const string FirstSecond[2] = {"Second", "First"};
const string possiblestr[2] = {"impossible", "possible"};
const string Possiblestr[2] = {"Impossible", "Possible"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }
void first(bool t = 1) { cout << firstsecond[t] << endl; }
void First(bool t = 1) { cout << FirstSecond[t] << endl; }
void possible(bool t = 1) { cout << possiblestr[t] << endl; }
void Possible(bool t = 1) { cout << Possiblestr[t] << endl; }
}; // namespace yesno_impl
using namespace yesno_impl;
#define INT(...) \
int __VA_ARGS__; \
IN(__VA_ARGS__)
#define INTd(...) \
int __VA_ARGS__; \
IN2(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
IN(__VA_ARGS__)
#define LLd(...) \
ll __VA_ARGS__; \
IN2(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
IN(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
IN(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
IN(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
IN(name)
#define VECd(type, name, size) \
vector<type> name(size); \
IN2(name)
#define VEC2(type, name1, name2, size) \
vector<type> name1(size), name2(size); \
for(int i = 0; i < size; i++) IN(name1[i], name2[i])
#define VEC2d(type, name1, name2, size) \
vector<type> name1(size), name2(size); \
for(int i = 0; i < size; i++) IN2(name1[i], name2[i])
#define VEC3(type, name1, name2, name3, size) \
vector<type> name1(size), name2(size), name3(size); \
for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i])
#define VEC3d(type, name1, name2, name3, size) \
vector<type> name1(size), name2(size), name3(size); \
for(int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i])
#define VEC4(type, name1, name2, name3, name4, size) \
vector<type> name1(size), name2(size), name3(size), name4(size); \
for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]);
#define VEC4d(type, name1, name2, name3, name4, size) \
vector<type> name1(size), name2(size), name3(size), name4(size); \
for(int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i], name4[i]);
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
IN(name)
#define VVd(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
IN2(name)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
void IN2() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
scan(head);
IN(tail...);
}
template <class Head, class... Tail> void IN2(Head &head, Tail &...tail) {
scan(head);
--head;
IN2(tail...);
}
template <int p = -1> void pat() {}
template <int p = -1, class Head, class... Tail> void pat(Head &h, Tail &...tail) {
h += p;
pat<p>(tail...);
}
template <typename T, typename S> T ceil(T x, S y) {
assert(y);
return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}
template <typename T, typename S> T floor(T x, S y) {
assert(y);
return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
template <typename T, typename S, typename U> U bigmul(const T &x, const S &y, const U &lim) { // clamp(x * y, -lim, lim)
if(x < 0 and y < 0) return bigmul(-x, -y, lim);
if(x < 0) return -bigmul(-x, y, lim);
if(y < 0) return -bigmul(x, -y, lim);
return y == 0 or x <= lim / y ? x * y : lim;
}
template <class T> T POW(T x, int n) {
T res = 1;
for(; n; n >>= 1, x *= x)
if(n & 1) res *= x;
return res;
}
template <class T, class S> T POW(T x, S n, const ll &mod) {
T res = 1;
x %= mod;
for(; n; n >>= 1, x = x * x % mod)
if(n & 1) res = res * x % mod;
return res;
}
vector<pll> factor(ll x) {
vector<pll> ans;
for(ll i = 2; i * i <= x; i++)
if(x % i == 0) {
ans.push_back({i, 1});
while((x /= i) % i == 0) ans.back().second++;
}
if(x != 1) ans.push_back({x, 1});
return ans;
}
template <class T> vector<T> divisor(T x) {
vector<T> ans;
for(T i = 1; i * i <= x; i++)
if(x % i == 0) {
ans.pb(i);
if(i * i != x) ans.pb(x / i);
}
return ans;
}
template <typename T> void zip(vector<T> &x) {
vector<T> y = x;
UNIQUE(y);
for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
template <class S> void fold_in(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {
for(auto e : a) v.emplace_back(e);
fold_in(v, tail...);
}
template <class S> void renumber(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {
for(auto &&e : a) e = lb(v, e);
renumber(v, tail...);
}
template <class S, class... Args> vector<S> zip(vector<S> &head, Args &&...args) {
vector<S> v;
fold_in(v, head, args...);
sort(all(v)), v.erase(unique(all(v)), v.end());
renumber(v, head, args...);
return v;
}
template <typename S> void rearrange(const vector<S> &id) {}
template <typename S, typename T> void rearrange_exec(const vector<S> &id, vector<T> &v) {
vector<T> w(v.size());
rep(i, si(id)) w[i] = v[id[i]];
v.swap(w);
}
// 並び替える順番, 並び替える vector 達
template <typename S, typename Head, typename... Tail> void rearrange(const vector<S> &id, Head &a, Tail &...tail) {
rearrange_exec(id, a);
rearrange(id, tail...);
}
template <typename T> vector<T> RUI(const vector<T> &v) {
vector<T> res(v.size() + 1);
for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];
return res;
}
template <typename T> void zeta_supersetsum(vector<T> &f) {
int n = f.size();
for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] += f[b | i];
}
template <typename T> void zeta_subsetsum(vector<T> &f) {
int n = f.size();
for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] += f[b];
}
template <typename T> void mobius_subset(vector<T> &f) {
int n = f.size();
for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] -= f[b | i];
}
template <typename T> void mobius_superset(vector<T> &f) {
int n = f.size();
for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] -= f[b];
}
// 反時計周りに 90 度回転
template <typename T> void rot(vector<vector<T>> &v) {
if(empty(v)) return;
int n = v.size(), m = v[0].size();
vector<vector<T>> res(m, vector<T>(n));
rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];
v.swap(res);
}
vector<int> counter(const vector<int> &v, int max_num = -1) {
if(max_num == -1) max_num = MAX(v);
vector<int> res(max_num + 1);
fore(e, v) res[e]++;
return res;
}
// x in [l, r)
template <class T, class S> bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; }
template <class T, class S> bool inc(const T &x, const pair<S, S> &p) { return p.first <= x and x < p.second; }
// 便利関数
constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }
constexpr ll tri(ll n) { return n * (n + 1) / 2; }
// l + ... + r
constexpr ll tri(ll l, ll r) { return (l + r) * (r - l + 1) / 2; }
ll max(int x, ll y) { return max((ll)x, y); }
ll max(ll x, int y) { return max(x, (ll)y); }
int min(int x, ll y) { return min((ll)x, y); }
int min(ll x, int y) { return min(x, (ll)y); }
// bit 演算系
#define bit(i) (1LL << i) // (1 << i)
#define test(b, i) (b >> i & 1) // b の i bit 目が立っているか
ll pow2(int i) { return 1LL << i; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
// int allbit(int n) { return (1 << n) - 1; }
constexpr ll mask(int n) { return (1LL << n) - 1; }
// int popcount(signed t) { return __builtin_popcount(t); }
// int popcount(ll t) { return __builtin_popcountll(t); }
int popcount(uint64_t t) { return __builtin_popcountll(t); }
static inline uint64_t popcount64(uint64_t x) {
uint64_t m1 = 0x5555555555555555ll;
uint64_t m2 = 0x3333333333333333ll;
uint64_t m4 = 0x0F0F0F0F0F0F0F0Fll;
uint64_t h01 = 0x0101010101010101ll;
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x = (x + (x >> 4)) & m4;
return (x * h01) >> 56;
}
bool ispow2(int i) { return i && (i & -i) == i; }
ll rnd(ll l, ll r) { //[l, r)
#ifdef noimi
static mt19937_64 gen;
#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
return uniform_int_distribution<ll>(l, r - 1)(gen);
}
ll rnd(ll n) { return rnd(0, n); }
template <class t> void random_shuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); }
int in() {
int x;
cin >> x;
return x;
}
ll lin() {
unsigned long long x;
cin >> x;
return x;
}
template <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }
template <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }
template <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }
template <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }
template <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }
template <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }
template <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }
template <class T> vector<T> &operator++(vector<T> &v) {
fore(e, v) e++;
return v;
}
template <class T> vector<T> operator++(vector<T> &v, int) {
auto res = v;
fore(e, v) e++;
return res;
}
template <class T> vector<T> &operator--(vector<T> &v) {
fore(e, v) e--;
return v;
}
template <class T> vector<T> operator--(vector<T> &v, int) {
auto res = v;
fore(e, v) e--;
return res;
}
template <class T> void connect(vector<T> &l, const vector<T> &r) { fore(e, r) l.eb(e); }
template <class T> vector<T> operator+(const vector<T> &l, const vector<T> &r) {
vector<T> res(max(si(l), si(r)));
rep(i, si(l)) res[i] += l[i];
rep(i, si(r)) res[i] += r[i];
return res;
}
template <class T> vector<T> operator-(const vector<T> &l, const vector<T> &r) {
vector<T> res(max(si(l), si(r)));
rep(i, si(l)) res[i] += l[i];
rep(i, si(r)) res[i] -= r[i];
return res;
}
template <class T> vector<T> &operator+=(const vector<T> &l, const vector<T> &r) {
if(si(l) < si(r)) l.resize(si(r));
rep(i, si(r)) l[i] += r[i];
return l;
}
template <class T> vector<T> &operator-=(const vector<T> &l, const vector<T> &r) {
if(si(l) < si(r)) l.resize(si(r));
rep(i, si(r)) l[i] -= r[i];
return l;
}
template <class T> vector<T> &operator+=(vector<T> &v, const T &x) {
fore(e, v) e += x;
return v;
}
template <class T> vector<T> &operator-=(vector<T> &v, const T &x) {
fore(e, v) e -= x;
return v;
}
template <typename T> struct edge {
int from, to;
T cost;
int id;
edge(int to, T cost) : from(-1), to(to), cost(cost) {}
edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}
constexpr bool operator<(const edge<T> &rhs) const noexcept { return cost < rhs.cost; }
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
friend ostream operator<<(ostream &os, const edge &e) { return os << e.to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T = int> Edges<T> read_edges(int m, bool weighted = false) {
Edges<T> res;
res.reserve(m);
for(int i = 0; i < m; i++) {
int u, v, c = 0;
scan(u), scan(v), u--, v--;
if(weighted) scan(c);
res.eb(u, v, c, i);
}
return res;
}
using Tree = vector<vector<int>>;
using Graph = vector<vector<int>>;
template <class T> using Wgraph = vector<vector<edge<T>>>;
Graph getG(int n, int m = -1, bool directed = false, int margin = 1) {
Tree res(n);
if(m == -1) m = n - 1;
while(m--) {
int a, b;
cin >> a >> b;
a -= margin, b -= margin;
res[a].emplace_back(b);
if(!directed) res[b].emplace_back(a);
}
return res;
}
Graph getTreeFromPar(int n, int margin = 1) {
Graph res(n);
for(int i = 1; i < n; i++) {
int a;
cin >> a;
res[a - margin].emplace_back(i);
}
return res;
}
template <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {
Wgraph<T> res(n);
if(m == -1) m = n - 1;
while(m--) {
int a, b;
T c;
scan(a), scan(b), scan(c);
a -= margin, b -= margin;
res[a].emplace_back(b, c);
if(!directed) res[b].emplace_back(a, c);
}
return res;
}
void add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); }
template <class S, class T> void add(Wgraph<S> &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); }
#define TEST \
INT(testcases); \
while(testcases--)
i128 abs(const i128 &x) { return x > 0 ? x : -x; }
istream &operator>>(istream &is, i128 &v) {
string s;
is >> s;
v = 0;
for(int i = 0; i < (int)s.size(); i++) {
if(isdigit(s[i])) { v = v * 10 + s[i] - '0'; }
}
if(s[0] == '-') { v *= -1; }
return is;
}
ostream &operator<<(ostream &os, const i128 &v) {
if(v == 0) { return (os << "0"); }
i128 num = v;
if(v < 0) {
os << '-';
num = -num;
}
string s;
for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }
reverse(s.begin(), s.end());
return (os << s);
}
namespace aux {
template <typename T, unsigned N, unsigned L> struct tp {
static void output(std::ostream &os, const T &v) {
os << std::get<N>(v) << (&os == &cerr ? ", " : " ");
tp<T, N + 1, L>::output(os, v);
}
};
template <typename T, unsigned N> struct tp<T, N, N> {
static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }
};
} // namespace aux
template <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {
if(&os == &cerr) { os << '('; }
aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);
if(&os == &cerr) { os << ')'; }
return os;
}
template <typename T, typename S, typename U> std::ostream &operator<<(std::ostream &os, const priority_queue<T, S, U> &_pq) {
auto pq = _pq;
vector<T> res;
while(!empty(pq)) res.emplace_back(pq.top()), pq.pop();
return os << res;
}
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {
if(&os == &cerr) { return os << "(" << p.first << ", " << p.second << ")"; }
return os << p.first << " " << p.second;
}
template <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {
bool f = true;
if(&os == &cerr) os << "[";
for(auto &y : x) {
if(&os == &cerr)
os << (f ? "" : ", ") << y;
else
os << (f ? "" : " ") << y;
f = false;
}
if(&os == &cerr) os << "]";
return os;
}
#define dump(...) static_cast<void>(0)
#define dbg(...) static_cast<void>(0)
void OUT() { cout << endl; }
template <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {
cout << head;
if(sizeof...(tail)) cout << ' ';
OUT(tail...);
}
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
template <class T, class S> constexpr pair<T, S> inf<pair<T, S>> = {inf<T>, inf<S>};
template <class T> void OUT2(const T &t, T INF = inf<T>, T res = -1) { OUT(t != INF ? t : res); }
template <class T> void OUT2(vector<T> &v, T INF = inf<T>, T res = -1) {
fore(e, v) if(e == INF) e = res;
OUT(v);
fore(e, v) if(e == res) e = INF;
}
template <class F> struct REC {
F f;
REC(F &&f_) : f(forward<F>(f_)) {}
template <class... Args> auto operator()(Args &&...args) const { return f(*this, forward<Args>(args)...); }
};
template <class S> vector<pair<S, int>> runLength(const vector<S> &v) {
vector<pair<S, int>> res;
for(auto &e : v) {
if(res.empty() or res.back().fi != e)
res.eb(e, 1);
else
res.back().se++;
}
return res;
}
vector<pair<char, int>> runLength(const string &v) {
vector<pair<char, int>> res;
for(auto &e : v) {
if(res.empty() or res.back().fi != e)
res.eb(e, 1);
else
res.back().se++;
}
return res;
}
struct string_converter {
char start = 0;
char type(const char &c) const { return (islower(c) ? 'a' : isupper(c) ? 'A' : isdigit(c) ? '0' : 0); }
int convert(const char &c) {
if(!start) start = type(c);
return c - start;
}
int convert(const char &c, const string &chars) { return chars.find(c); }
template <typename T> auto convert(const T &v) {
vector<decltype(convert(v[0]))> ret;
ret.reserve(size(v));
for(auto &&e : v) ret.emplace_back(convert(e));
return ret;
}
template <typename T> auto convert(const T &v, const string &chars) {
vector<decltype(convert(v[0], chars))> ret;
ret.reserve(size(v));
for(auto &&e : v) ret.emplace_back(convert(e, chars));
return ret;
}
int operator()(const char &v, char s = 0) {
start = s;
return convert(v);
}
int operator()(const char &v, const string &chars) { return convert(v, chars); }
template <typename T> auto operator()(const T &v, char s = 0) {
start = s;
return convert(v);
}
template <typename T> auto operator()(const T &v, const string &chars) { return convert(v, chars); }
} toint;
template <class T, class F> T bin_search(T ok, T ng, const F &f) {
while(abs(ok - ng) > 1) {
T mid = ok + ng >> 1;
(f(mid) ? ok : ng) = mid;
}
return ok;
}
template <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {
while(iter--) {
T mid = (ok + ng) / 2;
(f(mid) ? ok : ng) = mid;
}
return ok;
}
struct Setup_io {
Setup_io() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(11);
}
} setup_io;
#endif
#pragma endregion
namespace internal {
template <typename T>
using is_broadly_integral = typename conditional_t<is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>, true_type, false_type>::type;
template <typename T> using is_broadly_signed = typename conditional_t<is_signed_v<T> || is_same_v<T, __int128_t>, true_type, false_type>::type;
template <typename T> using is_broadly_unsigned = typename conditional_t<is_unsigned_v<T> || is_same_v<T, __uint128_t>, true_type, false_type>::type;
#define ENABLE_VALUE(x) template <typename T> constexpr bool x##_v = x<T>::value;
ENABLE_VALUE(is_broadly_integral);
ENABLE_VALUE(is_broadly_signed);
ENABLE_VALUE(is_broadly_unsigned);
#undef ENABLE_VALUE
#define ENABLE_HAS_TYPE(var) \
template <class, class = void> struct has_##var : std::false_type {}; \
template <class T> struct has_##var<T, std::void_t<typename T::var>> : std::true_type {}; \
template <class T> constexpr auto has_##var##_v = has_##var<T>::value;
} // namespace internal
template <uint32_t mod> struct LazyMontgomeryModInt {
using mint = LazyMontgomeryModInt;
using i32 = int32_t;
using u32 = uint32_t;
using u64 = uint64_t;
static constexpr u32 get_r() {
u32 ret = mod;
for(i32 i = 0; i < 4; ++i) ret *= 2 - mod * ret;
return ret;
}
static constexpr u32 r = get_r();
static constexpr u32 n2 = -u64(mod) % mod;
static_assert(mod < (1 << 30), "invalid, mod >= 2 ^ 30");
static_assert((mod & 1) == 1, "invalid, mod % 2 == 0");
static_assert(r * mod == 1, "this code has bugs.");
u32 a;
constexpr LazyMontgomeryModInt() : a(0) {}
constexpr LazyMontgomeryModInt(const int64_t &b) : a(reduce(u64(b % mod + mod) * n2)){};
static constexpr u32 reduce(const u64 &b) { return (b + u64(u32(b) * u32(-r)) * mod) >> 32; }
constexpr mint &operator+=(const mint &b) {
if(i32(a += b.a - 2 * mod) < 0) a += 2 * mod;
return *this;
}
constexpr mint &operator-=(const mint &b) {
if(i32(a -= b.a) < 0) a += 2 * mod;
return *this;
}
constexpr mint &operator*=(const mint &b) {
a = reduce(u64(a) * b.a);
return *this;
}
constexpr mint &operator/=(const mint &b) {
*this *= b.inverse();
return *this;
}
constexpr mint operator+(const mint &b) const { return mint(*this) += b; }
constexpr mint operator-(const mint &b) const { return mint(*this) -= b; }
constexpr mint operator*(const mint &b) const { return mint(*this) *= b; }
constexpr mint operator/(const mint &b) const { return mint(*this) /= b; }
constexpr bool operator==(const mint &b) const { return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a); }
constexpr bool operator!=(const mint &b) const { return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a); }
constexpr mint operator-() const { return mint() - mint(*this); }
constexpr mint operator+() const { return mint(*this); }
constexpr mint pow(u64 n) const {
mint ret(1), mul(*this);
while(n > 0) {
if(n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
constexpr mint inverse() const {
int x = get(), y = mod, u = 1, v = 0, t = 0, tmp = 0;
while(y > 0) {
t = x / y;
x -= t * y, u -= t * v;
tmp = x, x = y, y = tmp;
tmp = u, u = v, v = tmp;
}
return mint{u};
}
friend ostream &operator<<(ostream &os, const mint &b) { return os << b.get(); }
friend istream &operator>>(istream &is, mint &b) {
int64_t t;
is >> t;
b = LazyMontgomeryModInt<mod>(t);
return (is);
}
constexpr u32 get() const {
u32 ret = reduce(a);
return ret >= mod ? ret - mod : ret;
}
static constexpr u32 get_mod() { return mod; }
};
template <typename mint> struct NTT {
static constexpr uint32_t get_pr() {
uint32_t _mod = mint::get_mod();
using u64 = uint64_t;
u64 ds[32] = {};
int idx = 0;
u64 m = _mod - 1;
for(u64 i = 2; i * i <= m; ++i) {
if(m % i == 0) {
ds[idx++] = i;
while(m % i == 0) m /= i;
}
}
if(m != 1) ds[idx++] = m;
uint32_t _pr = 2;
while(1) {
int flg = 1;
for(int i = 0; i < idx; ++i) {
u64 a = _pr, b = (_mod - 1) / ds[i], r = 1;
while(b) {
if(b & 1) r = r * a % _mod;
a = a * a % _mod;
b >>= 1;
}
if(r == 1) {
flg = 0;
break;
}
}
if(flg == 1) break;
++_pr;
}
return _pr;
};
static constexpr uint32_t mod = mint::get_mod();
static constexpr uint32_t pr = get_pr();
static constexpr int level = __builtin_ctzll(mod - 1);
mint dw[level], dy[level];
void setwy(int k) {
mint w[level], y[level];
w[k - 1] = mint(pr).pow((mod - 1) / (1 << k));
y[k - 1] = w[k - 1].inverse();
for(int i = k - 2; i > 0; --i) w[i] = w[i + 1] * w[i + 1], y[i] = y[i + 1] * y[i + 1];
dw[1] = w[1], dy[1] = y[1], dw[2] = w[2], dy[2] = y[2];
for(int i = 3; i < k; ++i) {
dw[i] = dw[i - 1] * y[i - 2] * w[i];
dy[i] = dy[i - 1] * w[i - 2] * y[i];
}
}
NTT() { setwy(level); }
void fft4(vector<mint> &a, int k) {
if((int)a.size() <= 1) return;
if(k == 1) {
mint a1 = a[1];
a[1] = a[0] - a[1];
a[0] = a[0] + a1;
return;
}
if(k & 1) {
int v = 1 << (k - 1);
for(int j = 0; j < v; ++j) {
mint ajv = a[j + v];
a[j + v] = a[j] - ajv;
a[j] += ajv;
}
}
int u = 1 << (2 + (k & 1));
int v = 1 << (k - 2 - (k & 1));
mint one = mint(1);
mint imag = dw[1];
while(v) {
// jh = 0
{
int j0 = 0;
int j1 = v;
int j2 = j1 + v;
int j3 = j2 + v;
for(; j0 < v; ++j0, ++j1, ++j2, ++j3) {
mint t0 = a[j0], t1 = a[j1], t2 = a[j2], t3 = a[j3];
mint t0p2 = t0 + t2, t1p3 = t1 + t3;
mint t0m2 = t0 - t2, t1m3 = (t1 - t3) * imag;
a[j0] = t0p2 + t1p3, a[j1] = t0p2 - t1p3;
a[j2] = t0m2 + t1m3, a[j3] = t0m2 - t1m3;
}
}
// jh >= 1
mint ww = one, xx = one * dw[2], wx = one;
for(int jh = 4; jh < u;) {
ww = xx * xx, wx = ww * xx;
int j0 = jh * v;
int je = j0 + v;
int j2 = je + v;
for(; j0 < je; ++j0, ++j2) {
mint t0 = a[j0], t1 = a[j0 + v] * xx, t2 = a[j2] * ww, t3 = a[j2 + v] * wx;
mint t0p2 = t0 + t2, t1p3 = t1 + t3;
mint t0m2 = t0 - t2, t1m3 = (t1 - t3) * imag;
a[j0] = t0p2 + t1p3, a[j0 + v] = t0p2 - t1p3;
a[j2] = t0m2 + t1m3, a[j2 + v] = t0m2 - t1m3;
}
xx *= dw[__builtin_ctzll((jh += 4))];
}
u <<= 2;
v >>= 2;
}
}
void ifft4(vector<mint> &a, int k) {
if((int)a.size() <= 1) return;
if(k == 1) {
mint a1 = a[1];
a[1] = a[0] - a[1];
a[0] = a[0] + a1;
return;
}
int u = 1 << (k - 2);
int v = 1;
mint one = mint(1);
mint imag = dy[1];
while(u) {
// jh = 0
{
int j0 = 0;
int j1 = v;
int j2 = v + v;
int j3 = j2 + v;
for(; j0 < v; ++j0, ++j1, ++j2, ++j3) {
mint t0 = a[j0], t1 = a[j1], t2 = a[j2], t3 = a[j3];
mint t0p1 = t0 + t1, t2p3 = t2 + t3;
mint t0m1 = t0 - t1, t2m3 = (t2 - t3) * imag;
a[j0] = t0p1 + t2p3, a[j2] = t0p1 - t2p3;
a[j1] = t0m1 + t2m3, a[j3] = t0m1 - t2m3;
}
}
// jh >= 1
mint ww = one, xx = one * dy[2], yy = one;
u <<= 2;
for(int jh = 4; jh < u;) {
ww = xx * xx, yy = xx * imag;
int j0 = jh * v;
int je = j0 + v;
int j2 = je + v;
for(; j0 < je; ++j0, ++j2) {
mint t0 = a[j0], t1 = a[j0 + v], t2 = a[j2], t3 = a[j2 + v];
mint t0p1 = t0 + t1, t2p3 = t2 + t3;
mint t0m1 = (t0 - t1) * xx, t2m3 = (t2 - t3) * yy;
a[j0] = t0p1 + t2p3, a[j2] = (t0p1 - t2p3) * ww;
a[j0 + v] = t0m1 + t2m3, a[j2 + v] = (t0m1 - t2m3) * ww;
}
xx *= dy[__builtin_ctzll(jh += 4)];
}
u >>= 4;
v <<= 2;
}
if(k & 1) {
u = 1 << (k - 1);
for(int j = 0; j < u; ++j) {
mint ajv = a[j] - a[j + u];
a[j] += a[j + u];
a[j + u] = ajv;
}
}
}
void ntt(vector<mint> &a) {
if((int)a.size() <= 1) return;
fft4(a, __builtin_ctz(a.size()));
}
void intt(vector<mint> &a) {
if((int)a.size() <= 1) return;
ifft4(a, __builtin_ctz(a.size()));
mint iv = mint(a.size()).inverse();
for(auto &x : a) x *= iv;
}
vector<mint> multiply(const vector<mint> &a, const vector<mint> &b) {
int l = a.size() + b.size() - 1;
if(min<int>(a.size(), b.size()) <= 40) {
vector<mint> s(l);
for(int i = 0; i < (int)a.size(); ++i)
for(int j = 0; j < (int)b.size(); ++j) s[i + j] += a[i] * b[j];
return s;
}
int k = 2, M = 4;
while(M < l) M <<= 1, ++k;
setwy(k);
vector<mint> s(M);
for(int i = 0; i < (int)a.size(); ++i) s[i] = a[i];
fft4(s, k);
if(a.size() == b.size() && a == b) {
for(int i = 0; i < M; ++i) s[i] *= s[i];
} else {
vector<mint> t(M);
for(int i = 0; i < (int)b.size(); ++i) t[i] = b[i];
fft4(t, k);
for(int i = 0; i < M; ++i) s[i] *= t[i];
}
ifft4(s, k);
s.resize(l);
mint invm = mint(M).inverse();
for(int i = 0; i < l; ++i) s[i] *= invm;
return s;
}
void ntt_doubling(vector<mint> &a) {
int M = (int)a.size();
auto b = a;
intt(b);
mint r = 1, zeta = mint(pr).pow((mint::get_mod() - 1) / (M << 1));
for(int i = 0; i < M; i++) b[i] *= r, r *= zeta;
ntt(b);
copy(begin(b), end(b), back_inserter(a));
}
};
namespace ArbitraryNTT {
using i64 = int64_t;
using u128 = __uint128_t;
constexpr int32_t m0 = 167772161;
constexpr int32_t m1 = 469762049;
constexpr int32_t m2 = 754974721;
using mint0 = LazyMontgomeryModInt<m0>;
using mint1 = LazyMontgomeryModInt<m1>;
using mint2 = LazyMontgomeryModInt<m2>;
constexpr int r01 = mint1(m0).inverse().get();
constexpr int r02 = mint2(m0).inverse().get();
constexpr int r12 = mint2(m1).inverse().get();
constexpr int r02r12 = i64(r02) * r12 % m2;
constexpr i64 w1 = m0;
constexpr i64 w2 = i64(m0) * m1;
template <typename T, typename submint> vector<submint> mul(const vector<T> &a, const vector<T> &b) {
static NTT<submint> ntt;
vector<submint> s(a.size()), t(b.size());
for(int i = 0; i < (int)a.size(); ++i) s[i] = i64(a[i] % submint::get_mod());
for(int i = 0; i < (int)b.size(); ++i) t[i] = i64(b[i] % submint::get_mod());
return ntt.multiply(s, t);
}
template <typename T> vector<int> multiply(const vector<T> &s, const vector<T> &t, int mod) {
auto d0 = mul<T, mint0>(s, t);
auto d1 = mul<T, mint1>(s, t);
auto d2 = mul<T, mint2>(s, t);
int n = d0.size();
vector<int> ret(n);
const int W1 = w1 % mod;
const int W2 = w2 % mod;
for(int i = 0; i < n; i++) {
int n1 = d1[i].get(), n2 = d2[i].get(), a = d0[i].get();
int b = i64(n1 + m1 - a) * r01 % m1;
int c = (i64(n2 + m2 - a) * r02r12 + i64(m2 - b) * r12) % m2;
ret[i] = (i64(a) + i64(b) * W1 + i64(c) * W2) % mod;
}
return ret;
}
template <typename mint> vector<mint> multiply(const vector<mint> &a, const vector<mint> &b) {
if(a.size() == 0 && b.size() == 0) return {};
if(min<int>(a.size(), b.size()) < 128) {
vector<mint> ret(a.size() + b.size() - 1);
for(int i = 0; i < (int)a.size(); ++i)
for(int j = 0; j < (int)b.size(); ++j) ret[i + j] += a[i] * b[j];
return ret;
}
vector<int> s(a.size()), t(b.size());
for(int i = 0; i < (int)a.size(); ++i) s[i] = a[i].get();
for(int i = 0; i < (int)b.size(); ++i) t[i] = b[i].get();
vector<int> u = multiply<int>(s, t, mint::get_mod());
vector<mint> ret(u.size());
for(int i = 0; i < (int)u.size(); ++i) ret[i] = mint(u[i]);
return ret;
}
template <typename T> vector<u128> multiply_u128(const vector<T> &s, const vector<T> &t) {
if(s.size() == 0 && t.size() == 0) return {};
if(min<int>(s.size(), t.size()) < 128) {
vector<u128> ret(s.size() + t.size() - 1);
for(int i = 0; i < (int)s.size(); ++i)
for(int j = 0; j < (int)t.size(); ++j) ret[i + j] += i64(s[i]) * t[j];
return ret;
}
auto d0 = mul<T, mint0>(s, t);
auto d1 = mul<T, mint1>(s, t);
auto d2 = mul<T, mint2>(s, t);
int n = d0.size();
vector<u128> ret(n);
for(int i = 0; i < n; i++) {
i64 n1 = d1[i].get(), n2 = d2[i].get();
i64 a = d0[i].get();
i64 b = (n1 + m1 - a) * r01 % m1;
i64 c = ((n2 + m2 - a) * r02r12 + (m2 - b) * r12) % m2;
ret[i] = a + b * w1 + u128(c) * w2;
}
return ret;
}
} // namespace ArbitraryNTT
namespace MultiPrecisionIntegerImpl {
struct TENS {
static constexpr int offset = 30;
constexpr TENS() : _tend() {
_tend[offset] = 1;
for(int i = 1; i <= offset; i++) {
_tend[offset + i] = _tend[offset + i - 1] * 10.0;
_tend[offset - i] = 1.0 / _tend[offset + i];
}
}
long double ten_ld(int n) const {
assert(-offset <= n and n <= offset);
return _tend[n + offset];
}
private:
long double _tend[offset * 2 + 1];
};
} // namespace MultiPrecisionIntegerImpl
// 0 は neg=false, dat={} として扱う
struct MultiPrecisionInteger {
using M = MultiPrecisionInteger;
inline constexpr static MultiPrecisionIntegerImpl::TENS tens = {};
static constexpr int D = 1000000000;
static constexpr int logD = 9;
bool neg;
vector<int> dat;
MultiPrecisionInteger() : neg(false), dat() {}
MultiPrecisionInteger(bool n, const vector<int> &d) : neg(n), dat(d) {}
template <typename I, enable_if_t<internal::is_broadly_integral_v<I>> * = nullptr> MultiPrecisionInteger(I x) : neg(false) {
if constexpr(internal::is_broadly_signed_v<I>) {
if(x < 0) neg = true, x = -x;
}
while(x) dat.push_back(x % D), x /= D;
}
MultiPrecisionInteger(const string &S) : neg(false) {
assert(!S.empty());
if(S.size() == 1u && S[0] == '0') return;
int l = 0;
if(S[0] == '-') ++l, neg = true;
for(int ie = S.size(); l < ie; ie -= logD) {
int is = max(l, ie - logD);
long long x = 0;
for(int i = is; i < ie; i++) x = x * 10 + S[i] - '0';
dat.push_back(x);
}
}
friend M operator+(const M &lhs, const M &rhs) {
if(lhs.neg == rhs.neg) return {lhs.neg, _add(lhs.dat, rhs.dat)};
if(_leq(lhs.dat, rhs.dat)) {
// |l| <= |r|
auto c = _sub(rhs.dat, lhs.dat);
bool n = _is_zero(c) ? false : rhs.neg;
return {n, c};
}
auto c = _sub(lhs.dat, rhs.dat);
bool n = _is_zero(c) ? false : lhs.neg;
return {n, c};
}
friend M operator-(const M &lhs, const M &rhs) { return lhs + (-rhs); }
friend M operator*(const M &lhs, const M &rhs) {
auto c = _mul(lhs.dat, rhs.dat);
bool n = _is_zero(c) ? false : (lhs.neg ^ rhs.neg);
return {n, c};
}
friend pair<M, M> divmod(const M &lhs, const M &rhs) {
auto dm = _divmod_newton(lhs.dat, rhs.dat);
bool dn = _is_zero(dm.first) ? false : lhs.neg != rhs.neg;
bool mn = _is_zero(dm.second) ? false : lhs.neg;
return {M{dn, dm.first}, M{mn, dm.second}};
}
friend M operator/(const M &lhs, const M &rhs) { return divmod(lhs, rhs).first; }
friend M operator%(const M &lhs, const M &rhs) { return divmod(lhs, rhs).second; }
M &operator+=(const M &rhs) { return (*this) = (*this) + rhs; }
M &operator-=(const M &rhs) { return (*this) = (*this) - rhs; }
M &operator*=(const M &rhs) { return (*this) = (*this) * rhs; }
M &operator/=(const M &rhs) { return (*this) = (*this) / rhs; }
M &operator%=(const M &rhs) { return (*this) = (*this) % rhs; }
M operator-() const {
if(is_zero()) return *this;
return {!neg, dat};
}
M operator+() const { return *this; }
friend M abs(const M &m) { return {false, m.dat}; }
bool is_zero() const { return _is_zero(dat); }
friend bool operator==(const M &lhs, const M &rhs) { return lhs.neg == rhs.neg && lhs.dat == rhs.dat; }
friend bool operator!=(const M &lhs, const M &rhs) { return lhs.neg != rhs.neg || lhs.dat != rhs.dat; }
friend bool operator<(const M &lhs, const M &rhs) {
if(lhs == rhs) return false;
return _neq_lt(lhs, rhs);
}
friend bool operator<=(const M &lhs, const M &rhs) {
if(lhs == rhs) return true;
return _neq_lt(lhs, rhs);
}
friend bool operator>(const M &lhs, const M &rhs) {
if(lhs == rhs) return false;
return _neq_lt(rhs, lhs);
}
friend bool operator>=(const M &lhs, const M &rhs) {
if(lhs == rhs) return true;
return _neq_lt(rhs, lhs);
}
// a * 10^b (1 <= |a| < 10) の形で渡す
// 相対誤差:10^{-16} ~ 10^{-19} 程度 (処理系依存)
pair<long double, int> dfp() const {
if(is_zero()) return {0, 0};
int l = max<int>(0, _size() - 3);
int b = logD * l;
string prefix{};
for(int i = _size() - 1; i >= l; i--) { prefix += _itos(dat[i], i != _size() - 1); }
b += prefix.size() - 1;
long double a = 0;
for(auto &c : prefix) a = a * 10.0 + (c - '0');
a *= tens.ten_ld(-((int)prefix.size()) + 1);
a = clamp<long double>(a, 1.0, nextafterl(10.0, 1.0));
if(neg) a = -a;
return {a, b};
}
string to_string() const {
if(is_zero()) return "0";
string res;
if(neg) res.push_back('-');
for(int i = _size() - 1; i >= 0; i--) { res += _itos(dat[i], i != _size() - 1); }
return res;
}
long double to_ld() const {
auto [a, b] = dfp();
if(-tens.offset <= b and b <= tens.offset) { return a * tens.ten_ld(b); }
return a * powl(10, b);
}
long long to_ll() const {
long long res = _to_ll(dat);
return neg ? -res : res;
}
__int128_t to_i128() const {
__int128_t res = _to_i128(dat);
return neg ? -res : res;
}
friend istream &operator>>(istream &is, M &m) {
string s;
is >> s;
m = M{s};
return is;
}
friend ostream &operator<<(ostream &os, const M &m) { return os << m.to_string(); }
// 内部の関数をテスト
static void _test_private_function(const M &, const M &);
// private:
// size
int _size() const { return dat.size(); }
// a == b
static bool _eq(const vector<int> &a, const vector<int> &b) { return a == b; }
// a < b
static bool _lt(const vector<int> &a, const vector<int> &b) {
if(a.size() != b.size()) return a.size() < b.size();
for(int i = a.size() - 1; i >= 0; i--) {
if(a[i] != b[i]) return a[i] < b[i];
}
return false;
}
// a <= b
static bool _leq(const vector<int> &a, const vector<int> &b) { return _eq(a, b) || _lt(a, b); }
// a < b (s.t. a != b)
static bool _neq_lt(const M &lhs, const M &rhs) {
assert(lhs != rhs);
if(lhs.neg != rhs.neg) return lhs.neg;
bool f = _lt(lhs.dat, rhs.dat);
if(f) return !lhs.neg;
return lhs.neg;
}
// a == 0
static bool _is_zero(const vector<int> &a) { return a.empty(); }
// a == 1
static bool _is_one(const vector<int> &a) { return (int)a.size() == 1 && a[0] == 1; }
// 末尾 0 を削除
static void _shrink(vector<int> &a) {
while(a.size() && a.back() == 0) a.pop_back();
}
// 末尾 0 を削除
void _shrink() {
while(_size() && dat.back() == 0) dat.pop_back();
}
// a + b
static vector<int> _add(const vector<int> &a, const vector<int> &b) {
vector<int> c(max(a.size(), b.size()) + 1);
for(int i = 0; i < (int)a.size(); i++) c[i] += a[i];
for(int i = 0; i < (int)b.size(); i++) c[i] += b[i];
for(int i = 0; i < (int)c.size() - 1; i++) {
if(c[i] >= D) c[i] -= D, c[i + 1]++;
}
_shrink(c);
return c;
}
// a - b
static vector<int> _sub(const vector<int> &a, const vector<int> &b) {
assert(_leq(b, a));
vector<int> c{a};
int borrow = 0;
for(int i = 0; i < (int)a.size(); i++) {
if(i < (int)b.size()) borrow += b[i];
c[i] -= borrow;
borrow = 0;
if(c[i] < 0) c[i] += D, borrow = 1;
}
assert(borrow == 0);
_shrink(c);
return c;
}
// a * b (fft)
static vector<int> _mul_fft(const vector<int> &a, const vector<int> &b) {
if(a.empty() || b.empty()) return {};
auto m = ArbitraryNTT::multiply_u128(a, b);
vector<int> c;
c.reserve(m.size() + 3);
__uint128_t x = 0;
for(int i = 0;; i++) {
if(i >= (int)m.size() && x == 0) break;
if(i < (int)m.size()) x += m[i];
c.push_back(x % D);
x /= D;
}
_shrink(c);
return c;
}
// a * b (naive)
static vector<int> _mul_naive(const vector<int> &a, const vector<int> &b) {
if(a.empty() || b.empty()) return {};
vector<long long> prod(a.size() + b.size() - 1 + 1);
for(int i = 0; i < (int)a.size(); i++) {
for(int j = 0; j < (int)b.size(); j++) {
long long p = 1LL * a[i] * b[j];
prod[i + j] += p;
if(prod[i + j] >= (4LL * D * D)) {
prod[i + j] -= 4LL * D * D;
prod[i + j + 1] += 4LL * D;
}
}
}
vector<int> c(prod.size() + 1);
long long x = 0;
int i = 0;
for(; i < (int)prod.size(); i++) x += prod[i], c[i] = x % D, x /= D;
while(x) c[i] = x % D, x /= D, i++;
_shrink(c);
return c;
}
// a * b
static vector<int> _mul(const vector<int> &a, const vector<int> &b) {
if(_is_zero(a) || _is_zero(b)) return {};
if(_is_one(a)) return b;
if(_is_one(b)) return a;
if(min<int>(a.size(), b.size()) <= 128) { return a.size() < b.size() ? _mul_naive(b, a) : _mul_naive(a, b); }
return _mul_fft(a, b);
}
// 0 <= A < 1e18, 1 <= B < 1e9
static pair<vector<int>, vector<int>> _divmod_li(const vector<int> &a, const vector<int> &b) {
assert(0 <= (int)a.size() && (int)a.size() <= 2);
assert((int)b.size() == 1);
long long va = _to_ll(a);
int vb = b[0];
return {_integer_to_vec(va / vb), _integer_to_vec(va % vb)};
}
// 0 <= A < 1e18, 1 <= B < 1e18
static pair<vector<int>, vector<int>> _divmod_ll(const vector<int> &a, const vector<int> &b) {
assert(0 <= (int)a.size() && (int)a.size() <= 2);
assert(1 <= (int)b.size() && (int)b.size() <= 2);
long long va = _to_ll(a), vb = _to_ll(b);
return {_integer_to_vec(va / vb), _integer_to_vec(va % vb)};
}
// 1 <= B < 1e9
static pair<vector<int>, vector<int>> _divmod_1e9(const vector<int> &a, const vector<int> &b) {
assert((int)b.size() == 1);
if(b[0] == 1) return {a, {}};
if((int)a.size() <= 2) return _divmod_li(a, b);
vector<int> quo(a.size());
long long d = 0;
int b0 = b[0];
for(int i = a.size() - 1; i >= 0; i--) {
d = d * D + a[i];
assert(d < 1LL * D * b0);
int q = d / b0, r = d % b0;
quo[i] = q, d = r;
}
_shrink(quo);
return {quo, d ? vector<int>{int(d)} : vector<int>{}};
}
// 0 <= A, 1 <= B
static pair<vector<int>, vector<int>> _divmod_naive(const vector<int> &a, const vector<int> &b) {
if(_is_zero(b)) {
cerr << "Divide by Zero Exception" << endl;
exit(1);
}
assert(1 <= (int)b.size());
if((int)b.size() == 1) return _divmod_1e9(a, b);
if(max<int>(a.size(), b.size()) <= 2) return _divmod_ll(a, b);
if(_lt(a, b)) return {{}, a};
// B >= 1e9, A >= B
int norm = D / (b.back() + 1);
vector<int> x = _mul(a, {norm});
vector<int> y = _mul(b, {norm});
int yb = y.back();
vector<int> quo(x.size() - y.size() + 1);
vector<int> rem(x.end() - y.size(), x.end());
for(int i = quo.size() - 1; i >= 0; i--) {
if(rem.size() < y.size()) {
// do nothing
} else if(rem.size() == y.size()) {
if(_leq(y, rem)) { quo[i] = 1, rem = _sub(rem, y); }
} else {
assert(y.size() + 1 == rem.size());
long long rb = 1LL * rem[rem.size() - 1] * D + rem[rem.size() - 2];
int q = rb / yb;
vector<int> yq = _mul(y, {q});
// 真の商は q-2 以上 q+1 以下だが自信が無いので念のため while を回す
while(_lt(rem, yq)) q--, yq = _sub(yq, y);
rem = _sub(rem, yq);
while(_leq(y, rem)) q++, rem = _sub(rem, y);
quo[i] = q;
}
if(i) rem.insert(begin(rem), x[i - 1]);
}
_shrink(quo), _shrink(rem);
auto [q2, r2] = _divmod_1e9(rem, {norm});
assert(_is_zero(r2));
return {quo, q2};
}
// 0 <= A, 1 <= B
static pair<vector<int>, vector<int>> _divmod_dc(const vector<int> &a, const vector<int> &b);
// 1 / a を 絶対誤差 B^{-deg} で求める
static vector<int> _calc_inv(const vector<int> &a, int deg) {
assert(!a.empty() && D / 2 <= a.back() and a.back() < D);
int k = deg, c = a.size();
while(k > 64) k = (k + 1) / 2;
vector<int> z(c + k + 1);
z.back() = 1;
z = _divmod_naive(z, a).first;
while(k < deg) {
vector<int> s = _mul(z, z);
s.insert(begin(s), 0);
int d = min(c, 2 * k + 1);
vector<int> t{end(a) - d, end(a)}, u = _mul(s, t);
u.erase(begin(u), begin(u) + d);
vector<int> w(k + 1), w2 = _add(z, z);
copy(begin(w2), end(w2), back_inserter(w));
z = _sub(w, u);
z.erase(begin(z));
k *= 2;
}
z.erase(begin(z), begin(z) + k - deg);
return z;
}
static pair<vector<int>, vector<int>> _divmod_newton(const vector<int> &a, const vector<int> &b) {
if(_is_zero(b)) {
cerr << "Divide by Zero Exception" << endl;
exit(1);
}
if((int)b.size() <= 64) return _divmod_naive(a, b);
if((int)a.size() - (int)b.size() <= 64) return _divmod_naive(a, b);
int norm = D / (b.back() + 1);
vector<int> x = _mul(a, {norm});
vector<int> y = _mul(b, {norm});
int s = x.size(), t = y.size();
int deg = s - t + 2;
vector<int> z = _calc_inv(y, deg);
vector<int> q = _mul(x, z);
q.erase(begin(q), begin(q) + t + deg);
vector<int> yq = _mul(y, {q});
while(_lt(x, yq)) q = _sub(q, {1}), yq = _sub(yq, y);
vector<int> r = _sub(x, yq);
while(_leq(y, r)) q = _add(q, {1}), r = _sub(r, y);
_shrink(q), _shrink(r);
auto [q2, r2] = _divmod_1e9(r, {norm});
assert(_is_zero(r2));
return {q, q2};
}
// int -> string
// 先頭かどうかに応じて zero padding するかを決める
static string _itos(int x, bool zero_padding) {
assert(0 <= x && x < D);
string res;
for(int i = 0; i < logD; i++) { res.push_back('0' + x % 10), x /= 10; }
if(!zero_padding) {
while(res.size() && res.back() == '0') res.pop_back();
assert(!res.empty());
}
reverse(begin(res), end(res));
return res;
}
// convert ll to vec
template <typename I, enable_if_t<internal::is_broadly_integral_v<I>> * = nullptr> static vector<int> _integer_to_vec(I x) {
if constexpr(internal::is_broadly_signed_v<I>) { assert(x >= 0); }
vector<int> res;
while(x) res.push_back(x % D), x /= D;
return res;
}
static long long _to_ll(const vector<int> &a) {
long long res = 0;
for(int i = (int)a.size() - 1; i >= 0; i--) res = res * D + a[i];
return res;
}
static __int128_t _to_i128(const vector<int> &a) {
__int128_t res = 0;
for(int i = (int)a.size() - 1; i >= 0; i--) res = res * D + a[i];
return res;
}
static void _dump(const vector<int> &a, string s = "") {
if(!s.empty()) cerr << s << " : ";
cerr << "{ ";
for(int i = 0; i < (int)a.size(); i++) cerr << a[i] << ", ";
cerr << "}" << endl;
}
};
using bigint = MultiPrecisionInteger;
namespace RollingHashes {
constexpr ll MOD = (1LL << 61) - 1;
struct modint {
ll a;
constexpr modint(const ll x = 0) noexcept : a((x % MOD + MOD) % MOD) {}
constexpr ll &value() noexcept { return a; }
constexpr const ll &value() const noexcept { return a; }
constexpr modint operator-() const noexcept { return modint() - *this; }
constexpr modint operator+() const noexcept { return *this; }
constexpr modint &operator++() noexcept {
if(++a == MOD) a = 0;
return *this;
}
constexpr modint &operator--() noexcept {
if(!a) a = MOD;
a--;
return *this;
}
constexpr modint operator++(int) {
modint res = *this;
++*this;
return res;
}
constexpr modint operator--(int) {
modint res = *this;
--*this;
return res;
}
constexpr modint &operator+=(const modint rhs) noexcept {
a += rhs.a;
if(a >= MOD) { a -= MOD; }
return *this;
}
constexpr modint &operator-=(const modint rhs) noexcept {
if(a < rhs.a) { a += MOD; }
a -= rhs.a;
return *this;
}
constexpr modint &operator*=(const modint rhs) noexcept {
i128 t = (i128)(a) * (i128)(rhs.a);
t = (t >> 61) + (t & MOD);
if(t >= MOD) t -= MOD;
a = t;
return *this;
}
constexpr modint pow(long long n) const noexcept {
if(n < 0) {
n %= MOD - 1;
n = (MOD - 1) + n;
}
modint x = *this, r = 1;
while(n) {
if(n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
constexpr modint &operator/=(const modint rhs) noexcept { return (*this *= rhs.pow(MOD - 2)); }
constexpr modint inv() const noexcept { return pow(MOD - 2); }
constexpr friend modint operator+(const modint &lhs, const modint &rhs) { return modint(lhs) += modint(rhs); }
constexpr friend modint operator-(const modint &lhs, const modint &rhs) { return modint(lhs) -= modint(rhs); }
constexpr friend modint operator*(const modint &lhs, const modint &rhs) { return modint(lhs) *= modint(rhs); }
constexpr friend modint operator/(const modint &lhs, const modint &rhs) { return modint(lhs) /= modint(rhs); }
constexpr friend modint operator==(const modint &lhs, const modint &rhs) { return lhs.a == rhs.a; }
constexpr friend modint operator!=(const modint &lhs, const modint &rhs) { return lhs.a != rhs.a; }
// constexpr friend modint operator^=(const modint &lhs, const modint &rhs) { return modint(lhs) ^= modint(rhs); }
};
using mint = modint;
using Hash = mint;
static mint base = 5454547;
vector<mint> p{1};
void calc_pow(int N) {
while(si(p) < N) p.eb(p.back() * base);
}
struct RollingHash {
vector<mint> h;
template <typename T> RollingHash(const vector<T> &s) {
int sz = (int)s.size();
h.assign(sz + 1, 0);
p[0] = 1;
if(sz + 1 > (int)p.size()) {
int psz = p.size();
p.resize(sz + 1);
for(int i = psz; i < sz + 1; i++) p[i] = p[i - 1] * base;
}
for(int i = 0; i < sz; i++) { h[i + 1] = h[i] * base + s[i]; }
}
RollingHash(const string &s) : RollingHash(vector<char>(begin(s), end(s))) {}
ll get(int l, int r) const {
mint res = h[r] + MOD - h[l] * p[r - l];
return res.a;
}
ll connect(ull h1, ull h2, int h2len) const {
mint res = h1 * p[h2len] + h2;
return res.a;
}
int LCP(const RollingHash &b, int l1, int r1, int l2, int r2) {
int len = min(r1 - l1, r2 - l2);
int low = -1, high = len + 1;
while(high - low > 1) {
int mid = (low + high) / 2;
if(get(l1, l1 + mid) == b.get(l2, l2 + mid))
low = mid;
else
high = mid;
}
return (low);
}
RollingHash() = default;
};
template <class T> ll get_hash(const vector<T> &v) {
mint res;
rep(i, si(v)) res += p[i] * v[i];
return res.a;
}
ll get_hash(const string &v) {
mint res;
rep(i, si(v)) res += p[i] * v[i];
return res.a;
}
} // namespace RollingHashes
using namespace RollingHashes;
namespace modular {
constexpr int MOD = 998244353;
const int MAXN = 11000000;
template <int Modulus> class modint;
using mint = modint<MOD>;
using vmint = vector<mint>;
vector<mint> Inv;
mint inv(int x);
template <int Modulus> class modint {
public:
static constexpr int mod() { return Modulus; }
int a;
constexpr modint(const ll x = 0) noexcept : a(((x % Modulus) + Modulus) % Modulus) {}
constexpr int &val() noexcept { return a; }
constexpr const int &val() const noexcept { return a; }
constexpr modint operator-() const noexcept { return modint() - *this; }
constexpr modint operator+() const noexcept { return *this; }
constexpr modint &operator++() noexcept {
if(++a == MOD) a = 0;
return *this;
}
constexpr modint &operator--() noexcept {
if(!a) a = MOD;
a--;
return *this;
}
constexpr modint operator++(int) {
modint res = *this;
++*this;
return res;
}
constexpr modint operator--(int) {
mint res = *this;
--*this;
return res;
}
constexpr modint &operator+=(const modint rhs) noexcept {
a += rhs.a;
if(a >= Modulus) { a -= Modulus; }
return *this;
}
constexpr modint &operator-=(const modint rhs) noexcept {
if(a < rhs.a) { a += Modulus; }
a -= rhs.a;
return *this;
}
constexpr modint &operator*=(const modint rhs) noexcept {
a = (long long)a * rhs.a % Modulus;
return *this;
}
constexpr modint &operator/=(const modint rhs) noexcept {
a = (long long)a * (modular::inv(rhs.a)).a % Modulus;
return *this;
}
constexpr modint pow(long long n) const noexcept {
if(n < 0) {
n %= Modulus - 1;
n = (Modulus - 1) + n;
}
modint x = *this, r = 1;
while(n) {
if(n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
constexpr modint inv() const noexcept { return pow(Modulus - 2); }
constexpr friend modint operator+(const modint &lhs, const modint &rhs) { return modint(lhs) += modint(rhs); }
constexpr friend modint operator-(const modint &lhs, const modint &rhs) { return modint(lhs) -= modint(rhs); }
constexpr friend modint operator*(const modint &lhs, const modint &rhs) { return modint(lhs) *= modint(rhs); }
constexpr friend modint operator/(const modint &lhs, const modint &rhs) { return modint(lhs) /= modint(rhs); }
constexpr friend bool operator==(const modint &lhs, const modint &rhs) { return lhs.a == rhs.a; }
constexpr friend bool operator!=(const modint &lhs, const modint &rhs) { return lhs.a != rhs.a; }
// constexpr friend modint operator^=(const modint &lhs, const modint &rhs) { return modint(lhs) ^= modint(rhs); }
};
vmint Fact{1, 1}, Ifact{1, 1};
mint inv(int n) {
if(n > MAXN) return (mint(n)).pow(MOD - 2);
if(Inv.empty()) Inv.emplace_back(0), Inv.emplace_back(1);
if(Inv.size() > n)
return Inv[n];
else {
for(int i = Inv.size(); i <= n; ++i) {
auto [y, x] = div(int(MOD), i);
Inv.emplace_back(Inv[x] * (-y));
}
return Inv[n];
}
}
mint fact(int n) {
if(Fact.size() > n)
return Fact[n];
else
for(int i = Fact.size(); i <= n; ++i) Fact.emplace_back(Fact[i - 1] * i);
return Fact[n];
}
mint ifact(int n) {
if(Ifact.size() > n)
return Ifact[n];
else
for(int i = Ifact.size(); i <= n; ++i) Ifact.emplace_back(Ifact[i - 1] * inv(i));
return Ifact[n];
}
mint modpow(ll a, ll n) { return mint(a).pow(n); }
mint inv(mint a) { return inv(a.a); }
mint ifact(mint a) { return ifact(a.a); }
mint fact(mint a) { return fact(a.a); }
mint modpow(mint a, ll n) { return modpow(a.a, n); }
mint C(int a, int b) {
if(a < 0 || b < 0) return 0;
if(a < b) return 0;
if(a > MAXN) {
mint res = 1;
rep(i, b) res *= a - i, res /= i + 1;
return res;
}
return fact(a) * ifact(b) * ifact(a - b);
}
mint P(int a, int b) {
if(a < 0 || b < 0) return 0;
if(a < b) return 0;
if(a > MAXN) {
mint res = 1;
rep(i, b) res *= a - i;
return res;
}
return fact(a) * ifact(a - b);
}
ostream &operator<<(ostream &os, mint a) {
os << a.a;
return os;
}
istream &operator>>(istream &is, mint &a) {
ll x;
is >> x;
a = x;
return is;
}
ostream &operator<<(ostream &os, const vmint &a) {
if(!a.empty()) {
os << a[0];
for(int i = 1; i < si(a); i++) os << " " << a[i];
}
return os;
}
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace convolution {
namespace internal {
int ceil_pow2(int n) {
int x = 0;
while((1U << x) < (unsigned int)(n)) x++;
return x;
}
int bsf(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if(x < 0) x += m;
return x;
}
struct barrett {
unsigned int _m;
unsigned long long im;
barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
unsigned int umod() const { return _m; }
unsigned int mul(unsigned int a, unsigned int b) const {
unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned int v = (unsigned int)(z - x * _m);
if(_m <= v) v += _m;
return v;
}
};
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if(m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while(n) {
if(n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
constexpr bool is_prime_constexpr(int n) {
if(n <= 1) return false;
if(n == 2 || n == 7 || n == 61) return true;
if(n % 2 == 0) return false;
long long d = n - 1;
while(d % 2 == 0) d /= 2;
for(long long a : {2, 7, 61}) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while(t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if(y != n - 1 && t % 2 == 0) { return false; }
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if(a == 0) return {b, 0};
long long s = b, t = a;
long long m0 = 0, m1 = 1;
while(t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if(m0 < 0) m0 += b / s;
return {s, m0};
}
// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
if(m == 2) return 1;
if(m == 167772161) return 3;
if(m == 469762049) return 3;
if(m == 754974721) return 11;
if(m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while(x % 2 == 0) x /= 2;
for(int i = 3; (long long)(i)*i <= x; i += 2) {
if(x % i == 0) {
divs[cnt++] = i;
while(x % i == 0) { x /= i; }
}
}
if(x > 1) { divs[cnt++] = x; }
for(int g = 2;; g++) {
bool ok = true;
for(int i = 0; i < cnt; i++) {
if(pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if(ok) return g;
}
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);
void butterfly(std::vector<mint> &a) {
static constexpr int g = internal::primitive_root<mint::mod()>;
int n = int(a.size());
int h = internal::ceil_pow2(n);
static bool first = true;
static mint sum_e[30]; // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i]
if(first) {
first = false;
mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1
int cnt2 = bsf(mint::mod() - 1);
mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
for(int i = cnt2; i >= 2; i--) {
// e^(2^i) == 1
es[i - 2] = e;
ies[i - 2] = ie;
e *= e;
ie *= ie;
}
mint now = 1;
for(int i = 0; i < cnt2 - 2; i++) {
sum_e[i] = es[i] * now;
now *= ies[i];
}
}
for(int ph = 1; ph <= h; ph++) {
int w = 1 << (ph - 1), p = 1 << (h - ph);
mint now = 1;
for(int s = 0; s < w; s++) {
int offset = s << (h - ph + 1);
for(int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p] * now;
a[i + offset] = l + r;
a[i + offset + p] = l - r;
}
now *= sum_e[bsf(~(unsigned int)(s))];
}
}
}
void butterfly_inv(std::vector<mint> &a) {
static constexpr int g = internal::primitive_root<mint::mod()>;
int n = int(a.size());
int h = internal::ceil_pow2(n);
static bool first = true;
static mint sum_ie[30]; // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i]
if(first) {
first = false;
mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1
int cnt2 = bsf(mint::mod() - 1);
mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
for(int i = cnt2; i >= 2; i--) {
// e^(2^i) == 1
es[i - 2] = e;
ies[i - 2] = ie;
e *= e;
ie *= ie;
}
mint now = 1;
for(int i = 0; i < cnt2 - 2; i++) {
sum_ie[i] = ies[i] * now;
now *= es[i];
}
}
for(int ph = h; ph >= 1; ph--) {
int w = 1 << (ph - 1), p = 1 << (h - ph);
mint inow = 1;
for(int s = 0; s < w; s++) {
int offset = s << (h - ph + 1);
for(int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p];
a[i + offset] = l + r;
a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * inow.val();
}
inow *= sum_ie[bsf(~(unsigned int)(s))];
}
}
mint z = mint(n).inv();
for(int i = 0; i < n; i++) a[i] *= z;
}
} // namespace internal
std::vector<mint> convolution(std::vector<mint> a, std::vector<mint> b) {
int n = int(a.size()), m = int(b.size());
if(!n || !m) return {};
if(std::min(n, m) <= 60) {
if(n < m) {
std::swap(n, m);
std::swap(a, b);
}
std::vector<mint> ans(n + m - 1);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) { ans[i + j] += a[i] * b[j]; }
}
return ans;
}
int z = 1 << internal::ceil_pow2(n + m - 1);
a.resize(z);
internal::butterfly(a);
b.resize(z);
internal::butterfly(b);
for(int i = 0; i < z; i++) { a[i] *= b[i]; }
internal::butterfly_inv(a);
a.resize(n + m - 1);
// mint iz = mint(z).inv();
// for(int i = 0; i < n + m - 1; i++) a[i] *= iz;
return a;
}
} // namespace convolution
using Poly = vmint;
Poly low(const Poly &f, int s) { return Poly(f.begin(), f.begin() + min<int>(max(s, 1), f.size())); }
Poly operator-(Poly f) {
for(auto &&e : f) e = -e;
return f;
}
Poly &operator+=(Poly &l, const Poly &r) {
l.resize(max(l.size(), r.size()));
rep(i, r.size()) l[i] += r[i];
return l;
}
Poly operator+(Poly l, const Poly &r) { return l += r; }
Poly &operator-=(Poly &l, const Poly &r) {
l.resize(max(l.size(), r.size()));
rep(i, r.size()) l[i] -= r[i];
return l;
}
Poly operator-(Poly l, const Poly &r) { return l -= r; }
Poly &operator<<=(Poly &f, size_t n) { return f.insert(f.begin(), n, 0), f; }
Poly operator<<(Poly f, size_t n) { return f <<= n; }
Poly &operator>>=(Poly &f, size_t n) { return f.erase(f.begin(), f.begin() + min(f.size(), n)), f; }
Poly operator>>(Poly f, size_t n) { return f >>= n; }
Poly operator*(const Poly &l, const Poly &r) { return convolution::convolution(l, r); }
Poly &operator*=(Poly &l, const Poly &r) { return l = l * r; }
Poly &operator*=(Poly &l, const mint &x) {
for(auto &e : l) e *= x;
return l;
}
Poly operator*(const Poly &l, const mint &x) {
auto res = l;
return res *= x;
}
Poly inv(const Poly &f, int s = -1) {
if(s == -1) s = f.size();
Poly r(s);
r[0] = mint(1) / f[0];
for(int n = 1; n < s; n *= 2) {
auto F = low(f, 2 * n);
F.resize(2 * n);
convolution::internal::butterfly(F);
auto g = low(r, 2 * n);
g.resize(2 * n);
convolution::internal::butterfly(g);
rep(i, 2 * n) F[i] *= g[i];
convolution::internal::butterfly_inv(F);
rep(i, n) F[i] = 0;
convolution::internal::butterfly(F);
rep(i, 2 * n) F[i] *= g[i];
convolution::internal::butterfly_inv(F);
rep(i, n, min(2 * n, s)) r[i] -= F[i];
}
return r;
}
Poly integ(const Poly &f) {
Poly res(f.size() + 1);
for(int i = 1; i < (int)res.size(); ++i) res[i] = f[i - 1] / i;
return res;
}
Poly deriv(const Poly &f) {
if(f.size() == 0) return Poly();
Poly res(f.size() - 1);
rep(i, res.size()) res[i] = f[i + 1] * (i + 1);
return res;
}
Poly log(const Poly &f) {
Poly g = integ(inv(f) * deriv(f));
return Poly{g.begin(), g.begin() + f.size()};
}
Poly exp(const Poly &f) {
Poly g{1};
while(g.size() < f.size()) {
Poly x(f.begin(), f.begin() + min(f.size(), g.size() * 2));
x[0] += 1;
g.resize(2 * g.size());
x -= log(g);
x *= {g.begin(), g.begin() + g.size() / 2};
rep(i, g.size() / 2, min<int>(x.size(), g.size())) g[i] = x[i];
}
return {g.begin(), g.begin() + f.size()};
}
Poly pow(const Poly &f, ll k, int need = -1) {
const int n = (int)f.size();
if(need == -1) need = n;
int z = 0;
rep(i, n) {
if(f[i].a) break;
z++;
}
if(z * k >= need) return Poly(n);
mint rev = f[z].inv();
Poly res = exp(log((f >> z) * rev) * k) * f[z].pow(k);
res.resize(need - z * k);
return res << z * k;
}
struct Prd {
deque<Poly> deq;
Prd() = default;
void emplace(const Poly &f) { deq.emplace_back(f); }
Poly calc() {
if(deq.empty()) return {1};
sort(all(deq), [&](const Poly &f, const Poly &g) { return si(f) < si(g); });
while(deq.size() > 1) {
deq.emplace_back(deq[0] * deq[1]);
for(int i = 0; i < 2; ++i) deq.pop_front();
}
return deq.front();
}
};
Poly prd(vector<Poly> &v) {
Prd p;
for(auto &e : v) p.emplace(e);
return p.calc();
}
vmint power_table(mint x, int len) {
vmint res(len + 1);
res[0] = 1;
rep(i, len) res[i + 1] = res[i] * x;
return res;
}
// calc f(x + a)
Poly TaylorShift(Poly f, mint a) {
int n = f.size();
rep(i, n) f[i] *= fact(i);
reverse(all(f));
Poly g(n, 1);
rep(i, 1, n) g[i] = g[i - 1] * a * inv(i);
f = (f * g);
f.resize(n);
reverse(begin(f), end(f));
rep(i, n) f[i] *= ifact(i);
return f;
}
} // namespace modular
using namespace modular;
int main() {
INT(n, k);
vector<bigint> a;
string s(k, '9');
bigint M(s);
rep(n) {
STR(s);
REV(s);
bigint b(0);
rep(i, 0, si(s), k) {
int j = min(i + k, si(s));
auto t = s.substr(i, j - i);
REV(t);
b += bigint(t);
}
b._shrink();
while(true) {
auto v = b.to_string();
if(si(v) <= k) break;
int t = si(v) - k;
auto w = string(rng(v, 0, t));
v = string(rng(v, t, si(v)));
b = bigint(v) + bigint(w);
}
a.eb(b);
}
vector<ll> mods{999999929, 999999893, 999999797};
vv(ll, v, n, si(mods));
rep(i, n) {
rep(j, si(mods)) {
ll now = 0;
fore(e, a[i].to_string()) { now = (now * 10 + e - '0') % mods[j]; }
v[i][j] = now;
}
}
vl w(si(mods)), w2(si(mods));
rep(i, si(mods)) {
ll now = 0;
rep(k) now = (now * 10 + 9) % mods[i];
w[i] = now;
w2[i] = now * 2 % mods[i];
}
dump(v, w, w2);
map<array<int, 3>, int> mp;
int c = 0;
fore(e, a) if(e.is_zero()) c += 1;
int ans = 1LL * c * (c + 1) * (c + 2) / 6;
per(i, n) {
array<int, 3> a;
rep(j, 3) {
a[j] = w[j] - v[i][j];
if(a[j] < 0) a[j] += mods[j];
}
mp[a] += 1;
rep(j, 3) {
a[j] = w2[j] - v[i][j];
if(a[j] < 0) a[j] += mods[j];
}
mp[a] += 1;
rep(j, i + 1) {
array<int, 3> a;
rep(k, 3) {
a[k] = v[i][k] + v[j][k];
if(a[k] >= mods[k]) a[k] -= mods[k];
}
ans += mp[a];
}
}
OUT(ans);
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3668kb
input:
4 1 0 1 10 17
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 52ms
memory: 12132kb
input:
500 859 7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 325ms
memory: 15224kb
input:
500 17336 11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: -100
Time Limit Exceeded
input:
500 1 751324443898124078584847834484321089092662321556147445230263526014359393841194947303407593948729802551881289193716611867931891257925091769456350249725997883453296895094445731130479434019358742162771547784250401546380268386074363779242500860317042151185119666027858022664683818314351285215150806...
output:
2277515