QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#325430 | #7923. Ferris Wheel | georgeyucjr | AC ✓ | 4104ms | 290376kb | C++20 | 57.7kb | 2024-02-11 12:22:24 | 2024-02-11 12:22:24 |
Judging History
answer
# include <bits/stdc++.h>
# ifndef ONLINE_JUDGE
# define LOCAL
# endif
# if __cplusplus >= 201100LL
# else
# error Please use C++11 Or Higher CPP version
# endif
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using arr3 = std :: array < int, 3 >;
using arr4 = std :: array < int, 4 >;
using pii = std :: pair < int, int >;
# if __cplusplus >= 201400LL
# define rep(i, f, t, ...) for ( std :: decay < decltype ( f + t ) > :: type i = f, _END_##i = t, ##__VA_ARGS__; i <= _END_##i; ++i )
# define per(i, f, t, ...) for ( std :: decay < decltype ( f + t ) > :: type i = f, _END_##i = t, ##__VA_ARGS__; i >= _END_##i; --i )
# else
# define rep(i, f, t, ...) for ( decltype ( f + t ) > i = f, _END_##i = t, ##__VA_ARGS__; i <= _END_##i; ++i )
# define per(i, f, t, ...) for ( decltype ( f + t ) > i = f, _END_##i = t, ##__VA_ARGS__; i >= _END_##i; --i )
# endif
# define emb emplace_back
# define pb push_back
# define mkp make_pair
# define FILEIO(filename) freopen(filename ".in", "r", stdin), freopen(filename ".out", "w", stdout)
# ifndef INLINE_V
# if __cplusplus >= 201700LL
# define INLINE_V inline
# else
# define INLINE_V
# endif
# endif
# if __cplusplus >= 201400LL
# define EXPR constexpr
# else
# define EXPR
# endif
# if __cplusplus >= 201400LL
template < class T, class U >
inline constexpr auto ceil ( T && x, U && y ) {
return ( x > 0 ? ( x + y - 1 ) / y : x / y );
}
template < class T, class U >
inline constexpr auto floor ( T && x, U && y ) {
return ( x > 0 ? x / y : ( x - y + 1 ) / y );
}
template < class T, class U >
inline constexpr auto divmod ( T && x, U && y ) {
auto && q = floor ( x, y );
return std :: pair { q, x - q * y };
}
# else
template < class T, class U >
inline T ceil ( T && x, U && y ) {
return ( x > 0 ? ( x + y - 1 ) / y : x / y );
}
template < class T, class U >
inline T floor ( T && x, U && y ) {
return ( x > 0 ? x / y : ( x - y + 1 ) / y );
}
template < class T, class U >
inline pair < T, T > divmod ( T && x, U && y ) {
T && q = floor ( x, y );
return std :: pair { q, x - q * y };
}
# endif
# if __cplusplus >= 202000LL
# define ALL( vc ) std :: begin ( vc ), std :: end ( vc )
template < class T >
requires ( std :: is_signed_v < T > && std :: is_integral_v < T > )
INLINE_V constexpr static T inf = std :: numeric_limits < T > :: max ( ) >> 1;
template <std :: ranges :: forward_range R>
inline constexpr auto Sum ( R && r ) {
return std :: accumulate ( std :: ranges :: begin ( r ), std :: ranges :: end ( r ), 0 );
}
template < std :: ranges :: forward_range R, typename T >
inline constexpr auto Sum ( R && r, T init) {
return std :: accumulate ( std :: ranges :: begin ( r ), std :: ranges :: end ( r ), init );
}
template < std :: ranges :: forward_range R >
inline constexpr int SZ ( R && x ) { return std :: size ( x ); }
# else
# define ALL( vc ) vc.begin ( ), vc.end ( )
template < class R >
inline EXPR auto Sum ( R && r ) {
return std :: accumulate ( ALL ( r ), 0);
}
template < class R, typename T >
inline EXPR auto Sum( R && r, T init ) {
return std :: accumulate ( ALL ( r ), init );
}
template < class R >
inline EXPR auto Sum ( R f, R t ) {
return std ::accumulate ( f, t );
}
template < class R, typename T >
inline EXPR auto Sum ( R f, R t, T init ) {
return std :: accumulate ( f, t, init );
}
template < class T >
inline EXPR int SZ ( T && x ) { return static_cast < int > ( x.size ( ) ) ; }
template < class T >
INLINE_V constexpr static T inf = std :: numeric_limits < T > :: max ( ) >> 1;
# endif
template < class Os, class T >
inline Os & operator << ( Os & os, T & t ) {
for ( auto ele : t )
os << ele << " ";
os << "\n";
return os;
}
# ifndef __GEORGEYUCJR_READ__
# define __GEORGEYUCJR_READ__
# if __cplusplus >= 202000LL && !defined(MACOS) && defined(__unix__)
# define CHECK_EOF 0
# if CHECK_EOF
# define ECHK0 *ptr ? *ptr++ : -1
# define ECHK1 \
if (ch == -1) \
return set(false), *this;
# define ECHK2 \
if (!*ptr) \
return set(false), *this;
# define ECHK3 and ch != -1
# define ECHK4 and*ptr
# else
# define ECHK0 *ptr++
# define ECHK1
# define ECHK2
# define ECHK3
# define ECHK4
# endif
# if !defined(MACOS)
# define USE_MMAP
# include <sys/mman.h>
# include <sys/stat.h>
# endif
# if __cpluspls >= 202000LL && !defined(MACOS)
# define DISABLE_RANGE
# endif
# if CHECK_EOF || !defined(USE_MMAP)
# define EV -1
# else
# define EV 0
# endif
namespace FastIO {
template <typename T> struct make_unsigned : public std :: make_unsigned<T> {};
template <> struct make_unsigned<i128> {
using type = u128;
};
template <typename T>
concept tupleLike = requires(T t) { std :: tuple_size_v<decltype(std :: tuple_cat(t))>; };
constexpr ull pw10(int t) { return t == 0 ? 1 : pw10(t - 1) * 10; }
std :: mt19937 mrand(std :: random_device{}());
std :: mt19937_64 mrand64(std :: random_device{}());
constexpr std :: size_t bufSize = 1 << 20;
class IO;
class In {
friend class IO;
private:
FILE *inFile;
bool status = true;
# ifdef USE_MMAP
struct stat st;
char *ptr;
int fd;
# elif !defined(LOCAL)
char buf[bufSize], *p1, *p2;
# endif
public:
# ifdef LOCAL
inline int getch() { return fgetc_unlocked(inFile); }
inline void input(FILE *f) { inFile = f, set(); }
# elif defined(USE_MMAP)
inline int getch() { return ECHK0; }
inline void input(FILE *f) {
inFile = f;
if (inFile)
fd = fileno(inFile), fstat(fd, &st), ptr = (char *)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0), set();
}
static constexpr auto n = []() {
std :: array<uint32_t, 0x10000> n{};
for (int i = 0; i != 0x10000; ++i)
n[i] = -1;
constexpr uint32_t e0 = 0x01, e1 = 0x100;
int x = 0;
for (uint32_t i = 0, c0 = 0x3030; i != 10; i++, c0 += e0)
for (uint32_t j = 0, c1 = c0; j != 10; j++, c1 += e1)
n[c1] = x++;
return n;
}();
# else
inline int getch() { return (p1 == p2 ? (p2 = (p1 = buf) + fread(buf, 1, bufSize, inFile)) : 0), p1 == p2 ? -1 : *p1++; }
inline void input(FILE *f) { inFile = f, p1 = p2 = buf, set(); }
# endif
inline void input(const char *s) { input(fopen(s, "rb")); }
inline void set(bool s = true) { status = s; }
inline bool get() const { return status; }
};
class Out {
friend class IO;
public:
FILE *outFile;
# ifndef LOCAL
char pbuf[bufSize], *pp;
# endif
public:
# ifdef LOCAL
inline void flush() { outFile ? fflush(outFile) : 0; }
inline void putch(char c) { fputc_unlocked(c, outFile); }
# else
inline void flush() { (outFile ? fwrite(pbuf, 1, pp - pbuf, outFile), fflush(outFile) : 0), pp = pbuf; }
inline void putch(char c) { ((pp - pbuf == bufSize) ? fwrite(pbuf, 1, bufSize, outFile), pp = pbuf : 0), *pp++ = c; }
# endif
inline void output(const char *s) { flush(), outFile = fopen(s, "wb"); }
inline void output(FILE *f) { flush(), outFile = f; }
};
class IO : public In, public Out {
private:
std :: size_t precision = 12;
public:
IO(FILE * = nullptr, FILE * = nullptr);
~IO() { flush(); }
static constexpr bool blank(char);
template <typename... Args>
requires(sizeof...(Args) > 1)
IO &read(Args &...);
template <typename T>
requires(std :: is_signed_v<T> && std :: is_integral_v<T>) || std :: is_same_v<std :: decay_t<T>, i128>
IO &read(T &);
template <typename T>
requires(std :: is_unsigned_v<T> && std :: is_integral_v<T>) || std :: is_same_v<std :: decay_t<T>, u128>
IO &read(T &);
template <typename T>
requires std :: is_floating_point_v<T>
IO &read(T &);
template <typename T> T read();
IO &read(char &);
IO &read(char *);
IO &read(std :: string &);
IO &readline(char *);
IO &readline(std :: string &);
template <tupleLike T> IO &read(T &&);
template <std :: ranges::input_range R> IO &read(R &&r) { return readArray(std :: forward<R>(r)); }
void setprecision(std :: size_t n = 6u) { precision = n; }
template <typename... Args>
requires(sizeof...(Args) > 1)
void write(Args &&...);
inline void write() const {}
template <typename T>
requires(std :: is_signed_v<T> && std :: is_integral_v<T>) || std :: is_same_v<std :: decay_t<T>, i128>
void write(T);
template <typename T>
requires(std :: is_unsigned_v<T> && std :: is_integral_v<T>)
void write(T);
void write(u128);
inline void write(char);
template <typename T>
requires std :: is_floating_point_v<T>
void write(T);
inline void write(bool);
void write(char *);
void write(const char *);
void write(const std :: string &);
template <typename T> void write(std :: initializer_list<T>);
template <tupleLike T> void write(T &&);
template <std :: ranges::input_range R> void write(R &&);
template <typename... Args> void writeln(Args &&...);
template <typename T> inline void writespc(T && x);
operator bool() const { return get(); }
} io(stdin, stdout);
template <typename... Args> inline void writeln(Args &&...x) { io.writeln(std :: forward<Args>(x)...); }
template <typename T> inline void writeln(std :: initializer_list<T> x) { io.writeln(x); }
#pragma endregion
# define isdigit(x) ((x) >= '0' && (x) <= '9')
IO::IO(FILE *in, FILE *out) { input(in), output(out); }
constexpr bool IO::blank(char c) { return c == ' ' || c == '\n' || c == '\r'; }
template <typename... Args>
requires(sizeof...(Args) > 1)
IO &IO::read(Args &...args) {
if constexpr (CHECK_EOF)
(read(args) && ...);
else
(read(args), ...);
return *this;
}
template <typename T>
requires(std :: is_signed_v<T> && std :: is_integral_v<T>) || std :: is_same_v<std :: decay_t<T>, i128>
IO &IO::read(T &x) {
x = 0;
static typename make_unsigned<T>::type t;
bool sign = false;
# ifndef USE_MMAP
int ch = getch();
while (!isdigit(ch) ECHK3)
sign = (ch == '-'), ch = getch();
ECHK1
t = 0;
while (isdigit(ch))
t = t * 10 + (ch ^ 48), ch = getch();
# else
while (!isdigit(*ptr) ECHK4)
sign = *ptr++ == '-';
ECHK2
t = *ptr++ ^ 48;
while (~n[*reinterpret_cast<std :: uint16_t *&>(ptr)])
t = t * 100 + n[*reinterpret_cast<std :: uint16_t *&>(ptr)++];
if (isdigit(*ptr))
t = t * 10 + (*ptr++ ^ 48);
# endif
x = sign ? (~t + 1) : t;
return *this;
}
template <typename T>
requires(std :: is_unsigned_v<T> && std :: is_integral_v<T>) || std :: is_same_v<std :: decay_t<T>, u128>
IO &IO::read(T &x) {
x = 0;
# ifndef USE_MMAP
int ch = getch();
while (!isdigit(ch) ECHK3)
ch = getch();
ECHK1
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getch();
# else
while (!isdigit(*ptr) ECHK4)
ptr++;
ECHK2
x = *ptr++ ^ 48;
while (~n[*reinterpret_cast<std :: uint16_t *&>(ptr)])
x = x * 100 + n[*reinterpret_cast<std :: uint16_t *&>(ptr)++];
if (isdigit(*ptr))
x = x * 10 + (*ptr++ ^ 48);
# endif
return *this;
}
template <typename T>
requires std :: is_floating_point_v<T>
IO &IO::read(T &x) {
x = 0;
T t = 1;
bool sign = false;
int ch = getch();
while (!isdigit(ch) ECHK3)
sign = (ch == '-'), ch = getch();
ECHK1
while (isdigit(ch))
x = x * 10 + (ch ^ 48), ch = getch();
if (ch == '.')
for (ch = getch(); isdigit(ch); ch = getch())
x += (t /= 10) * (ch ^ 48);
x = sign ? -x : x;
return *this;
}
template <typename T> T IO::read() {
static std :: decay_t<T> x;
return read(x), x;
}
IO &IO::read(char &ch) {
do
ch = getch();
while (blank(ch));
ECHK1
return *this;
}
IO &IO::read(char *s) {
int ch = getch();
while (blank(ch))
ch = getch();
ECHK1
while (!blank(ch) && ch != EV)
*s++ = ch, ch = getch();
*s = 0;
return *this;
}
IO &IO::read(std :: string &s) {
int ch = getch();
while (blank(ch))
ch = getch();
ECHK1
s.erase();
while (!blank(ch) && ch != EV)
s.append(1, ch), ch = getch();
return *this;
}
IO &IO::readline(char *s) {
char *t = s;
int ch = getch();
while (ch != '\n' && ch != EV)
*s++ = ch, ch = getch();
*s = 0;
if (s == t && ch != '\n')
set(false);
return *this;
}
IO &IO::readline(std :: string &s) {
s.erase();
int ch = getch();
while (ch != '\n' && ch != EV)
s.append(1, ch), ch = getch();
if (s.empty() && ch != '\n')
set(false);
return *this;
}
template <tupleLike T> IO &IO::read(T &&t) {
std :: apply([&](auto & ...t) { (read(t), ...); }, t);
return *this;
}
template <typename... Args>
requires(sizeof...(Args) > 1)
void IO::write(Args &&...args) {
(write(std :: forward<Args>(args)), ...);
}
template <typename T>
requires(std :: is_signed_v<T> && std :: is_integral_v<T>) || std :: is_same_v<std :: decay_t<T>, i128>
void IO::write(T x) {
static typename make_unsigned<T>::type y;
y = x;
if (x < 0)
putch('-'), write(~y + 1);
else
write(y);
}
constexpr auto D = []() {
constexpr uint32_t e0 = 0x1, e1 = 0x100, e2 = 0x10000, e3 = 0x1000000;
std :: array<uint32_t, 10000> m{};
int x = 0;
for (uint32_t i = 0, c0 = 0x30303030; i != 10; i++, c0 += e0)
for (uint32_t j = 0, c1 = c0; j != 10; j++, c1 += e1)
for (uint32_t k = 0, c2 = c1; k != 10; k++, c2 += e2)
for (uint32_t l = 0, c3 = c2; l != 10; l++, c3 += e3)
m[x++] = c3;
return m;
}();
template <typename T>
requires(std :: is_unsigned_v<T> && std :: is_integral_v<T>)
void IO::write(T x) {
# ifndef LOCAL
if (std :: end(pbuf) - pp < 64)
flush();
auto L = [&](int x) { return x == 1 ? 0 : pw10(x - 1); };
auto R = [&](int x) { return pw10(x) - 1; };
# define de(t) \
case L(t)... R(t): \
*reinterpret_cast<uint32_t *>(pp) = D[x / pw10((t)-4)]; \
pp += 4; \
x %= pw10((t)-4);
ull y = x;
switch (y) {
de(18);
de(14);
de(10);
de(6);
case L(2)... R(2):
*reinterpret_cast<uint32_t *>(pp) = D[x * 100];
pp += 2;
break;
de(17);
de(13);
de(9);
de(5);
case L(1)... R(1):
*pp = static_cast<char>('0' + x);
pp += 1;
break;
default:
*reinterpret_cast<uint32_t *>(pp) = D[x / pw10(16)];
pp += 4;
x %= pw10(16);
de(16);
de(12);
de(8);
case L(4)... R(4):
*reinterpret_cast<uint32_t *>(pp) = D[x];
pp += 4;
break;
de(19);
de(15);
de(11);
de(7);
case L(3)... R(3):
*reinterpret_cast<uint32_t *>(pp) = D[x * 10];
pp += 3;
break;
}
# else
write(u128(x));
# endif
}
constexpr u128 _pw10(int x) { return x == 0 ? 1 : _pw10(x - 1) * 10; }
void IO::write(u128 x) {
if (std :: end(pbuf) - pp < 64)
flush();
constexpr auto pw10 = _pw10;
auto L = [&](int x) { return x == 1 ? 0 : pw10(x - 1); };
auto R = [&](int x) { return pw10(x) - 1; };
# define de(t) \
case L(t)... R(t): \
*reinterpret_cast<uint32_t *>(pp) = D[x / pw10((t)-4)]; \
pp += 4; \
x %= pw10((t)-4);
switch (x) {
de(38);
de(34);
de(30);
de(26);
de(22);
de(18);
de(14);
de(10);
de(6);
case L(2)... R(2):
*reinterpret_cast<uint32_t *>(pp) = D[x * 100];
pp += 2;
break;
de(37);
de(33);
de(29);
de(25);
de(21);
de(17);
de(13);
de(9);
de(5);
case L(1)... R(1):
*pp = static_cast<char>('0' + x);
pp += 1;
break;
de(36);
de(32);
de(28);
de(24);
de(20);
de(16);
de(12);
de(8);
case L(4)... R(4):
*reinterpret_cast<uint32_t *>(pp) = D[x];
pp += 4;
break;
default:
*reinterpret_cast<uint32_t *>(pp) = D[x / pw10(35)];
pp += 4;
x %= pw10(35);
de(35);
de(31);
de(27);
de(23);
de(19);
de(15);
de(11);
de(7);
case L(3)... R(3):
*reinterpret_cast<uint32_t *>(pp) = D[x * 10];
pp += 3;
break;
}
}
void IO::write(bool x) { putch(x ^ 48); }
void IO::write(char c) { putch(c); }
template <typename T>
requires std :: is_floating_point_v<T>
void IO::write(T x) {
static char buf[512];
*std :: to_chars(buf, buf + 512, x, std :: chars_format::fixed, precision).ptr = 0;
write(buf);
}
void IO::write(char *s) {
while (*s)
putch(*s++);
}
void IO::write(const char *s) {
while (*s)
putch(*s++);
}
void IO::write(const std :: string &s) { write(s.data()); }
template <typename T> void IO::write(std :: initializer_list<T> t) {
auto first = std :: begin(t), last = std :: end(t);
if (first != last)
for (write(*first++); first != last; ++first) {
putch(' ');
write(*first);
}
}
template <tupleLike T> void IO::write(T &&t) {
[&]<std :: size_t... I>(std :: index_sequence<I...>) {
(..., (I == 0 ? void(0) : putch(' '), write(std :: get<I>(t))));
}(std :: make_index_sequence<std :: tuple_size_v<std :: remove_reference_t<T>>>());
}
template <std :: ranges::input_range R> void IO::write(R &&r) {
if constexpr (std :: is_same_v<std :: decay_t<R>, std :: string>)
return write(r.data());
auto first = std :: ranges::begin(r), last = std :: ranges::end(r);
if (first != last)
for (write(*first++); first != last; ++first) {
putch(' ');
write(*first);
}
}
template <typename... Args> void IO::writeln(Args &&...x) { write(std :: forward<Args>(x)...), putch('\n'); }
template <typename T> inline void IO::writespc(T && x) { write(x), putch('\n'); }
} // namespace FastIO
using namespace FastIO;
# else
# ifndef INLINE_V
# if __cplusplus >= 201700LL
# define INLINE_V inline
# else
# define INLINE_V
# endif
# endif
# ifndef LUOGU
# define auto_att __attribute__((target("avx2"), optimize(3, "Ofast", "unroll-loops", "inline")))
# else
# define auto_att
# endif
namespace FastIO {
# define endl '\n'
INLINE_V static constexpr size_t buf_size = 1 << 18;
INLINE_V static constexpr size_t integer_size = 20;
INLINE_V static constexpr size_t block_size = 10000;
static char inbuf[buf_size + 1] = {};
static char outbuf[buf_size + 1] = {};
static char block_str[block_size * 4 + 1] = {};
static constexpr uint64_t power10[] = {1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000,
10000000000000000000u
};
struct Scanner {
# ifndef LOCAL
private:
size_t pos, end;
inline void load() {
end = fread(inbuf, 1, buf_size, stdin);
inbuf[end] = '\0';
}
inline void reload() {
size_t len = end - pos;
memmove(inbuf, inbuf + pos, len);
end = len + fread(inbuf + len, 1, buf_size - len, stdin);
inbuf[end] = '\0';
pos = 0;
}
inline void skip_space() {
while (inbuf[pos] <= ' ') {
if (__builtin_expect(++pos == end, 0))
reload();
}
}
inline char get_next() { return inbuf[pos++]; }
inline char get_next_nonspace() {
skip_space();
return inbuf[pos++];
}
public:
Scanner() { load(); }
auto_att inline void read(char &c) { c = get_next_nonspace(); }
auto_att inline void read(std :: string &s) {
skip_space();
s = "";
do {
size_t start = pos;
while (inbuf[pos] > ' ')
++pos;
s += std :: string(inbuf + start, inbuf + pos);
if (inbuf[pos] != '\0')
break;
reload();
} while (true);
}
template <class T> typename std :: enable_if<std :: is_integral<T>::value, void>::type read(T &x) {
char c = get_next_nonspace();
if (__builtin_expect(pos + integer_size >= end, 0))
reload();
bool neg = false;
if (c == '-')
neg = true, x = 0;
else
x = c & 15;
while ((c = get_next()) >= '0')
x = (x << 3) + (x << 1) + (c & 15);
if (neg)
x = -x;
}
# else
template <typename _Tp> inline void read(_Tp &x) {
char ch = getchar();
int f = 1;
x = 0;
for (; ch < '0' || ch > '9'; ch = getchar())
f = ch == '-' ? -1 : 1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 3) + (x << 1) + (ch ^ 48);
x *= f;
}
inline void read(char &c) { c = getchar(); }
inline void read(char *x) {
int i = 0;
char c = ' ';
for (; c <= ' ';)
c = getchar();
for (; c > ' ';)
x[i++] = c, c = getchar();
x[i] = 0;
}
inline void read(std :: string &x) {
x.clear();
char c = ' ';
for (; c <= ' ';)
c = getchar();
for (; c > ' ';)
x += c, c = getchar();
}
# endif
template <class Head, class... Others> void read(Head &head, Others &...others) {
read(head);
read(others...);
}
template <class T1, class T2> inline void read(std :: pair<T1, T2> &x) { read(x.first), read(x.second); }
} is;
struct Printer {
# ifndef LOCAL
private:
size_t pos = 0;
auto_att inline void flush() {
fwrite(outbuf, 1, pos, stdout);
pos = 0;
}
inline void pre_calc() {
for (size_t i = 0; i < block_size; ++i) {
size_t j = 4, k = i;
while (j--) {
block_str[i * 4 + j] = k % 10 | 48;
k /= 10;
}
}
}
static EXPR size_t get_integer_size(uint64_t n) {
if (n >= power10[10]) {
if (n >= power10[19])
return 20;
if (n >= power10[18])
return 19;
if (n >= power10[17])
return 18;
if (n >= power10[16])
return 17;
if (n >= power10[15])
return 16;
if (n >= power10[14])
return 15;
if (n >= power10[13])
return 14;
if (n >= power10[12])
return 13;
if (n >= power10[11])
return 12;
return 11;
} else {
if (n >= power10[9])
return 10;
if (n >= power10[8])
return 9;
if (n >= power10[7])
return 8;
if (n >= power10[6])
return 7;
if (n >= power10[5])
return 6;
if (n >= power10[4])
return 5;
if (n >= power10[3])
return 4;
if (n >= power10[2])
return 3;
if (n >= power10[1])
return 2;
return 1;
}
}
public:
Printer() { pre_calc(); }
~Printer() { flush(); }
inline void write(char c) {
outbuf[pos++] = c;
if (__builtin_expect(pos == buf_size, 0))
flush();
}
inline void write(const char *s) {
while (*s != 0) {
outbuf[pos++] = *s++;
// if (pos == buf_size) flush();
if (__builtin_expect(pos == buf_size, 0))
flush();
}
}
inline void write(const std :: string &s) {
for (auto c : s) {
outbuf[pos++] = c;
// if (pos == buf_size) flush();
if (__builtin_expect(pos == buf_size, 0))
flush();
}
}
template <class T> typename std :: enable_if<std :: is_integral<T>::value, void>::type write(T x) {
if (__builtin_expect(pos + integer_size >= buf_size, 0))
flush();
if (x < 0)
write('-'), x = -x;
size_t digit = get_integer_size(x);
size_t len = digit;
while (len >= 4) {
len -= 4;
memcpy(outbuf + pos + len, block_str + ((x % block_size) << 2), 4);
x /= block_size;
}
memcpy(outbuf + pos, block_str + (x << 2) + (4 - len), len);
pos += digit;
}
# else
inline void write(char c) { putchar(c); }
inline void write(const char *x) {
for (int i = 0; x[i]; ++i)
write(x[i]);
}
inline void write(std::string s) {
for (auto c : s)
write(c);
}
template <typename _Tp> inline void write(_Tp x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 ^ 48);
}
# endif
template <class Head, class... Others> void write(const Head &head, const Others &...others) {
write(head);
write(' ');
write(others...);
}
template <class... Args> void writeln(const Args &...args) {
write(args...);
write('\n');
}
template < class T >
inline void writespc ( const T & x ) {
write ( x );
write ( " " );
}
template <class T1, class T2> inline void write(const std :: pair<T1, T2> &x) { write(x.first, x.second); }
} os;
struct IO : public Scanner, public Printer {
} io;
}; // namespace FastIO
using namespace FastIO;
# endif
# endif
using namespace std;
// by Nyyan
template <typename mint>
struct NTT {
static constexpr uint32_t get_pr() {
uint32_t _mod = mint::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::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].inv();
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) {
{
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;
}
}
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()).inv();
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).inv();
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));
}
};
template <typename mint>
struct FormalPowerSeries : vector<mint> {
using vector<mint>::vector;
using FPS = FormalPowerSeries;
FPS &operator+=(const FPS &r) {
if (r.size() > this->size()) this->resize(r.size());
for (int i = 0; i < (int)r.size(); i++) (*this)[i] += r[i];
return *this;
}
FPS &operator+=(const mint &r) {
if (this->empty()) this->resize(1);
(*this)[0] += r;
return *this;
}
FPS &operator-=(const FPS &r) {
if (r.size() > this->size()) this->resize(r.size());
for (int i = 0; i < (int)r.size(); i++) (*this)[i] -= r[i];
return *this;
}
FPS &operator-=(const mint &r) {
if (this->empty()) this->resize(1);
(*this)[0] -= r;
return *this;
}
FPS &operator*=(const mint &v) {
for (int k = 0; k < (int)this->size(); k++) (*this)[k] *= v;
return *this;
}
FPS &operator/=(const FPS &r) {
if (this->size() < r.size()) {
this->clear();
return *this;
}
int n = this->size() - r.size() + 1;
if ((int)r.size() <= 64) {
FPS f(*this), g(r);
g.shrink();
mint coeff = g.back().inverse();
for (auto &x : g) x *= coeff;
int deg = (int)f.size() - (int)g.size() + 1;
int gs = g.size();
FPS quo(deg);
for (int i = deg - 1; i >= 0; i--) {
quo[i] = f[i + gs - 1];
for (int j = 0; j < gs; j++) f[i + j] -= quo[i] * g[j];
}
*this = quo * coeff;
this->resize(n, mint(0));
return *this;
}
return *this = ((*this).rev().pre(n) * r.rev().inv(n)).pre(n).rev();
}
FPS &operator%=(const FPS &r) {
*this -= *this / r * r;
shrink();
return *this;
}
FPS operator+(const FPS &r) const { return FPS(*this) += r; }
FPS operator+(const mint &v) const { return FPS(*this) += v; }
FPS operator-(const FPS &r) const { return FPS(*this) -= r; }
FPS operator-(const mint &v) const { return FPS(*this) -= v; }
FPS operator*(const FPS &r) const { return FPS(*this) *= r; }
FPS operator*(const mint &v) const { return FPS(*this) *= v; }
FPS operator/(const FPS &r) const { return FPS(*this) /= r; }
FPS operator%(const FPS &r) const { return FPS(*this) %= r; }
FPS operator-() const {
FPS ret(this->size());
for (int i = 0; i < (int)this->size(); i++) ret[i] = -(*this)[i];
return ret;
}
void shrink() {
while (this->size() && this->back() == mint(0)) this->pop_back();
}
FPS rev() const {
FPS ret(*this);
reverse(begin(ret), end(ret));
return ret;
}
FPS dot(FPS r) const {
FPS ret(min(this->size(), r.size()));
for (int i = 0; i < (int)ret.size(); i++) ret[i] = (*this)[i] * r[i];
return ret;
}
// 前 sz 項を取ってくる。sz に足りない項は 0 埋めする
FPS pre(int sz) const {
FPS ret(begin(*this), begin(*this) + min((int)this->size(), sz));
if ((int)ret.size() < sz) ret.resize(sz);
return ret;
}
FPS operator>>(int sz) const {
if ((int)this->size() <= sz) return {};
FPS ret(*this);
ret.erase(ret.begin(), ret.begin() + sz);
return ret;
}
FPS operator<<(int sz) const {
FPS ret(*this);
ret.insert(ret.begin(), sz, mint(0));
return ret;
}
FPS diff() const {
const int n = (int)this->size();
FPS ret(max(0, n - 1));
mint one(1), coeff(1);
for (int i = 1; i < n; i++) {
ret[i - 1] = (*this)[i] * coeff;
coeff += one;
}
return ret;
}
FPS integral() const {
const int n = (int)this->size();
FPS ret(n + 1);
ret[0] = mint(0);
if (n > 0) ret[1] = mint(1);
auto mod = mint::get_mod();
for (int i = 2; i <= n; i++) ret[i] = (-ret[mod % i]) * (mod / i);
for (int i = 0; i < n; i++) ret[i + 1] *= (*this)[i];
return ret;
}
mint eval(mint x) const {
mint r = 0, w = 1;
for (auto &v : *this) r += w * v, w *= x;
return r;
}
FPS log(int deg = -1) const {
assert(!(*this).empty() && (*this)[0] == mint(1));
if (deg == -1) deg = (int)this->size();
return (this->diff() * this->inv(deg)).pre(deg - 1).integral();
}
FPS pow(int64_t k, int deg = -1) const {
const int n = (int)this->size();
if (deg == -1) deg = n;
if (k == 0) {
FPS ret(deg);
if (deg) ret[0] = 1;
return ret;
}
for (int i = 0; i < n; i++) {
if ((*this)[i] != mint(0)) {
mint rev = mint(1) / (*this)[i];
FPS ret = (((*this * rev) >> i).log(deg) * k).exp(deg);
ret *= (*this)[i].pow(k);
ret = (ret << (i * k)).pre(deg);
if ((int)ret.size() < deg) ret.resize(deg, mint(0));
return ret;
}
if (__int128_t(i + 1) * k >= deg) return FPS(deg, mint(0));
}
return FPS(deg, mint(0));
}
static void *ntt_ptr;
static void set_fft();
FPS &operator*=(const FPS &r);
void ntt();
void intt();
void ntt_doubling();
static int ntt_pr();
FPS inv(int deg = -1) const;
FPS exp(int deg = -1) const;
};
template <typename mint>
void *FormalPowerSeries<mint>::ntt_ptr = nullptr;
/**
* @brief 多項式/形式的冪級数ライブラリ
* @docs docs/fps/formal-power-series.md
*/
template <typename mint>
void FormalPowerSeries<mint>::set_fft() {
if (!ntt_ptr) ntt_ptr = new NTT<mint>;
}
template <typename mint>
FormalPowerSeries<mint>& FormalPowerSeries<mint>::operator*=(
const FormalPowerSeries<mint>& r) {
if (this->empty() || r.empty()) {
this->clear();
return *this;
}
set_fft();
auto ret = static_cast<NTT<mint>*>(ntt_ptr)->multiply(*this, r);
return *this = FormalPowerSeries<mint>(ret.begin(), ret.end());
}
template <typename mint>
void FormalPowerSeries<mint>::ntt() {
set_fft();
static_cast<NTT<mint>*>(ntt_ptr)->ntt(*this);
}
template <typename mint>
void FormalPowerSeries<mint>::intt() {
set_fft();
static_cast<NTT<mint>*>(ntt_ptr)->intt(*this);
}
template <typename mint>
void FormalPowerSeries<mint>::ntt_doubling() {
set_fft();
static_cast<NTT<mint>*>(ntt_ptr)->ntt_doubling(*this);
}
template <typename mint>
int FormalPowerSeries<mint>::ntt_pr() {
set_fft();
return static_cast<NTT<mint>*>(ntt_ptr)->pr;
}
template <typename mint>
FormalPowerSeries<mint> FormalPowerSeries<mint>::inv(int deg) const {
assert((*this)[0] != mint(0));
if (deg == -1) deg = (int)this->size();
FormalPowerSeries<mint> res(deg);
res[0] = {mint(1) / (*this)[0]};
for (int d = 1; d < deg; d <<= 1) {
FormalPowerSeries<mint> f(2 * d), g(2 * d);
for (int j = 0; j < min((int)this->size(), 2 * d); j++) f[j] = (*this)[j];
for (int j = 0; j < d; j++) g[j] = res[j];
f.ntt();
g.ntt();
for (int j = 0; j < 2 * d; j++) f[j] *= g[j];
f.intt();
for (int j = 0; j < d; j++) f[j] = 0;
f.ntt();
for (int j = 0; j < 2 * d; j++) f[j] *= g[j];
f.intt();
for (int j = d; j < min(2 * d, deg); j++) res[j] = -f[j];
}
return res.pre(deg);
}
template <typename mint>
FormalPowerSeries<mint> FormalPowerSeries<mint>::exp(int deg) const {
using fps = FormalPowerSeries<mint>;
assert((*this).size() == 0 || (*this)[0] == mint(0));
if (deg == -1) deg = this->size();
fps inv;
inv.reserve(deg + 1);
inv.push_back(mint(0));
inv.push_back(mint(1));
auto inplace_integral = [&](fps & F) -> void {
const int n = (int)F.size();
auto mod = mint::get_mod();
while ((int)inv.size() <= n) {
int i = inv.size();
inv.push_back((-inv[mod % i]) * (mod / i));
}
F.insert(begin(F), mint(0));
for (int i = 1; i <= n; i++) F[i] *= inv[i];
};
auto inplace_diff = [](fps & F) -> void {
if (F.empty()) return;
F.erase(begin(F));
mint coeff = 1, one = 1;
for (int i = 0; i < (int)F.size(); i++) {
F[i] *= coeff;
coeff += one;
}
};
fps b{1, 1 < (int)this->size() ? (*this)[1] : 0}, c{1}, z1, z2{1, 1};
for (int m = 2; m < deg; m *= 2) {
auto y = b;
y.resize(2 * m);
y.ntt();
z1 = z2;
fps z(m);
for (int i = 0; i < m; ++i) z[i] = y[i] * z1[i];
z.intt();
fill(begin(z), begin(z) + m / 2, mint(0));
z.ntt();
for (int i = 0; i < m; ++i) z[i] *= -z1[i];
z.intt();
c.insert(end(c), begin(z) + m / 2, end(z));
z2 = c;
z2.resize(2 * m);
z2.ntt();
fps x(begin(*this), begin(*this) + min<int>(this->size(), m));
x.resize(m);
inplace_diff(x);
x.push_back(mint(0));
x.ntt();
for (int i = 0; i < m; ++i) x[i] *= y[i];
x.intt();
x -= b.diff();
x.resize(2 * m);
for (int i = 0; i < m - 1; ++i) x[m + i] = x[i], x[i] = mint(0);
x.ntt();
for (int i = 0; i < 2 * m; ++i) x[i] *= z2[i];
x.intt();
x.pop_back();
inplace_integral(x);
for (int i = m; i < min<int>(this->size(), 2 * m); ++i) x[i] += (*this)[i];
fill(begin(x), begin(x) + m, mint(0));
x.ntt();
for (int i = 0; i < 2 * m; ++i) x[i] *= y[i];
x.intt();
b.insert(end(b), begin(x) + m, end(x));
}
return fps{begin(b), begin(b) + deg};
}
#ifndef ATCODER_MODINT_HPP
#define ATCODER_MODINT_HPP 1
#include <type_traits>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#ifndef ATCODER_INTERNAL_MATH_HPP
#define ATCODER_INTERNAL_MATH_HPP 1
#include <utility>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0)
x += m;
return x;
}
// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
unsigned int _m;
unsigned long long im;
// @param m `1 <= m < 2^31`
barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
// @return m
unsigned int umod() const { return _m; }
// @param a `0 <= a < m`
// @param b `0 <= b < m`
// @return `a * b % m`
unsigned int mul(unsigned int a, unsigned int b) const {
// [1] m = 1
// a = b = im = 0, so okay
// [2] m >= 2
// im = ceil(2^64 / m)
// -> im * m = 2^64 + r (0 <= r < m)
// let z = a*b = c*m + d (0 <= c, d < m)
// a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
// c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) <
// 2^64 * 2
// ((ab * im) >> 64) == c or c + 1
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;
}
};
// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
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;
}
// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
if (n <= 1)
return false;
if (n == 2 || n == 7 || n == 61)
return true;
if (n % 2 == 0)
return false;
long long d = n - 1;
while (d % 2 == 0)
d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n>
constexpr bool is_prime = is_prime_constexpr(n);
// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
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};
// Contracts:
// [1] s - m0 * a = 0 (mod b)
// [2] t - m1 * a = 0 (mod b)
// [3] s * |m1| + t * |m0| <= b
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
// [3]:
// (s - t * u) * |m1| + t * |m0 - m1 * u|
// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
// = s * |m1| + t * |m0| <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
// by [3]: |m0| <= b/g
// by g != b: |m0| < b/g
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);
} // namespace internal
} // namespace atcoder
#endif // ATCODER_INTERNAL_MATH_HPP
#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP
#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional < std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type >::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional < std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type >::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional < std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type >::type;
template <class T>
using is_signed_int = typename std::conditional < (is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type >::type;
template <class T>
using is_unsigned_int =
typename std::conditional < (is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type >::type;
template <class T>
using to_unsigned = typename std::conditional <
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type >::type;
#else
template <class T>
using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional < is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type >::type;
template <class T>
using is_unsigned_int =
typename std::conditional < is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type >::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T>
using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
#endif // ATCODER_INTERNAL_TYPE_TRAITS_HPP
namespace atcoder {
namespace internal {
struct modint_base {};
struct static_modint_base : modint_base {};
template <class T>
using is_modint = std::is_base_of<modint_base, T>;
template <class T>
using is_modint_t = std::enable_if_t<is_modint<T>::value>;
} // namespace internal
template < int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
using mint = static_modint;
public:
static constexpr int mod() { return m; }
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
static_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
static_modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0)
x += umod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
static_modint(T v) {
_v = (unsigned int)(v % umod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod())
_v = 0;
return *this;
}
mint& operator--() {
if (_v == 0)
_v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod())
_v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod())
_v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1)
r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
if (prime) {
assert(_v);
return pow(umod() - 2);
} else {
auto eg = internal::inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static constexpr unsigned int umod() { return m; }
static constexpr bool prime = internal::is_prime<m>;
};
template <int id>
struct dynamic_modint : internal::modint_base {
using mint = dynamic_modint;
public:
static int mod() { return (int)(bt.umod()); }
static void set_mod(int m) {
assert(1 <= m);
bt = internal::barrett(m);
}
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
dynamic_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
dynamic_modint(T v) {
long long x = (long long)(v % (long long)(mod()));
if (x < 0)
x += mod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
dynamic_modint(T v) {
_v = (unsigned int)(v % mod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod())
_v = 0;
return *this;
}
mint& operator--() {
if (_v == 0)
_v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod())
_v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod())
_v -= umod();
return *this;
}
mint& operator*=(const mint& rhs) {
_v = bt.mul(_v, rhs._v);
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1)
r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
auto eg = internal::inv_gcd(_v, mod());
assert(eg.first == 1);
return eg.second;
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id>
internal::barrett dynamic_modint<id>::bt = 998244353;
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint < -1 >;
namespace internal {
template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;
template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;
template <class>
struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};
template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;
} // namespace internal
} // namespace atcoder
#endif // ATCODER_MODINT_HPP
template < class Mint > class Comb {
private: std :: vector < Mint > fac; std :: vector < Mint > ifac;
public: inline void rsz ( int _n ) {
int prv_n = ( int ) fac.size ( ) - 1; if (prv_n >= _n) return ; fac.resize(_n + 1), ifac.resize(_n + 1);
for (int i = prv_n + 1; i <= _n; ++i) fac[i] = fac[i - 1] * Mint::raw(i); ifac[_n] = fac[_n].inv();
for (int i = _n; i > prv_n; --i) ifac[i - 1] = ifac[i] * Mint::raw(i);
} Comb ( int n = 1 ) { fac.assign(2, Mint(1)), ifac.assign(2, Mint(1)), rsz(n); } inline Mint Fac(int n) {
if ( n >= (int)fac.size ( ) ) rsz ( n + 5 ); return fac[n];
} inline Mint InvFac(int n) {
if (n >= (int)fac.size()) rsz(n + 5); return ifac[n];
} inline Mint Inv(int n) {
if (n >= (int)fac.size()) rsz(n + 5); return ifac[n] * fac[n - 1];
} inline Mint C(int n, int m) {
if (n < 0 || n < m || m < 0) return Mint(0); if (n >= (int)fac.size()) rsz(n + 5); return fac[n] * ifac[m] * ifac[n - m];
} inline Mint A(int n, int m) {
if (n < 0 || n < m || m < 0) return Mint(0); if (n >= (int)fac.size()) rsz(n + 5); return fac[n] * ifac[n - m];
} inline Mint operator()(int n, int m) { return C(n, m); }
};
using mint = atcoder :: modint998244353;
Comb < mint > comb;
using fps = FormalPowerSeries < mint >;
# ifndef __GEORGEYUCJR_READ__
# ifdef LOCAL
# include <georgeyucjr/debug.h>
using namespace georgeyucjr;
# else
# define write(...) void ( 36 )
# define bug(...) void ( 36 )
# endif
# else
# warning Do not use debug.hpp.
# endif
int n, m;
int gcd(int x, int y) {return y ? gcd(y, x % y) : x;}
signed main() {
io.read (n, m);
comb.rsz(2 * n + 5);
auto clc = [&](int x) { assert(x >= 0); return comb(2 * x, x) * comb.Inv(x + 1); };
fps pre(2 * n + 1);
fps c(n + 1);
rep(i, 1, n) c[i] = clc(i - 1) * m * mint(m - 1).pow(i - 1);
fps f = (fps{1} - c).inv(n + 1);
rep(i, 0, n) pre[i * 2] = f[i];
int E = n / 2 * 2 + 10;
fps d(E + 1);
rep(i, 0, E / 2) d[i * 2] = clc(i) * mint(m - 1).pow(i);
d = d << 2;
fps f1 = fps{1} - d * m;
fps f2 = fps{1} - d * (m - 1);
fps mus = (((f2 * f2).pre(E) - fps{0, 0, m - 1}) * f1).pre(E);
fps num = (f2 << 1) * m;
fps g = num * mus.inv();
for (int i = 1; i <= n; i += 2) pre[i] = g[i];
mint ans = 0;
rep(i, 1, 2 * n) ans += pre[gcd(i, 2 * n)];
io.writeln((ans / (2 * n)).val());
# ifdef LOCAL
cerr << "Time Information : " << endl;
cerr << "Time : " << clock ( ) * 1. / CLOCKS_PER_SEC * 1000. << "ms" << endl;
# endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3772kb
input:
3 2
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 0ms
memory: 4076kb
input:
5 3
output:
372
result:
ok single line: '372'
Test #3:
score: 0
Accepted
time: 2ms
memory: 4192kb
input:
2023 1126
output:
900119621
result:
ok single line: '900119621'
Test #4:
score: 0
Accepted
time: 4054ms
memory: 283332kb
input:
2882880 2892778
output:
421329574
result:
ok single line: '421329574'
Test #5:
score: 0
Accepted
time: 4074ms
memory: 289496kb
input:
2993760 2738336
output:
436950672
result:
ok single line: '436950672'
Test #6:
score: 0
Accepted
time: 4048ms
memory: 287732kb
input:
2948400 2885387
output:
4079921
result:
ok single line: '4079921'
Test #7:
score: 0
Accepted
time: 4001ms
memory: 278380kb
input:
2827440 2802363
output:
243025786
result:
ok single line: '243025786'
Test #8:
score: 0
Accepted
time: 4061ms
memory: 287408kb
input:
2962575 2720060
output:
651075152
result:
ok single line: '651075152'
Test #9:
score: 0
Accepted
time: 4070ms
memory: 286376kb
input:
2927925 2880541
output:
547809301
result:
ok single line: '547809301'
Test #10:
score: 0
Accepted
time: 4011ms
memory: 280748kb
input:
2837835 2955737
output:
66014301
result:
ok single line: '66014301'
Test #11:
score: 0
Accepted
time: 4097ms
memory: 289644kb
input:
2993445 2802568
output:
852155961
result:
ok single line: '852155961'
Test #12:
score: 0
Accepted
time: 4071ms
memory: 290376kb
input:
3000000 2721531
output:
330139745
result:
ok single line: '330139745'
Test #13:
score: 0
Accepted
time: 4104ms
memory: 289432kb
input:
2999999 2874068
output:
11487172
result:
ok single line: '11487172'
Test #14:
score: 0
Accepted
time: 0ms
memory: 4012kb
input:
1 2879803
output:
2879803
result:
ok single line: '2879803'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3836kb
input:
2 1634023
output:
363699315
result:
ok single line: '363699315'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3700kb
input:
3 1133070
output:
941531760
result:
ok single line: '941531760'
Test #17:
score: 0
Accepted
time: 0ms
memory: 4088kb
input:
4 2975012
output:
600544573
result:
ok single line: '600544573'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
5 478043
output:
284058138
result:
ok single line: '284058138'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3780kb
input:
6 377374
output:
21417458
result:
ok single line: '21417458'
Test #20:
score: 0
Accepted
time: 878ms
memory: 58232kb
input:
575199 2998538
output:
985915533
result:
ok single line: '985915533'
Test #21:
score: 0
Accepted
time: 1962ms
memory: 142092kb
input:
1566414 2864371
output:
555268438
result:
ok single line: '555268438'
Test #22:
score: 0
Accepted
time: 911ms
memory: 70216kb
input:
759957 2868981
output:
485958800
result:
ok single line: '485958800'
Test #23:
score: 0
Accepted
time: 2083ms
memory: 163056kb
input:
1906997 2799171
output:
340977363
result:
ok single line: '340977363'
Test #24:
score: 0
Accepted
time: 3896ms
memory: 253588kb
input:
2420926 497282
output:
927874930
result:
ok single line: '927874930'
Test #25:
score: 0
Accepted
time: 3971ms
memory: 269124kb
input:
2660337 2477085
output:
817341716
result:
ok single line: '817341716'
Test #26:
score: 0
Accepted
time: 4000ms
memory: 269244kb
input:
2661314 2721730
output:
576896798
result:
ok single line: '576896798'
Test #27:
score: 0
Accepted
time: 435ms
memory: 35044kb
input:
353896 2925858
output:
274269675
result:
ok single line: '274269675'
Test #28:
score: 0
Accepted
time: 3975ms
memory: 274068kb
input:
2726809 819583
output:
864442996
result:
ok single line: '864442996'
Test #29:
score: 0
Accepted
time: 4046ms
memory: 283608kb
input:
2893369 2895789
output:
521969424
result:
ok single line: '521969424'
Test #30:
score: 0
Accepted
time: 4016ms
memory: 281600kb
input:
2858441 2736227
output:
357358178
result:
ok single line: '357358178'
Test #31:
score: 0
Accepted
time: 4082ms
memory: 287008kb
input:
2954587 744730
output:
540167871
result:
ok single line: '540167871'
Test #32:
score: 0
Accepted
time: 2075ms
memory: 162844kb
input:
1902149 2964684
output:
967678819
result:
ok single line: '967678819'
Test #33:
score: 0
Accepted
time: 4046ms
memory: 284072kb
input:
2906083 2818150
output:
800415339
result:
ok single line: '800415339'
Test #34:
score: 0
Accepted
time: 975ms
memory: 83712kb
input:
947213 2843556
output:
704887450
result:
ok single line: '704887450'
Test #35:
score: 0
Accepted
time: 1809ms
memory: 109124kb
input:
1068401 2866144
output:
643696823
result:
ok single line: '643696823'
Test #36:
score: 0
Accepted
time: 3807ms
memory: 210276kb
input:
2097152 2706726
output:
879969833
result:
ok single line: '879969833'
Test #37:
score: 0
Accepted
time: 1966ms
memory: 143376kb
input:
1594323 1385645
output:
166289728
result:
ok single line: '166289728'
Test #38:
score: 0
Accepted
time: 2064ms
memory: 166116kb
input:
1953125 2702955
output:
356924892
result:
ok single line: '356924892'
Test #39:
score: 0
Accepted
time: 950ms
memory: 76044kb
input:
823543 469787
output:
217410289
result:
ok single line: '217410289'
Test #40:
score: 0
Accepted
time: 2010ms
memory: 154344kb
input:
1771561 2338083
output:
17307265
result:
ok single line: '17307265'
Test #41:
score: 0
Accepted
time: 432ms
memory: 37320kb
input:
371293 2739508
output:
768654866
result:
ok single line: '768654866'
Test #42:
score: 0
Accepted
time: 1877ms
memory: 122812kb
input:
1278612 1
output:
1
result:
ok single line: '1'
Test #43:
score: 0
Accepted
time: 4041ms
memory: 287720kb
input:
2949300 1
output:
1
result:
ok single line: '1'