跳到正文

剪贴板

剪贴板 srcba13b

原文
剪贴板信息

剪贴板信息

收录
更新

CZの多项式模板

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int Qp(int, int);
const int P = 998244353, O = P - 1 >> 1, G = 5, Gi = 598946612, N = 400010, _Imag = 86583718;
int n, m, k, r[N];
string str;
int Qp(int a, int b = P - 2)
{
	int res = 1;
	for (; b; b >>= 1, a = a * a % P)
		if (b & 1)
			res = res * a % P;
	return res;
}
struct Poly
{
	int F[N], n; //[0,n)
	explicit Poly(int n = 0) : n(n)
	{
		memset(F, 0, sizeof F);
	}
	int &operator[](int x)
	{
		return F[x];
	}
	void resize(int m)
	{
		for (int i = m; i < n; ++i)
			F[i] = 0;
		n = m;
	}
	void print()
	{
		for (int i = 0; i < n; ++i)
			cout << F[i] << ' ';
		cout << '\n';
	}
	void rev()
	{
		reverse(F, F + n);
	}
	Poly Rev()
	{
		Poly f = *this;
		f.rev();
		return f;
	}
	void NTT(bool);
	friend Poly operator+(Poly &, Poly);
	friend Poly operator-(Poly &, Poly);
	friend Poly operator*(Poly, Poly);
	// friend Poly operator*(Poly,int);
	friend pair<Poly, Poly> operator/(Poly &, Poly &);
	// friend Poly operator/(Poly,int);
	friend Poly operator^(Poly &, int);
	friend void operator+=(Poly &, Poly &);
	friend void operator-=(Poly &, Poly &);
	friend void operator*=(Poly &, Poly);
	// friend void operator*=(Poly&,int);
	friend void operator/=(Poly &, Poly &);
	// friend void operator/=(Poly&,int);
	// friend void operator^=(Poly&,int);
	Poly Inverse();
	Poly Derivative();
	Poly Integral();
	Poly Ln();
	Poly Exp();
	Poly Sqrt();
	Poly Sin();
	Poly Cos();
	Poly Mul(int);
	Poly Div(int);
	// void inverse();
	void derivative();
	void integral();
	// void ln();
	// void exp();
	// void sqrt();
} A, B;
Poly operator+(Poly &a, Poly b)
{
	b.n = max(a.n, b.n);
	for (int i = 0; i < b.n; ++i)
		b[i] = (a[i] + b[i]) % P;
	return b;
}
void operator+=(Poly &a, Poly &b)
{
	a.n = max(a.n, b.n);
	for (int i = 0; i < a.n; ++i)
		a[i] = (a[i] + b[i]) % P;
}
Poly operator-(Poly &a, Poly b)
{
	b.n = max(a.n, b.n);
	for (int i = 0; i < b.n; ++i)
		b[i] = (a[i] - b[i] + P) % P;
	return b;
}
void operator-=(Poly &a, Poly &b)
{
	a.n = max(a.n, b.n);
	for (int i = 0; i < a.n; ++i)
		a[i] = (a[i] - b[i] + P) % P;
}
Poly Poly::Mul(int x)
{
	Poly a;
	a.n = n;
	x = (x % P + P) % P;
	for (int i = 0; i < n; ++i)
		a[i] = F[i] * x % P;
	return a;
}
Poly Poly::Div(int x)
{
	Poly a;
	a.n = n;
	x = Qp((x % P + P) % P);
	for (int i = 0; i < n; ++i)
		a[i] = F[i] * x % P;
	return a;
}
int setr(int);
Poly Poly::Derivative()
{
	Poly c(n - 1);
	for (int i = 0; i < n - 1; ++i)
		c[i] = F[i + 1] * (i + 1) % P;
	return c;
}
void Poly::derivative()
{
	for (int i = 0; i < n - 1; ++i)
		F[i] = F[i + 1] * (i + 1) % P;
	F[n - 1] = 0;
	resize(n - 1);
}
Poly Poly::Integral()
{
	Poly c(n + 1);
	for (int i = n; i; --i)
		c[i] = F[i - 1] * Qp(i) % P;
	return c;
}
void Poly::integral()
{
	resize(n + 1);
	for (int i = n; i; --i)
		F[i] = F[i - 1] * Qp(i) % P;
	F[0] = 0;
}
void Poly::NTT(bool s)
{
	int i, j, k, x, y, w, wn;
	for (i = 0; i < n; ++i)
		if (i < r[i])
			swap(F[i], F[r[i]]);
	for (i = 1; i < n; i <<= 1)
	{
		wn = Qp(s ? G : Gi, O / i);
		for (j = 0; j < n; j += i << 1)
			for (k = 0, w = 1; k < i; ++k, w = w * wn % P)
				x = F[j + k], y = w * F[i + j + k] % P, F[j + k] = (x + y) % P, F[i + j + k] = (x - y + P) % P;
	}
	if (!s)
		for (i = 0, x = Qp(n); i < n; ++i)
			F[i] = F[i] * x % P;
}
int setr(int x)
{
	int l = 1 + __lg(x - 1), n = 1 << l; // l=ceil(log2(x))
	for (int i = 0; i < n; ++i)
		r[i] = r[i >> 1] >> 1 | (i & 1) << l - 1;
	return n;
}
Poly operator*(Poly a, Poly b)
{
	int len = a.n + b.n - 1;
	a.n = b.n = setr(len);
	a.NTT(1), b.NTT(1);
	for (int i = 0; i < a.n; ++i)
		a[i] = a[i] * b[i] % P;
	a.NTT(0);
	a.resize(len);
	return a;
}
void operator*=(Poly &a, Poly b)
{
	int len = a.n + b.n - 1;
	a.n = b.n = setr(len);
	a.NTT(1), b.NTT(1);
	for (int i = 0; i < a.n; ++i)
		a[i] = a[i] * b[i] % P;
	a.NTT(0);
	a.resize(len);
}
void Mul(Poly &a, Poly b, function<int(int, int)> bh)
{
	int len = a.n + b.n - 1;
	a.n = b.n = setr(len);
	a.NTT(1), b.NTT(1);
	for (int i = 0; i < a.n; ++i)
		a[i] = bh(a[i], b[i]);
	a.NTT(0);
	a.resize(len);
}
void Mul_nocopy(Poly &a, Poly &b, function<int(int, int)> bh)
{
	int len = a.n + b.n - 1;
	a.n = b.n = setr(len);
	a.NTT(1), b.NTT(1);
	for (int i = 0; i < a.n; ++i)
		a[i] = bh(a[i], b[i]);
	a.NTT(0);
	a.resize(len);
}
Poly Poly::Inverse()
{
	Poly b, c;
	b.n = 2;
	b.F[0] = Qp(F[0]);
	int k = 1;
	while (k < n)
	{
		k <<= 1;
		b.resize(k);
		c.resize(k);
		copy(F, F + k, c.F);
		Mul_nocopy(b, c, [](int B, int C)
				 { return (2 - B * C % P + P) * B % P; });
		b.resize(k);
	}
	b.resize(n);
	return b;
}
pair<Poly, Poly> operator/(Poly &a, Poly &b)
{
	Poly f = a.Rev(), g = b.Rev(), h, v;
	g.resize(a.n - b.n + 1), g = g.Inverse();
	h = f * g, h.resize(a.n - b.n + 1), h.rev();
	v = h * b, v.resize(b.n - 1), v = a - v, v.resize(b.n - 1);
	return {h, v};
}
void operator/=(Poly &a, Poly &b)
{
	a *= b.Inverse();
}
Poly Poly::Ln()
{
	Poly x = Derivative();
	x /= *this, x.resize(n - 1), x.integral();
	return x;
}
Poly Poly::Exp()
{
	Poly b, c;
	b.n = 2;
	b.F[0] = 1;
	int k = 1;
	while (k < n)
	{
		k <<= 1;
		b.resize(k);
		c = b.Ln();
		for (int i = 0; i < k; ++i)
			c[i] = (F[i] - c[i] + P + !i) % P;
		b *= c;
		b.resize(k);
	}
	b.resize(n);
	return b;
}
/*
Poly operator^(Poly&a,int k)
{
  Poly b=a.Ln();
  for(int i=0;i<a.n;++i) b[i]=b[i]*k%P;
  return b.Exp();
}
void operator^=(Poly&a,int k)
{
  a=a.Ln();
  for(int i=0;i<a.n;++i) a[i]=a[i]*k%P;
  a=a.Exp();
}
*/
Poly Pow(Poly a, int k)
{
	a = a.Ln();
	for (int i = 0; i < a.n; ++i)
		a[i] = a[i] * k % P;
	return a.Exp();
}
Poly operator^(Poly a, string k)
{
	auto S_Mod = [&](int P)
	{
		int res = 0;
		for (char x : str)
			res = (res * 10 + x - '0') % P;
		return res;
	};
	auto Get = [&](int val)
	{
		int res = 0;
		for (char x : str)
		{
			res = res * 10 + x - '0';
			if (res > val)
				return -1ll;
		}
		return res;
	};
	int s = 0;
	while (!a[s])
		++s;
	int p = a[s], q = Qp(p), d;
	Poly b(a.n - s);
	for (int i = 0; i < a.n - s; ++i)
		b[i] = a[s + i] * q % P;
	b = Pow(b, S_Mod(P)), p = Qp(p, S_Mod(P - 1));
	for (int i = s; i < a.n; ++i)
		a[i] = b[i - s] * p % P;
	if (!s)
		goto end;
	d = Get(a.n / s);
	if (d == -1)
		a = Poly(a.n);
	else
	{
		d = s * (d - 1);
		copy_backward(a.F, a.F + a.n - d, a.F + a.n);
		fill(a.F, a.F + d, 0);
	}
end:;
	return a;
}
int readk()
{
	string s;
	cin >> s;
	int k = 0;
	if (s[0] == '-')
	{
		for (int i = 1; i < s.size(); ++i)
			k = (k * 10 + s[i] - '0') % P;
		k = (P - k) % P;
	}
	else
		for (int i = 0; i < s.size(); ++i)
			k = (k * 10 + s[i] - '0') % P;
	return k;
}
struct Complex
{
	int x, y;
};
void Mul(Complex &a, Complex &b, int p)
{
	a = {(a.x * b.x + p * a.y % P * b.y % P) % P, (a.x * b.y + a.y * b.x) % P};
}
int qp(Complex a, int b, int p)
{
	Complex res = {1, 0};
	for (; b; b >>= 1, Mul(a, a, p))
		if (b & 1)
			Mul(res, a, p);
	return res.x;
}
mt19937 rnd(random_device{}());
uniform_int_distribution<int> gen(0, P);
int Get(int p)
{
	if (Qp(p, O) == P - 1)
		return -1;
	while (true)
	{
		int a = gen(rnd), w = ((a * a - p) % P + P) % P;
		if (Qp(w, O) == P - 1)
		{
			int x = qp({a, 1}, P + 1 >> 1, w);
			return min(x, P - x);
		}
	}
}
Poly Poly::Sqrt()
{
	Poly b, c;
	b.n = 2;
	b[0] = Get(F[0]);
	int k = 1;
	while (k < n)
	{
		k <<= 1;
		b.resize(k);
		c.resize(k);
		copy(F, F + k, c.F);
		c *= b.Inverse();
		for (int i = 0; i < k; ++i)
			b[i] = (P - P / 2) * (b[i] + c[i]) % P;
	}
	b.resize(n);
	return b;
}
Poly Poly::Sin()
{
	Poly a = Mul(_Imag).Exp();
	Poly b = Mul(-_Imag).Exp();
	return (a - b).Div(2 * _Imag);
}
Poly Poly::Cos()
{
	Poly a = Mul(_Imag).Exp();
	Poly b = Mul(-_Imag).Exp();
	return (a + b).Div(2);
}
main()
{
	cin.tie(0)->sync_with_stdio(0);
	/*
	cin>>n>>m,A.n=n+1,B.n=m+1;
	for(int i=0;i<=n;++i)cin>>A[i];
	for(int i=0;i<=m;++i)cin>>B[i];
	Poly res=A*B;
	for(int i=0;i<=n+m;++i)cout<<res[i]<<' ';
	*/
	/*
	cin>>n,A.n=n;
	for(int i=0;i<n;++i)cin>>A[i];
	A.Inverse().print();
	*/
	/*
	cin>>n,A.n=n;
	for(int i=0;i<n;++i) cin>>A[i];
	A.Ln().print();
	*/
	/*
	cin>>n,A.n=n;
	for(int i=0;i<n;++i) cin>>A[i];
	A.Exp().print();
	*/
	/*
	cin>>n>>m,A.n=++n,B.n=++m;
	for(int i=0;i<n;++i) cin>>A[i];
	for(int i=0;i<m;++i) cin>>B[i];
	auto[F,G]=A/B;
	F.print(),G.print();
	*/
	/*
	cin>>n,A.n=n,k=readk();
	for(int i=0;i<n;++i) cin>>A[i];
	(A^k).print();
	*/
	/*
	cin>>n,A.n=n,k=P-P/2;
	for(int i=0;i<n;++i) cin>>A[i];
	(A^k).print();
	*/
	/*
	cin>>n>>str,A.n=n;
	for(int i=0;i<n;++i) cin>>A[i];
	(A^str).print();
	*/
	/*
	cin>>n,A.n=n;
	for(int i=0;i<n;++i) cin>>A[i];
	A.Sqrt().print();
	*/
	int typ;
	cin >> n >> typ;
	A.n = n;
	for (int i = 0; i < n; ++i)
		cin >> A[i];
	if (typ == 0)
		A.Sin().print();
	else
		A.Cos().print();
}