前言
之前零零碎碎的学了一些多项式的操作(模意义下即NTT)
现在打算还是仔细的学习一下吧,顺带自己推一波式子
然后把板子写一下
多项式乘法
见FFT
朴素的还是挺方便的
当然在一些情况下可以进行优化(暂时咕咕咕)
分治FFT&分治套FFT
这两个东西本质上只是思路,所以没什么好封装的
分治FFT
分治套FFT(这个是猎人杀的题解,里面有讲到分治套FFT)
模意义&循环卷积
一般多项式都有一个操作,模
x
n
x^n
xn,即次数大于等于
n
n
n的项都要舍去
注意这里要和循环卷积分开来,循环卷积是把所有指数大于等于
n
n
n的项系数都加到次数模
n
n
n的相应项里
多项式求逆
给定一个多项式
A
(
x
)
A(x)
A(x),求模
x
n
x^n
xn意义下的逆元
A
−
1
(
x
)
A^{-1}(x)
A−1(x)满足
A
(
x
)
A
−
1
(
x
)
≡
1
(
m
o
d
x
n
)
A(x)A^{-1}(x)\equiv1\pmod{x^n}
A(x)A−1(x)≡1(modxn)
我们发现:
若
n
=
1
n=1
n=1,我们可以
O
(
l
o
g
n
)
\mathcal O(logn)
O(logn)求出常数项的逆元
若
n
≠
1
n\neq1
n̸=1,我们考虑递归解决这个问题
设我们要求的为
B
(
x
)
B(x)
B(x),即
A
(
x
)
B
(
x
)
≡
1
(
m
o
d
x
n
)
A(x)B(x)\equiv1\pmod{x^n}
A(x)B(x)≡1(modxn)
已经递归求得
B
0
(
x
)
B_0(x)
B0(x),即
A
(
x
)
B
0
(
x
)
≡
1
(
m
o
d
x
⌈
n
2
⌉
)
A(x)B_0(x)\equiv1\pmod{x^{\left\lceil\frac n2\right\rceil}}
A(x)B0(x)≡1(modx⌈2n⌉)
容易发现
A
(
x
)
B
(
x
)
≡
1
(
m
o
d
x
⌈
n
2
⌉
)
A(x)B(x)\equiv1\pmod{x^{\left\lceil\frac n2\right\rceil}}
A(x)B(x)≡1(modx⌈2n⌉)
相减得
A
(
x
)
(
B
(
x
)
−
B
0
(
x
)
)
≡
0
(
m
o
d
x
⌈
n
2
⌉
)
A(x)(B(x)-B_0(x))\equiv0\pmod{x^{\left\lceil\frac n2\right\rceil}}
A(x)(B(x)−B0(x))≡0(modx⌈2n⌉)
我们发现,如果
A
(
x
)
m
o
d
  
x
⌈
n
2
⌉
=
0
A(x)\mod x^{\left\lceil\frac n2\right\rceil}=0
A(x)modx⌈2n⌉=0,显然
B
0
(
x
)
=
0
B_0(x)=0
B0(x)=0,我们的期望结果是
B
(
x
)
=
0
B(x)=0
B(x)=0,那么
B
(
x
)
=
B
0
(
x
)
B(x)=B_0(x)
B(x)=B0(x)
如果
A
(
x
)
m
o
d
  
