拉格朗日反演

1.幂级数的复合

对于幂级数\(F(x)\)\(G(x)\),我们称\(F(G(x))\)为幂级数F和G的复合

2.复合逆:

如果\(F(x)\)\(G(x)\)满足\(F(G(x))=G(F(x))=x\)则称它们互为复合逆

3.拉格朗日反演:

如果\(F(x)\)\(G(x)\)互为复合逆,则有\([x^n]G(x)=\frac1n[x^{n-1}](\frac{1}{F(x)/x})^n\)

可以通过这个在\(O(n\log n)\)(多项式exp和ln求快速幂,巨大常数)或\(O(n\log^2n)\)(倍增快速幂,小常数)来求复合逆的某一项(如果求整个复合逆的最优复杂度为\(O(n^2)\)的大步小步思想)

一下为多项式倍增快速幂取模和拉格朗日反演的板子,可以配合ghj1222的多项式板子食用

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;
}
posted @ 2019-02-19 13:17  ghj1222  阅读(690)  评论(0编辑  收藏  举报