多项式板子·新

upd于2.19:拉格朗日反演、多项式倍增快速幂

(好像是还差一个多项式取模...)算了不写了

注意本板子使用过程中:每个函数传的len一定要保证是2的倍数,并且传递的数组需要保证他有多于2*len的空间

每个函数传进来的指针保证[0,len)有值,[len,2*len)有定义

注意new出来的内存一定要清零,不然肯定会炸

#include <cstdio>
#include <algorithm>
using namespace std;

const int p = 998244353;

int qpow(int x, int y)
{
	int res = 1;
	while (y > 0)
	{
		if (y & 1) res = res * (long long)x % p;
		x = x * (long long)x % p, y >>= 1;
	}
	return res;
}

void ntt(int *a, int len, int flag)
{
	int *r = new int[len];
	r[0] = 0;
	for (int i = 1; i < len; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) * (len >> 1));
	for (int i = 0; i < len; i++) if (i < r[i]) swap(a[i], a[r[i]]);
	for (int i = 1; i < len; i <<= 1)
	{
		int g1 = qpow(3, (p - 1) / (i * 2));
		for (int j = 0; j < len; j += i << 1)
			for (int g = 1, k = 0; k < i; k++, g = g * (long long)g1 % p)
			{
				int t = a[j + i + k] * (long long)g % p;
				a[j + i + k] = ((a[j + k] - t) % p + p) % p;
				a[j + k] = (a[j + k] + t) % p;
			}
	}
	if (flag == -1)
	{
		reverse(a + 1, a + len);
		for (int i = 0, inv = qpow(len, p - 2); i < len; i++) a[i] = a[i] * (long long)inv % p;
	}
	delete []r;
}

void poly_inv(int *a, int len)
{
	if (len == 1) { a[0] = qpow(a[0], p - 2); return; }
	int len1 = len / 2;
	int *f0 = new int[len * 2];
	for (int i = 0; i < len1; i++) f0[i] = a[i];
	for (int i = len1; i < len * 2; i++) f0[i] = 0;
	poly_inv(f0, len1);
	for (int i = len1; i < len * 2; i++) f0[i] = 0;
	ntt(f0, len * 2, 1), ntt(a, len * 2, 1);
	for (int i = 0; i < len * 2; i++) a[i] = ((2 * f0[i] % p - a[i] * (long long)f0[i] % p * f0[i] % p) % p + p) % p;
	ntt(a, len * 2, -1);
	for (int i = len; i < len * 2; i++) a[i] = 0;
	delete []f0;
}

void poly_derivation(int *a, int len)
{
	for (int i = 1; i < len; i++)
		a[i - 1] = a[i] * (long long)i % p;
	a[len - 1] = 0;
}

void poly_intergal(int *a, int len)
{
	for (int i = len + 1; i >= 1; i--)
		a[i] = a[i - 1] * (long long)qpow(i, p - 2) % p;
	a[0] = 0;
}

void poly_ln(int *a, int len)
{
	int *b = new int[len * 2];
	for (int i = 0; i < len; i++) b[i] = a[i];
	for (int i = len; i < len * 2; i++) b[i] = 0;
	poly_derivation(b, len);
	poly_inv(a, len);
	ntt(a, len * 2, 1);
	ntt(b, len * 2, 1);
	for (int i = 0; i < len * 2; i++)
		a[i] = a[i] * (long long)b[i] % p;
	ntt(a, len * 2, -1);
	poly_intergal(a, len);
	for (int i = len; i < len * 2; i++)
		a[i] = 0;
	delete []b;
}

//这里本来应该打二次剩余的,但是因为那道题只有常数项为1的情况,就写了暴力枚举了
int mod_sqrt(int x)
{
	for (int i = 0; i < p; i++) if (i * (long long)i % p == x) return x;
	printf("No Solution\n");
	return 0;
}