x
⌈
n
2
⌉
≠
0
A(x)\mod x^{\left\lceil\frac n2\right\rceil}\neq0
A(x)modx⌈2n⌉̸=0,那么右边一项在模意义下为
0
0
0
所以,得出如下式子
B
(
x
)
−
B
0
(
x
)
≡
0
(
m
o
d
x
⌈
n
2
⌉
)
B(x)-B_0(x)\equiv0\pmod{x^{\left\lceil\frac n2\right\rceil}}
B(x)−B0(x)≡0(modx⌈2n⌉)
两边平方
B
2
(
x
)
−
2
B
(
x
)
B
0
(
x
)
+
B
0
2
(
x
)
≡
0
(
m
o
d
x
n
)
B^2(x)-2B(x)B_0(x)+B_0^2(x)\equiv0\pmod{x^n}
B2(x)−2B(x)B0(x)+B02(x)≡0(modxn)
我们发现,所有次数小于
n
n
n的项至少由一边的一个次数小于
⌈
n
2
⌉
\left\lceil\frac n2\right\rceil
⌈2n⌉的项乘得,所以可以将模数改为
x
n
x^n
xn
我们将等式两边同时乘上
A
(
x
)
A(x)
A(x)
得到
B
(
x
)
−
2
B
0
(
x
)
+
A
(
x
)
B
0
2
(
x
)
≡
0
(
m
o
d
x
n
)
B(x)-2B_0(x)+A(x)B_0^2(x)\equiv0\pmod{x^n}
B(x)−2B0(x)+A(x)B02(x)≡0(modxn)
移项得
B
(
x
)
≡
2
B
0
(
x
)
−
A
(
x
)
B
0
2
(
x
)
(
m
o
d
x
n
)
B(x)\equiv2B_0(x)-A(x)B_0^2(x)\pmod{x^n}
B(x)≡2B0(x)−A(x)B02(x)(modxn)
合并得
B
(
x
)
≡
B
0
(
x
)
(
2
−
A
(
x
)
B
0
(
x
)
)
(
m
o
d
x
n
)
B(x)\equiv B_0(x)(2-A(x)B_0(x))\pmod{x^n}
B(x)≡B0(x)(2−A(x)B0(x))(modxn)
复杂度
T
(
n
)
=
T
(
n
2
)
+
O
(
n
l
o
g
n
)
=
O
(
n
l
o
g
n
)
T(n)=T(\frac n2)+\mathcal O(nlogn)=O(nlogn)
T(n)=T(2n)+O(nlogn)=O(nlogn)
小提示:在具体代码实现的时候可以不用封装的多项式乘法,而只在开始和结束分别做DFT/IDFT即可,中间的运算用每个点值进行运算来解决,能够大大减少常数
多项式除法/取模
给定一个长度为
n
n
n的多项式
A
(
x
)
A(x)
A(x),给定一个长度为
m
(
m
≤
n
)
m(m\le n)
m(m≤n)的多项式
B
(
x
)
B(x)
B(x)
求两个多项式
D
(
x
)
,
R
(
x
)
D(x),R(x)
D(x),R(x)满足
A
(
x
)
=
D
(
x
)
B
(
x
)
+
R
(
x
)
A(x)=D(x)B(x)+R(x)
A(x)=D(x)B(x)+R(x)
并且
L
e
n
t
h
R
(
x
)
<
m
Lenth_{R(x)}<m
LenthR(x)<m(定义
L
e
n
t
h
F
(
x
)
Lenth_{F(x)}
LenthF(x)为多项式
F
(
x
)
F(x)
F(x)的长度)
容易发现,
L
e
n
t
h
D
(
x
)
Lenth_{D(x)}
LenthD(x)
≤
n
−
m
+
1
\le n-m+1
≤n−m+1
注意这里没有模意义下,所以答案是唯一的
传统的除法复杂度是
O
(
n
m
)
\mathcal O(nm)
O(nm)的,并不优秀
我们发现问题在
R
(
x
)
R(x)
R(x)上,我们想办法消除影响
有一个很好的思路是:选择一个合适的
x
a
x^a
xa使得在这个模意义下的
D
(
x
)
D(x)
D(x)不受影响,而
R
(
x
)
R(x)
R(x)被模掉,这样就可以直接套用多项式求逆了
我们发现
L
e
n
t
h
R
(
x
)
+
L
e
n
t
h
D
(
x
)
≤
n
Lenth_{R(x)}+Lenth_{D(x)}\le n
LenthR(x)+LenthD(x)≤n
那么我们想办法把
L
e
n
t
h
R
(
x
)
Lenth_{R(x)}
LenthR(x)的那一部分进行系数翻转
定义对于一个多项式在次数
n
n
n下的翻转:
F
R
n
(
x
)
=
F
(
1
x
)
x
n
−
1
=
∑
i
=
0
n
−
1
a
n
−
1
−
i
x
i
F^{R_n}(x)=F(\frac 1x)x^{n-1}=\sum_{i=0}^{n-1}a_{n-1-i}x^i
FRn(x)=F(x1)xn−1=i=0∑n−1an−1−ixi
(我们发现对于
F
(
x
)
⇒
F
R
a
(
x
)
F(x)\Rightarrow F^{R_a}(x)
F(x)⇒FRa(x)需要乘上
x
a
−
1
x^{a-1}
xa−1)
对于式子
A
(
x
)
=
D
(
x
)
B
(
x
)
+
R
(
x
)
A(x)=D(x)B(x)+R(x)
A(x)=D(x)B(x)+R(x)
我们将
1
x
\frac 1x
x1带入(这样等式显然依然成立),得到
A
(
1
x
)
=
D
(
1
x
)
B
(
1
x
)
+
R
(
1
x
)
A(\frac 1x)=D(\frac 1x)B(\frac 1x)+R(\frac 1x)
A(x1)=D(x1)B(x1)+R(x1)
等式两边同乘
x
n
−
1
x^{n-1}
xn−1,得到
A
R
n
(
x
)
=
D
R
n
−
m
+
1
(
x
)
B
R
m
(
x
)
+
x
n
−
m
R
R
m
(
x
)
A^{R_n}(x)=D^{R_{n-m+1}}(x)B^{R_{m}}(x)+x^{n-m}R^{R_m}(x)
ARn(x)=DRn−m+1(x)BRm(x)+xn−mRRm(x)
我们发现我们发现这个式子只有最后一项的
x
x
x次数均
≥
n
−
m
\ge n-m
≥n−m,其余项的
x
x
x次数均
<
n
−
m
<n-m
<n−m,所以我们把这个式子转到模
x
n
−
m
x^{n-m}
xn−m意义下即可,得到
A
R
n
(
x
)
≡
D
R
n
−
m
+
1
(
x
)
B
R
m
(
x
)
(
m
o
d
x
n
−
m
)
A^{R_n}(x)\equiv D^{R_{n-m+1}}(x)B^{R_{m}}(x)\pmod{x^{n-m}}
ARn(x)≡DRn−m+1(x)BRm(x)(modxn−m)
我们用多项式求逆
O
(
n
l
o
g
n
)
\mathcal O(nlogn)
O(nlogn)求
D
(
x
)
D(x)
D(x),得到
D
(
x
)
D(x)
D(x)后可以直接用多项式乘法和减法求出
R
(
x
)
R(x)
R(x)
复杂度
O
(
n
l
o
g
n
)
\mathcal O(nlogn)
O(nlogn)
多项式微分
由于
x
n
(
n
∈
N
)
x^n(n\in \mathbb N)
xn(n∈N)的一阶导数为
n
∗
x
n
−
1
n*x^{n-1}
n∗xn−1
所以直接做即可
复杂度
O
(
n
)
\mathcal O(n)
O(n)
多项式积分
根据微分的式子积回来即可,写板子的话常数项默认为
0
0
0吧
需要预处理逆元
复杂度
O
(
n
)
\mathcal O(n)
O(n)
多项式牛顿迭代法(思路)
假设我们现在有一个函数
g
(
x
)
g(x)
g(x)要求,求
f
(
x
)
f(x)
f(x)满足
g
(
f
(
x
)
)
≡
0
(
m
o
d
x
n
)
g(f(x))\equiv0\pmod{x^n}
g(f(x))≡0(modxn)
多项式牛顿迭代法需要确定初始近似值,即前
1
1
1位
这
1
1
1位的值也就是常数项需要单独讨论
假设当前的近似值为
f
0
(
x
)
f_0(x)
f0(x),其确定位数为
n
n
n
我们现在要求
f
(
x
)
f(x)
f(x),其确定位数为
2
n
2n
2n
已知条件:
f
0
(
x
)
≡
f
(
x
)
(
m
o
d
x
n
)
f_0(x)\equiv f(x)\pmod{x^n}
f0(x)≡f(x)(modxn)
g
(
f
0
(
x
)
)
≡
0
(
m
o
d
x
n
)
g(f_0(x))\equiv0\pmod{x^n}
g(f0(x))≡0(modxn)
我们将
g
(
f
(
x
)
)
g(f(x))
g(f(x))在
f
0
(
x
)
f_0(x)
f0(x)上泰勒展开并代入
f
(
x
)
f(x)
f(x)
g
(
f
(
x
)
)
=
∑
i
=
0
∞
g
(
i
)
(
f
0
(
x
)
)
∗
(
f
(
x
)
−
f
0
(
x
)
)
i
i
!
=
g
(
f
0
(
x
)
)
0
!
+
g
′
(
f
0
(
x
)
)
(
f
(
x
)
−
f
0
(
x
)
)
1
!
+
g
′
′
(
f
0
(
x
)
)
(
f
(
x
)
−
f
0
(
x
)
)
2
2
!
⋅
⋅
⋅
\begin{aligned} g(f(x))&=\sum_{i=0}^{\infty}\frac{g^{(i)}(f_0(x))*(f(x)-f_0(x))^i}{i!}\\ &=\frac {g(f_0(x))}{0!}+\frac{g'(f_0(x))(f(x)-f_0(x))}{1!}+\frac{g''(f_0(x))(f(x)-f_0(x))^2}{2!}··· \end{aligned}
g(f(x))=i=0∑∞i!g(i)(f0(x))∗(f(x)−f0(x))i=0!g(f0(x))+1!g′(f0(x))(f(x)−f0(x))+2!g′′(f0(x))(f(x)−f0(x))2⋅⋅⋅
注意:由于我们这里求的是f(x),所以求导是对f(x)求导而不是对x求导
我们发现,由于
f
(
x
)
−
f
0
(
x
)
≡
0
(
m
o
d
x
n
)
f(x)-f_0(x)\equiv0\pmod{x^n}
f(x)−f0(x)≡0(modxn),而我们求的
g
(
f
(
x
)
)
g(f(x))
g(f(x))是在模
x
2
n
x^{2n}
x2n意义下的,所以所有
i
≥
2
i\ge2
i≥2的项都是无用的
所以得到
g
(
f
(
x
)
)
≡
g
(
f
0
(
x
)
)
+
g
′
(
f
0
(
x
)
)
(
f
(
x
)
−
f
0
(
x
)
)
(
m
o
d
x
2
n
)
g(f(x))\equiv g(f_0(x))+g'(f_0(x))(f(x)-f_0(x))\pmod{x^{2n}}
g(f(x))≡g(f0(x))+g′(f0(x))(f(x)−f0(x))(modx2n)
将
g
(
f
(
x
)
)
≡
0
(
m
o
d
x
2
n
)
g(f(x))\equiv0\pmod{x^{2n}}
g(f(x))≡0(modx2n)带入并移项得
f
(
x
)
≡
f
0
(
x
)
−
g
(
f
0
(
x
)
)
g
′
(
f
0
(
x
)
)
(
m
o
d
x
2
n
)
f(x)\equiv f_0(x)-\frac{g(f_0(x))}{g'(f_0(x))}\pmod{x^{2n}}
f(x)≡f0(x)−g′(f0(x))g(f0(x))(modx2n)
如果求导后的结果计算很方便,那么就可以迭代求值
多项式开根
承接牛顿迭代的思路
对于一个多项式
f
(
x
)
f(x)
f(x),这里
g
(
f
(
x
)
)
=
f
2
(
x
)
−
A
(
x
)
g(f(x))=f^2(x)-A(x)
g(f(x))=f2(x)−A(x)
即
f
2
(
x
)
≡
A
(
x
)
(
m
o
d
x
n
)
f^2(x)\equiv A(x)\pmod{x^n}
f2(x)≡A(x)(modxn)
我们代入迭代的式子(提示:
x
2
x^2
x2的导数为
2
x
2x
2x)
f
(
x
)
≡
f
0
(
x
)
−
g
(
f
0
(
x
)
)
g
′
(
f
0
(
x
)
)
(
m
o
d
x
n
)
≡
f
0
(
x
)
−
f
0
2
(
x
)
−
A
(
x
)
2
f
0
(
x
)
(
m
o
d
x
n
)
≡
1
2
(
f
0
(
x
)
+
A
(
x
)
f
0
(
x
)
)
(
m
o
d
x
n
)
\begin{aligned} f(x)&\equiv f_0(x)-\frac{g(f_0(x))}{g'(f_0(x))}\pmod{x^{n}}\\ &\equiv f_0(x)-\frac{f_0^2(x)-A(x)}{2f_0(x)}\pmod{x^{n}}\\ &\equiv\frac12(f_0(x)+\frac{A(x)}{f_0(x)})\pmod{x^{n}}\\ \end{aligned}
f(x)≡f0(x)−g′(f0(x))g(f0(x))(modxn)≡f0(x)−2f0(x)f02(x)−A(x)(modxn)≡21(f0(x)+f0(x)A(x))(modxn)
然后常数项需要在模意义下开根(所以如果常数项不是这个模数下的二次剩余那么这个多项式将无法开根,一般题目会说明保证常数项是模意义下的二次剩余,二次剩余博客传送门),当然如果保证常数项为
1
1
1,那么结果也为
1
1
1
多项式ln
复合函数求导法则:
设
f
(
x
)
=
g
(
h
(
x
)
)
f(x)=g(h(x))
f(x)=g(h(x))
则
f
′
(
x
)
=
g
′
(
h
(
x
)
)
h
′
(
x
)
f'(x)=g'(h(x))h'(x)
f′(x)=g′(h(x))h′(x)
我们现在有一个多项式
A
(
x
)
A(x)
A(x)
要求
l
n
(
A
(
x
)
)
ln(A(x))
ln(A(x))
l
n
(
A
(
x
)
)
=
∫
l
n
′
(
A
(
x
)
)
=
∫
A
′
(
x
)
A
(
x
)
\begin{aligned} ln(A(x))&=\int ln'(A(x))\\ &=\int \frac{A'(x)}{A(x)} \end{aligned}
ln(A(x))=∫ln′(A(x))=∫A(x)A′(x)
注意:这里的ln都是对于每个x取确定值的,所以是对x求导
用上面的微分和积分,这些都是
O
(
n
)
\mathcal O(n)
O(n)的,求逆
O
(
n
l
o
g
n
)
\mathcal O(nlogn)
O(nlogn),总复杂度
O
(
n
l
o
g
n
)
\mathcal O(nlogn)
O(nlogn)
同开根一样,常数项也需要在模意义下进行
l
n
ln
ln
一般写法默认
A
(
x
)
A(x)
A(x)常数项为
1
1
1,计算出的
f
(
x
)
f(x)
f(x)的常数项为
0
0
0
多项式exp
求
e
A
(
x
)
(
m
o
d
x
n
)
e^{A(x)}\pmod{x^n}
eA(x)(modxn)
化多项式牛顿迭代经典式
设
f
(
x
)
≡
e
A
(
x
)
(
m
o
d
x
n
)
f(x)\equiv e^{A(x)}\pmod{x^n}
f(x)≡eA(x)(modxn)
两边求ln
l
n
(
f
(
x
)
)
≡
A
(
x
)
(
m
o
d
x
n
)
ln(f(x))\equiv A(x)\pmod{x^n}
ln(f(x))≡A(x)(modxn)
l
n
(
f
(
x
)
)
−
A
(
x
)
≡
0
(
m
o
d
x
n
)
ln(f(x))-A(x)\equiv0\pmod{x^n}
ln(f(x))−A(x)≡0(modxn)
代入式子
f
(
x
)
≡
f
0
(
x
)
−
g
(
f
0
(
x
)
)
g
′
(
f
0
(
x
)
)
(
m
o
d
x
n
)
≡
f
0
(
x
)
−
l
n
(
f
0
(
x
)
)
−
A
(
x
)
1
f
0
(
x
)
(
m
o
d
x
n
)
≡
f
0
(
x
)
(
1
−
l
n
(
f
0
(
x
)
)
+
A
(
x
)
)
(
m
o
d
x
n
)
\begin{aligned} f(x)&\equiv f_0(x)-\frac{g(f_0(x))}{g'(f_0(x))}\pmod{x^{n}}\\ &\equiv f_0(x)-\frac{ln(f_0(x))-A(x)}{\frac1{f_0(x)}}\pmod{x^{n}}\\ &\equiv f_0(x)(1-ln(f_0(x))+A(x))\pmod{x^{n}}\\ \end{aligned}
f(x)≡f0(x)−g′(f0(x))g(f0(x))(modxn)≡f0(x)−f0(x)1ln(f0(x))−A(x)(modxn)≡f0(x)(1−ln(f0(x))+A(x))(modxn)
同上,常数项需要单独计算exp
一般写法默认
A
(
x
)
A(x)
A(x)常数项为
0
0
0,计算出的
f
(
x
)
f(x)
f(x)的常数项为
1
1
1
复杂度
O
(
n
l
o
g
n
)
\mathcal O(nlogn)
O(nlogn)
多项式k次幂
快速幂:由于每次要IDFT回来保证长度,所以复杂度为
O
(
n
l
o
g
n
l
o
g
k
)
\mathcal O(nlognlogk)
O(nlognlogk)
提出最低次数项的次数及
x
x
x(设为
a
x
b
ax^b
axb),再进行ln和exp即可
f
k
(
x
)
=
a
k
x
k
b
e
x
p
(
k
∗
l
n
(
f
(
x
)
a
x
b
)
)
f^k(x)=a^kx^{kb}exp(k*ln(\frac{f(x)}{ax^b}))
fk(x)=akxkbexp(k∗ln(axbf(x)))
复杂度
O
(
n
l
o
g
n
)
\mathcal O(nlogn)
O(nlogn)
封装代码
贴出封装的代码,所有功能都能很方便的调用
这是loj150挑战多项式的ac代码
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<vector>
namespace fast_IO
{
const int IN_LEN=10000000,OUT_LEN=10000000;
char ibuf[IN_LEN],obuf[OUT_LEN],*ih=ibuf+IN_LEN,*oh=obuf,*lastin=ibuf+IN_LEN,*lastout=obuf+OUT_LEN-1;
inline char getchar_(){return (ih==lastin)&&(lastin=(ih=ibuf)+fread(ibuf,1,IN_LEN,stdin),ih==lastin)?EOF:*ih++;}
inline void putchar_(const char x){if(oh==lastout)fwrite(obuf,1,oh-obuf,stdout),oh=obuf;*oh++=x;}
inline void flush(){fwrite(obuf,1,oh-obuf,stdout);}
}
using namespace fast_IO;
#define getchar() getchar_()
#define putchar(x) putchar_((x))
typedef long long ll;
#define rg register
template <typename T> inline T max(const T a,const T b){return a>b?a:b;}
template <typename T> inline T min(const T a,const T b){return a<b?a:b;}
template <typename T> inline T mind(T&a,const T b){a=a<b?a:b;}
template <typename T> inline T maxd(T&a,const T b){a=a>b?a:b;}
template <typename T> inline T abs(const T a){return a>0?a:-a;}
template <typename T> inline void swap(T&a,T&b){T c=a;a=b;b=c;}
template <typename T> inline void swap(T*a,T*b){T c=a;a=b;b=c;}
template <typename T> inline T gcd(const T a,const T b){if(!b)return a;return gcd(b,a%b);}
template <typename T> inline T square(const T x){return x*x;};
template <typename T> inline void read(T&x)
{
char cu=getchar();x=0;bool fla=0;
while(!isdigit(cu)){if(cu=='-')fla=1;cu=getchar();}
while(isdigit(cu))x=x*10+cu-'0',cu=getchar();
if(fla)x=-x;
}
template <typename T> void printe(const T x)
{
if(x>=10)printe(x/10);
putchar(x%10+'0');
}
template <typename T> inline void print(const T x)
{
if(x<0)putchar('-'),printe(-x);
else printe(x);
}
const int maxn=2097152,mod=998244353;
inline int Md(const int x){return x>=mod?x-mod:x;}
template<typename T>
inline int pow(int x,T y)
{
rg int res=1;x%=mod;
for(;y;y>>=1,x=(ll)x*x%mod)if(y&1)res=(ll)res*x%mod;
return res;
}
namespace Poly///namespace of Poly
{
int W_[maxn],ha[maxn],hb[maxn],Inv[maxn];
inline void init(const int x)
{
rg int tim=0,lenth=1;
while(lenth<x)lenth<<=1,tim++;
for(rg int i=1;i<lenth;i<<=1)
{
const int WW=pow(3,(mod-1)/(i*2));
W_[i]=1;
for(rg int j=i+1,k=i<<1;j<k;j++)W_[j]=(ll)W_[j-1]*WW%mod;
}
Inv[0]=Inv[1]=1;
for(rg int i=2;i<x;i++)Inv[i]=(ll)(mod-mod/i)*Inv[mod%i]%mod;
}
int L;
inline void DFT(int*A)//prepare:init L
{
for(rg int i=0,j=0;i<L;i++)
{
if(i>j)swap(A[i],A[j]);
for(rg int k=L>>1;(j^=k)<k;k>>=1);
}
for(rg int i=1;i<L;i<<=1)
for(rg int j=0,J=i<<1;j<L;j+=J)
for(rg int k=0;k<i;k++)
{
const int x=A[j+k],y=(ll)A[j+k+i]*W_[i+k]%mod;
A[j+k]=Md(x+y),A[j+k+i]=Md(mod+x-y);
}
}
void IDFT(int*A)
{
for(rg int i=1;i<L-i;i++)swap(A[i],A[L-i]);
DFT(A);
}
inline int Quadratic_residue(const int a)
{
if(a==0)return 0;
int b=(rand()<<14^rand())%mod;
while(pow(b,(mod-1)>>1)!=mod-1)b=(rand()<<14^rand())%mod;
int s=mod-1,t=0,x,inv=pow(a,mod-2),f=1;
while(!(s&1))s>>=1,t++,f<<=1;
t--,x=pow(a,(s+1)>>1),f>>=1;
while(t)
{
f>>=1;
if(pow((int)((ll)inv*x%mod*x%mod),f)!=1)x=(ll)x*pow(b,s)%mod;
t--,s<<=1;
}
return min(x,mod-x);
}
struct poly
{
std::vector<int>A;
poly(){A.resize(0);}
poly(const int x){A.resize(1),A[0]=x;}
inline int&operator[](const int x){return A[x];}
inline int operator[](const int x)const{return A[x];}
inline void clear(){A.clear();}
inline unsigned int size()const{return A.size();}
inline void resize(const unsigned int x){A.resize(x);}
void RE(const int x)
{
A.resize(x);
for(rg int i=0;i<x;i++)A[i]=0;
}
void readin(const int MAX)
{
A.resize(MAX);
for(rg int i=0;i<MAX;i++)read(A[i]);
}
void putout()const
{
for(rg unsigned int i=0;i<A.size();i++)print(A[i]),putchar(' ');
}
inline poly operator +(const poly b)const
{
poly RES;
RES.resize(max(size(),b.size()));
for(rg unsigned int i=0;i<RES.size();i++)RES[i]=Md((i<size()?A[i]:0)+(i<b.size()?b[i]:0));
return RES;
}
inline poly operator -(const poly b)const
{
poly RES;
RES.resize(max(size(),b.size()));
for(rg unsigned int i=0;i<RES.size();i++)RES[i]=Md((i<size()?A[i]:0)+mod-(i<b.size()?b[i]:0));
return RES;
}
inline poly operator *(const int b)const
{
poly RES=*this;
for(rg unsigned int i=0;i<RES.size();i++)RES[i]=(ll)RES[i]*b%mod;
return RES;
}
inline poly operator /(const int b)const
{
poly RES=(*this)*pow(b,mod-2);
return RES;
}
inline poly operator *(const poly b)const
{
const int RES=A.size()+b.size()-1;
L=1;while(L<RES)L<<=1;
poly c;c.A.resize(RES);
memset(ha,0,L<<2);
memset(hb,0,L<<2);
for(rg unsigned int i=0;i<A.size();i++)ha[i]=A[i];
for(rg unsigned int i=0;i<b.A.size();i++)hb[i]=b.A[i];
DFT(ha),DFT(hb);
for(rg int i=0;i<L;i++)ha[i]=(ll)ha[i]*hb[i]%mod;
IDFT(ha);
const int inv=pow(L,mod-2);
for(rg int i=0;i<RES;i++)c.A[i]=(ll)ha[i]*inv%mod;
return c;
}
inline poly inv()const
{
poly C;
if(A.size()==1){C=*this;C[0]=pow(C[0],mod-2);return C;}
C.resize((A.size()+1)>>1);
for(rg unsigned int i=0;i<C.size();i++)C[i]=A[i];
C=C.inv();
L=1;while(L<(int)size()*2-1)L<<=1;
for(rg unsigned int i=0;i<A.size();i++)ha[i]=A[i];
for(rg unsigned int i=0;i<C.size();i++)hb[i]=C[i];
memset(ha+A.size(),0,(L-A.size())<<2);
memset(hb+C.size(),0,(L-C.size())<<2);
DFT(ha),DFT(hb);
for(rg int i=0;i<L;i++)ha[i]=(ll)(2-(ll)hb[i]*ha[i]%mod+mod)*hb[i]%mod;
IDFT(ha);
const int inv=pow(L,mod-2);
C.resize(size());
for(rg unsigned int i=0;i<size();i++)C[i]=(ll)ha[i]*inv%mod;
return C;
}
/* inline poly inv()const
{
poly C;
if(A.size()==1){C=*this;C[0]=pow(C[0],mod-2);return C;}
C.resize((A.size()+1)>>1);
for(rg unsigned int i=0;i<C.size();i++)C[i]=A[i];
C=C.inv();
poly D=(poly)2-*this*C;
D.resize(size());
D=D*C;
D.resize(size());
return D;
}*///大常数版本
inline void Reverse(const int n)
{
A.resize(n);
for(rg int i=0,j=n-1;i<j;i++,j--)swap(A[i],A[j]);
}
inline poly operator /(const poly B)const
{
if(size()<B.size())return 0;
poly a=*this,b=B;a.Reverse(size()),b.Reverse(B.size());
b.resize(size()-B.size()+1);
b=b.inv();
b=b*a;
b.Reverse(size()-B.size()+1);
return b;
}
inline poly operator %(const poly B)const
{
poly C=(*this)-(*this)/B*B;C.resize(B.size()-1);
return C;
}
inline poly diff()const
{
poly C;C.resize(size()-1);
for(rg unsigned int i=1;i<size();i++)C[i-1]=(ll)A[i]*i%mod;
return C;
}
inline poly inte()const
{
poly C;C.resize(size()+1);
for(rg unsigned int i=0;i<size();i++)C[i+1]=(ll)A[i]*Inv[i+1]%mod;
C[0]=0;
return C;
}
inline poly ln()const
{
poly C=(diff()*inv()).inte();C.resize(size());
return C;
}
inline poly exp()const
{
poly C;
if(size()==1){C=*this;C[0]=1;return C;}
C.resize((size()+1)>>1);
for(rg unsigned int i=0;i<C.size();i++)C[i]=A[i];
C=C.exp();C.resize(size());
poly D=(poly)1-C.ln()+*this;
D=D*C;
D.resize(size());
return D;
}
inline poly sqrt()const
{
poly C;
if(size()==1)
{
C=*this;C[0]=Quadratic_residue(C[0]);
return C;
}
C.resize((size()+1)>>1);
for(rg unsigned int i=0;i<C.size();i++)C[i]=A[i];
C=C.sqrt();C.resize(size());
C=(C+*this*C.inv())*(int)499122177;
C.resize(size());
return C;
}
inline poly operator >>(const unsigned int x)const
{
poly C;if(size()<x){C.resize(0);return C;}
C.resize(size()-x);
for(rg unsigned int i=0;i<C.size();i++)C[i]=A[i+x];
return C;
}
inline poly operator <<(const unsigned int x)const
{
poly C;C.RE(size()+x);
for(rg unsigned int i=0;i<size();i++)C[i+x]=A[i];
return C;
}
inline poly Pow(const unsigned int x)const
{
for(rg unsigned int i=0;i<size();i++)
if(A[i])
{
poly C=((((*this/A[i])>>i).ln()*x).exp()*pow(A[i],x))<<(min(i*x,size()));
C.resize(size());
return C;
}
return *this;
}
inline void cheng(const poly&B)
{
for(rg unsigned int i=0;i<size();i++)A[i]=(ll)A[i]*B[i]%mod;
}
inline void jia(const poly&B)
{
for(rg unsigned int i=0;i<size();i++)A[i]=Md(A[i]+B[i]);
}
inline void dft()
{
memset(ha,0,L<<2);
for(rg unsigned int i=0;i<A.size();i++)ha[i]=A[i];
DFT(ha);
resize(L);
for(rg int i=0;i<L;i++)A[i]=ha[i];
}
inline void idft()
{
memset(ha,0,L<<2);
for(rg unsigned int i=0;i<A.size();i++)ha[i]=A[i];
IDFT(ha);
const int inv=pow(L,mod-2);
for(rg int i=0;i<L;i++)A[i]=(ll)ha[i]*inv%mod;
while(size()&&!A[size()-1])A.pop_back();
}
};
void fz(const int root,const int l,const int r,std::vector<int>&v,std::vector<poly>&A)
{
if(l==r)
{
A[root].resize(2);
A[root][0]=(mod-v[l])%mod;
A[root][1]=1;
return;
}
const int mid=(l+r)>>1;
fz(root<<1,l,mid,v,A),fz(root<<1|1,mid+1,r,v,A);
A[root]=A[root<<1]*A[root<<1|1];
}
void calc(const int root,const int l,const int r,std::vector<int>&v,std::vector<poly>&A,std::vector<poly>&B)
{
if(l==r)
{
v[l]=B[root][0];
return;
}
const int mid=(l+r)>>1;
B[root<<1]=B[root]%A[root<<1];
B[root<<1|1]=B[root]%A[root<<1|1];
calc(root<<1,l,mid,v,A,B),calc(root<<1|1,mid+1,r,v,A,B);
}
void multi_point_evaluation(const poly a,std::vector<int>&v)
{
std::vector<poly>A,B;A.resize(maxn),B.resize(maxn);
fz(1,0,v.size()-1,v,A);
B[1]=a%A[1];
calc(1,0,v.size()-1,v,A,B);
}
void fz2(const int root,const int l,const int r,std::vector<int>&y,std::vector<poly>&A,std::vector<poly>&B)
{
if(l==r)
{
B[root].resize(1),B[root][0]=y[l];
return;
}
const int mid=(l+r)>>1;
fz2(root<<1,l,mid,y,A,B),fz2(root<<1|1,mid+1,r,y,A,B);
B[root]=B[root<<1]*A[root<<1|1]+B[root<<1|1]*A[root<<1];
}
poly interpolation(std::vector<int>&x,std::vector<int>&y)
{
std::vector<poly>A,B;A.resize(maxn),B.resize(maxn);
fz(1,0,x.size()-1,x,A);
multi_point_evaluation(A[1].diff(),x);
for(rg unsigned int i=0;i<x.size();i++)y[i]=(ll)y[i]*pow(x[i],mod-2)%mod;
fz2(1,0,x.size()-1,y,A,B);
return B[1];
}
}///namespace of Poly
Poly::poly a;
int n,m;
int main()
{
Poly::init(maxn);///namespace of Poly
read(n),n++,read(m);
a.readin(n);
a=(((Poly::poly){2-a[0]}+a-a.sqrt().inv().inte().exp()).ln()+(Poly::poly)1).Pow(m).diff();
a.resize(n-1);
a.putout();
return flush(),0;
}
update by 2019.2.24: 优化了NTT函数,减小了一半的常数
总结
要注意求导对什么求,否则会推错
很多算法基于牛顿迭代
对于求导积分会损失常数项,所以要单独求
自己写一遍博客才知道很多式子的细节所在
还被迫学了二次剩余