多项式板子·新
Posted oier
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多项式板子·新相关的知识,希望对你有一定的参考价值。
upd于2.5: 今天是春节,为了使rp成指数增长,写了多项式exp板子
注意本板子使用过程中:每个函数传的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
");
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;
}
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_ln(a, len);
for (int i = 0; i < n; i++) printf("%d ", a[i]);
return 0;
}
以上是关于多项式板子·新的主要内容,如果未能解决你的问题,请参考以下文章