跳到正文

剪贴板

剪贴板 tuz6zm0w

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
//#pragma GCC optimize("O3", "Ofast", "-ffast-math", "-funroll-loops")
namespace fastio
{
	const int MAXBUF = 1 << 23;
	char buf[MAXBUF], *p1 = buf, *p2 = buf;
	char pbuf[MAXBUF], *pp = pbuf;
	inline char getc() { return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, MAXBUF, stdin)), p1 == p2 ? (char)EOF : *p1++; }
	inline void putc(char ch) { (pp == pbuf + MAXBUF) && (fwrite(pbuf, 1, MAXBUF, stdout), pp = pbuf), *pp++ = ch; }
	inline void flush() { fwrite(pbuf, 1, pp - pbuf, stdout), pp = pbuf; }
	struct flusher { ~flusher() { flush(); } } flusher;
	inline bool isspace(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\f' || ch == '\v'; }
	inline int read(char &ch)
	{
		for (ch = getc(); ch != EOF && isspace(ch); ch = getc());
		return ch != EOF;
	}
	template <class Tp> inline int read(Tp &x)
	{
		bool sign = 0;
		char ch = getc();
		for (; ch != EOF && !('0' <= ch && ch <= '9'); ch = getc()) sign |= (ch == '-');
		for (x = 0; '0' <= ch && ch <= '9'; ch = getc()) x = x * 10 + (ch ^ 48);
		x = sign ? -x : x;
		return ch != EOF;
	}
	inline int read(char *s)
	{
		char ch = getc();
		for (; isspace(ch); ch = getc());
		for (; ch != EOF && !isspace(ch); ch = getc()) *s++ = ch;
		*s = '\0';
		return ch != EOF;
	}
	inline int read(double &x)
	{
		bool sign = 0;
		char ch = getc();
		for (; ch != EOF && !('0' <= ch && ch <= '9'); ch = getc()) sign |= (ch == '-');
		for (x = 0; '0' <= ch && ch <= '9'; ch = getc()) x = x * 10 + (ch ^ 48);
		if (ch == '.')
		{
			double t = 0.1;
			for (ch = getc(); '0' <= ch && ch <= '9'; ch = getc(), t /= 10) x += t * (ch ^ 48);
		}
		x = sign ? -x : x;
		return ch != EOF;
	}
	template <class Tp, class ...Tps> inline int read(Tp &x, Tps &...rest) { return read(x) + read(rest...); }
	template <class Tp> inline int read(Tp *begin, Tp *end)
	{
		int res = 0;
		for (Tp* i = begin; i != end; i++) res += read(*i);
		return res;
	}
	inline void write(char ch) { putc(ch); }
	template <class Tp> inline void write(Tp x)
	{
		if (x < 0) putc('-'), x = -x;
		if (x > 9) write(x / 10);
		putc((char)(x % 10) ^ 48);
	}
	inline void write(char *s) { for (; *s != '\0'; s++) putc(*s); }
	inline void write(const char *s) { for (; *s != '\0'; s++) putc(*s); }
	inline void write(std::pair<double, int> x)
	{
		if (x.first < 0) putc('-'), x.first = -x.first;
		double t = 0.5;
		for (int j = 1; j <= x.second; j++) t /= 10;
		x.first = x.first + t;
		write((long long)(floor(x.first) + 0.1));
		if (x.second > 0) putc('.');
		while (x.second--) x.first = x.first * 10, putc((char)((long long)(floor(x.first) + 0.1) % 10) ^ 48);
	}
	inline void write(double x) { write({x, 6}); }
	template <class Tp, class ...Tps> inline void write(Tp x, Tps ...rest) { write(x), write(rest...); }
	template <class Tp> inline void write(Tp* begin, Tp* end)
	{
		for (Tp* i = begin; i != end; i++) write(*i), putc(' ');
		putc('\n');
	}
}
using fastio::read;
using fastio::write;

const long long p = 998244353, root = 3;

long long qpow(long long a, long long n)
{
	long long res = 1; a %= p;
	while (n) res = n & 1 ? res * a % p : res, a = a * a % p, n >>= 1;
	return res;
}

const int maxn = (1 << 18) + 10;