void poly_sqrt(int *a, int len)
{
	if (len == 1) { a[0] = mod_sqrt(a[0]); return; }
	int len1 = len / 2;
	int *f0 = new int[len * 2];
	for (int i = 0; i < len1; i++) f0[i] = a[i];
	for (int i = len1; i < len * 2; i++) f0[i] = 0;
	poly_sqrt(f0, len1);
	for (int i = len1; i < len * 2; i++) f0[i] = 0;
	int *tmp = new int[len * 2];
	for (int i = 0; i < len * 2; i++) tmp[i] = f0[i] * 2 % p;
	poly_inv(tmp, len);
	ntt(f0, len * 2, 1);
	for (int i = 0; i < len * 2; i++) f0[i] = f0[i] * (long long)f0[i] % p;
	ntt(f0, len * 2, -1);
	for (int i = 0; i < len; i++) f0[i] = (f0[i] + a[i]) % p;
	ntt(f0, len * 2, 1);
	for (int i = len; i < 2 * len; i++) tmp[i] = 0;
	ntt(tmp, len * 2, 1);
	for (int i = 0; i < len * 2; i++) a[i] = tmp[i] * (long long)f0[i] % p;
	ntt(a, len * 2, -1);
	for (int i = len; i < len * 2; i++) a[i] = 0;
	delete []tmp;
	delete []f0;
}

void poly_exp(int *a, int len)
{
	if (len == 1) { a[0]++; return; }
	int len1 = len / 2;
	int *f0 = new int[len * 2];
	for (int i = 0; i < len1; i++) f0[i] = a[i];
	for (int i = len1; i < len * 2; i++) f0[i] = 0;
	poly_exp(f0, len1);
	for (int i = len1; i < len * 2; i++) f0[i] = 0;
	int *lnf0 = new int[len * 2];
	for (int i = 0; i < len * 2; i++) lnf0[i] = f0[i];
	poly_ln(lnf0, len);
	a[0]++;
	for (int i = 0; i < len; i++)
	{
		a[i] -= lnf0[i];
		if (a[i] < 0) a[i] += p;
	}
	ntt(a, len * 2, 1);
	ntt(f0, len * 2, 1);
	for (int i = 0; i < len * 2; i++) a[i] = a[i] * (long long)f0[i] % p;
	ntt(a, len * 2, -1);
	for (int i = len; i < len * 2; i++) a[i] = 0;
}

void poly_qpow(int *a, int len, int n)
{
	int *tmp = new int[len * 2];
	for (int i = 0; i < len * 2; i++) tmp[i] = i >= len ? 0 : a[i], a[i] = (i == 0);
	while (n > 0)
	{
		ntt(tmp, len * 2, 1);
		if (n & 1)
		{
			ntt(a, len * 2, 1);
			for (int i = 0; i < len * 2; i++) a[i] = a[i] * (long long)tmp[i] % p;
			ntt(a, len * 2, -1);
			for (int i = len; i < len * 2; i++) a[i] = 0;
		}
		for (int i = 0; i < len * 2; i++) tmp[i] = tmp[i] * (long long)tmp[i] % p;
		ntt(tmp, len * 2, -1);
		for (int i = len; i < len * 2; i++) tmp[i] = 0;
		n >>= 1;
	}
	delete []tmp;
}

int lagrange_inversion(int *aa, int len, int n)
{
	int *a = new int[len * 2];
	for (int i = 0; i < len; i++) a[i] = aa[i];
	for (int i = len; i < len * 2; i++) a[i] = 0;
	for (int i = 1; i < len; i++) a[i - 1] = a[i];
	poly_inv(a, len);
	poly_qpow(a, len, n);
	int ans = a[n - 1] * (long long)qpow(n, p - 2) % p;
	delete []a;
	return ans;
}

int a[1000000], n, len = 1;

int main()
{
	scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	while (len < n) len *= 2;
	poly_exp(a, len);
	for (int i = 0; i < n; i++) printf("%d ", a[i]);
	return 0;
}

posted @ 2019-02-19 13:16  ghj1222  阅读(238)  评论(0编辑  收藏  举报