#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include <bit>
#include <random>
#include <complex>
#include <numbers>
using B = bool;
using S = int16_t;
using I = int32_t;
using L = long;
using LL = int64_t;
using SL = __int128_t;
using US = uint16_t;
using U = uint32_t;
using UL = unsigned long;
using ULL = uint64_t;
using USL = __uint128_t;
using F = float;
using D = double;
using LD = long double;
using C = std::complex<double>;
using VB = std::vector<B>;
using VS = std::vector<S>;
using VI = std::vector<I>;
using VL = std::vector<L>;
using VLL = std::vector<LL>;
using VSL = std::vector<__int128_t>;
using VUS = std::vector<US>;
using VU = std::vector<U>;
using VUL = std::vector<UL>;
using VULL = std::vector<ULL>;
using VUSL = std::vector<USL>;
using VF = std::vector<F>;
using VD = std::vector<D>;
using VLD = std::vector<LD>;
using VC = std::vector<C>;
#define IBUFSIZE 1000000
#define OBUFSIZE 1000000
#define INF ((((USL)1 << 126) - 1 << 1) + 1)
namespace random {std::random_device rd; std::mt19937_64 gen(rd());}
namespace IO
{
const USL brt10 = ((USL)1 << 64) / 10;
static char ibuf[IBUFSIZE], obuf[OBUFSIZE], *l = ibuf, *r = ibuf, *p = obuf;
#define getchar() (l == r && (r = (l = ibuf) + fread(ibuf, 1, IBUFSIZE, stdin), l == r) ? EOF : *l ++ )
#define flush() fwrite(obuf, 1, p - obuf, stdout), p = obuf
void putchar(char x) {if (p - obuf == OBUFSIZE) flush(); *p ++ = x;}
class Flusher {~Flusher() {flush();}} flusher;
U read(char s[]) {memset(s, 0, sizeof s); char *p = s, c = getchar(); while (isspace(c)) c = getchar(); while (!isspace(c) && c ^ EOF) *p ++ = c, c = getchar(); return p - s;}
template<class integer_class> void read(integer_class &x) {x = 0; char c = getchar(); I s = 1; while (isspace(c)) c = getchar(); if (c == '-') s = -1, c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (x ^ 48), c = getchar(); x *= s;}
template<class integer_class> void read(integer_class x[]) {for (integer_class &e : x) read(e);}
template<class float_class> void read_float(float_class &x) {USL integer, decimal = 0; read(integer); char c = getchar(); x = integer; LD d = 1; while (isdigit(c)) d /= 10, x += (c ^ 48) * d;}
template<class float_class> void read_float(float_class x[]) {for (float_class &e : x) read(e);}
void write(char s[]) {U n = strlen(s); char *end = s + n; if (n > p - obuf) {memcpy(p, s, p - obuf), flush(), n -= p - obuf; while (n >= OBUFSIZE) fwrite(end - n, 1, OBUFSIZE, stdout), n -= OBUFSIZE; memcpy(p, end - n, n), p = obuf + n;}}
template<class integer_class> void write(integer_class x) {if (x < 0) putchar('-'), x = -x; if (!x) return putchar('0'); static char t[40], p = 0; while (x) {t[p] = x - 10 * (brt10 * x >> 64), x /= 10; while (t[p] > 10) t[p] -= 10; t[p ++ ] ^= 48;} while (p) putchar(t[ -- p]);}
}
namespace math
{
// template<class integer_class> integer_class lowbit(integer_class x) {return 1 << std::countr_zero(x);}
template<class integer_class> integer_class lowbit(integer_class x) {return x & -x;}
template<class integer_class, class mod_class> void qmod(integer_class x[], mod_class mod) {const USL brt = ((USL)1 << 64) / mod; for (integer_class &e : x) {e -= mod * (brt * e >> 64); while (e > mod) e -= mod;}}
template<class numeric_class> I sign(numeric_class x, LD eps = 0) {return abs(x) > eps ? x > 0 ? 1 : -1 : 0;}
template<class integer_class, class mod_class> mod_class pow(integer_class a, integer_class b, mod_class mod) {integer_class res = 1 % mod; while (b) {if (b & 1) res = res * a % mod; a = a * a % mod, b >>= 1;} return res;}
template<class integer_class> integer_class phi(integer_class x) {integer_class res = x; for (U i{2}; i <= x / i; i ++ ) if (!(x % i)) {res /= i *= (i - 1); while (!(x % i)) x /= i;} if (x > 1) res /= x *= (x - 1); return res;}
template<class integer_class> bool miller_rabin(integer_class x, U k) {std::uniform_int_distribution<integer_class> ud; if (x < 2 || !(x & 1)) return false; U s = std::countr_zero(x - 1), d = (x - 1) >> s; for (U i{}; i < k; i ++ ) {integer_class a = ud(random::gen), r = pow(a, d, x); for (U j{}; j < s; j ++ ) {integer_class l = pow(r, 2, x); if (l == 1 && r ^ 1 && r ^ x - 1) return false; r = l;} if (r ^ 1) return false;} return true;}
template<class integer_class> std::pair<integer_class, integer_class> exgcd(integer_class a, integer_class b) {if (!b) return {1, 0}; auto p = exgcd(b, a % b); return {p.second, p.first - a / b * p.second};}
template<class integer_class> std::tuple<std::vector<integer_class>, VB, std::vector<integer_class>, VI> sieve(integer_class n) {std::vector<integer_class> p, phi(n + 1); VB st(n + 1); VI mu(n + 1); st[0] = true, st[1] = true, mu[1] = 1; for (integer_class i{2}; i <= n; i ++ ) {if (!st[i]) p.push_back(i), phi[i] = i - 1, mu[i] = -1; for (integer_class j{}; p[j] * i <= n; j ++ ) {st[p[j] * i] = true; if (!(i % p[j])) {phi[p[j] * i] = phi[i] * p[j]; break;} phi[p[j] * i] = phi[i] * (p[j] - 1), mu[p[j] * i] = -mu[i];}} return {p, st, phi, mu};}
void FFT(VC &f, I s) {U n = f.size(); if (n == 1) return ; VC t; for (C e : f) t.push_back(e); for (I i{}; i < n; i ++ ) f[i >> 1 + (i & 1 ? n >> 1 : 0)] = t[i]; VC a, b; for (I i{}; i < n >> 1; i ++ ) a.push_back(f[i]); for (I i{n >> 1}; i < n; i ++ ) b.push_back(f[i]); FFT(a, s), FFT(b, s); C c(1, 0), st(std::cos(2 * std::numbers::pi / n), std::sin(2 * std::numbers::pi * s / n)); for (I i{}; i < n >> 1; i ++ ) t[i] = a[i] + c * b[i], t[i + (n >> 1)] = a[i] - c * b[i], c *= st; f = t;}
}
template<const U base> class big_int
{
VU x;
public:
big_int(VU x_) : x(x_) {}
U size() {return x.size();}
U operator [](U idx) {return x[x.size() - idx - 1];}
big_int operator +(big_int y) {if (x.size() < y.x.size()) return y + big_int(x); VU res; U t{}; for (U i{}; i < x.size(); i ++ ) {t += x[i]; if (i < y.size()) t += y[i]; res.push_back(t % base), t /= base;} if (t) res.push_back(t); return big_int(res);}
big_int operator -(big_int y) {if (x.size() < y.x.size()) return y + big_int(x); VU res; for (U i{}, t{}; i < x.size(); i ++ ) {t = x[i] - t; if (i < y.size()) t -= y[i]; res.push_back((t + base) % base), t = t < 0;} while (res.size() > 1 && !res.back()) res.pop_back(); return big_int(res);}
};
懒得 debug 了,大家帮忙看一下
FFT 错了,待修
好像还有少分号的。另外
read
函数里面好像有点问题,感觉那个getchar
应该放到里面去。现在应该好了
懒,是罪恶之源你用的什么编译器?为什么我这里编译不过/dk
C++20才能过吧
哦。小熊猫C不支持C20。。。
class Flusher {~Flusher() {flush();}} flusher;
应该改成class Flusher { public: ~Flusher() {flush();}} flusher;
本来就不应该是 public 的吧
不 public 好像没法编译
我编译了,但是这里没错
其他地方错了现在能编译了
IO::Flusher
好像不大对能写写怎么调用吗?
IO 就是普通的快读模板,flush 是刷新输出,flusher 负责在程序结束时调用 flush()。
math 里 qmod 实现了 Barrett 约减,sieve 是素数、欧拉函数、莫比乌斯函数一起筛。
OK谢谢,收藏了。