long long omega[maxn];
void ntt(long long f[], int k, int sign)
{
	int n = 1 << k;
	for (int i = 0, j = 0; i < n; i++)
	{
		if (j > i) std::swap(f[i], f[j]);
		for (int t = k - 1; t >= 0; t--)
		{
			j ^= 1 << t;
			if (j & (1 << t)) break;
		}
	}
	omega[1] = (omega[0] = 1) * qpow(root, (p - 1) / n);
	if (sign == -1) omega[1] = qpow(omega[1], p - 2);
	for (int i = 1; i < n; i++) omega[i] = omega[i - 1] * omega[1] % p;
	for (int t = 0; t < k; t++)
	{
		int tt = (1 << t) - 1;
		for (int i = 0; i < n; i++) if ((i & (1 << t)) == 0)
		{
			int j = i ^ (1 << t);
			long long x = f[i], y = omega[(i & tt) << (k - t - 1)] * f[j] % p;
			f[i] = (x + y) % p, f[j] = (x - y + p) % p;
		}
		else i += tt;
	}
	long long inv = qpow(n, p - 2);
	if (sign == -1) for (int i = 0; i < n; i++) f[i] = f[i] * inv % p;
}

long long F[maxn], G[maxn], H[maxn];
struct poly
{
	int n;
	std::vector<long long> a;
	poly(int _n) { n = _n, a.resize(n + 1); }
	poly(long long x) { n = 0, a.resize(1), a[0] = (x % p + p) % p; }
	poly mod(int m) const
	{
		poly res(m - 1);
		for (int j = 0; j < m; j++) res.a[j] = j <= n ? a[j] : 0;
		return res;
	}
	poly operator + (const poly &t) const
	{
		poly res(std::max(n, t.n));
		for (int i = 0; i <= res.n; i++) res.a[i] = ((i <= n ? a[i] : 0) + (i <= t.n ? t.a[i] : 0)) % p;
		return res;
	}
	poly operator - (const poly &t) const
	{
		poly res(std::max(n, t.n));
		for (int i = 0; i <= res.n; i++) res.a[i] = ((i <= n ? a[i] : 0) - (i <= t.n ? t.a[i] : 0) + p) % p;
		return res;
	}
	poly operator * (const poly &t) const
	{
		poly res(n + t.n);
		if (std::min(n, t.n) <= std::__lg(n + t.n + 1))
		{
			for (int i = 0; i <= n; i++) for (int j = 0; j <= t.n; j++)
				res.a[i + j] = (res.a[i + j] + a[i] * t.a[j]) % p;
		}
		else
		{
			int k = 0; while ((1 << k) < n + t.n + 1) k++;
			for (int i = 0; i < (1 << k); i++) F[i] = i <= n ? a[i] : 0;
			for (int i = 0; i < (1 << k); i++) G[i] = i <= t.n ? t.a[i] : 0;
			ntt(F, k, 1), ntt(G, k, 1);
			for (int i = 0; i < (1 << k); i++) H[i] = F[i] * G[i] % p;
			ntt(H, k, -1);
			for (int i = 0; i <= res.n; i++) res.a[i] = H[i];
		}
		return res;
	}
	poly inv(const int &m) const
	{
		poly t = qpow(a[0], p - 2);
		for (int i = 2; i / 2 < m; i *= 2) t = (t * ((poly)2LL - this->mod(i) * t)).mod(i);
		return t.mod(m);
	}
	poly reverse() const
	{
		poly res(n);
		for (int i = 0; i <= n; i++) res.a[n - i] = a[i];
		return res;
	}
	poly operator / (const poly &t) const
	{
		return (this->reverse() * t.reverse().inv(n - t.n + 1)).mod(n - t.n + 1).reverse();
	}
	poly operator % (const poly &t) const
	{
		return (*this - t * (*this / t)).mod(t.n);
	}
	poly operator << (const int &t) const
	{
		poly res(n);
		for (int i = 0; i + t <= n; i++) res.a[i] = a[i + t];
		return res;
	}
	poly operator >> (const int &t) const
	{
		poly res(n);
		for (int i = n; i - t >= 0; i--) res.a[i] = a[i - t];
		return res;
	}
	long long operator () (const long long &x) const
	{
		long long t = 1, res = 0;
		for (int i = 0; i <= n; i++) res = (res + a[i] * t) % p, t = t * x % p;
		return res;
	}
	long long operator [] (const int &x) const
	{
		return a[x];
	}
	void print()
	{
		for (int i = 0; i <= n; i++) printf("%lld ", a[i]);
		printf("\n");
	}
};