标题
增加 1 行删除 1 行
1浅谈多项式&生成函数科技1浅谈多项式 & 生成函数科技(一)
正文
增加 180 行删除 4 行
4 行未改动5678910111213130 行未改动全部展开144145146147148149150151152757 行未改动全部展开9109119129139149159169179189199209219229239249259264 行未改动**前排广告**:- [从斐波那契通项公式到卡特兰数通项公式](https://www.luogu.com.cn/article/fwir6z7m)- [莫比乌斯反演学习笔记](https://www.luogu.com.cn/article/qiz6mq20)- [基于 A* 思想的 SPFA 优化-SPFA*](https://www.luogu.com.cn/article/mcxa9lph)(已被 hack)# 引子#### [P10780 BZOJ3028 食物](https://www.luogu.com.cn/problem/P10780)130 行未改动全部展开由于 $G$ 是好求的,所以直接上多项式 $\ln$ 模板。但是大部分人应该不会多项式全家桶,所以知道做法即可,马上会讲到这些东西。# 章三·多项式全家桶提示:1. 这部分篇幅可能稍长,如果你会多项式全家桶了,可以快进到后面。2. 这里仅给出模数为 $998244353$ 的 NTT 全家桶。757 行未改动全部展开return 0;}```## 多项式除法 & 取模式子长成这个模样:$F=G*Q+R$。若 $R=0$ 的话,那直接做一个多项式乘法逆就完成了。但这个情况并不普遍。更多情况下还是会出现余多项式的。# 参考资料- [指数型生成函数 & 下降幂 学习笔记](https://www.luogu.com/article/p7qti8te)——by @[C_liar](https://www.luogu.com.cn/user/663681);- [快速傅里叶变换(FFT)学习笔记](https://www.luogu.com.cn/article/ne7zgqp6)——by @[KobeBeanBryantCox](https://www.luogu.com.cn/user/865625);- [多项式初等函数](https://oi.wiki/math/poly/elementary-func)——by [OI Wiki](https://oi.wiki);- 《算法导论·第三版》——by 机械工业出版社;- [题解 P3803 【【模板】多项式乘法(FFT)】](https://www.luogu.com.cn/article/pvwh9hpp)——by @[NaCly_Fish](https://www.luogu.com.cn/user/115864)- [[可能有用科技]多项式任意次幂&任意次根](https://www.luogu.com/article/00rovx59)——@[望月Asta](https://www.luogu.com.cn/user/122520)4 行未改动5678910111213130 行未改动全部展开144145146147148149150151152757 行未改动全部展开91091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111024 行未改动**前排广告**:- [从斐波那契通项公式到卡特兰数通项公式](https://www.luogu.com.cn/article/fwir6z7m)- [莫比乌斯反演学习笔记](https://www.luogu.com.cn/article/qiz6mq20)- ~~[基于 A* 思想的 SPFA 优化-SPFA*](https://www.luogu.com.cn/article/mcxa9lph)~~(已被 hack)(怎么被当成题解误杀了)# 引子#### [P10780 BZOJ3028 食物](https://www.luogu.com.cn/problem/P10780)130 行未改动全部展开由于 $G$ 是好求的,所以直接上多项式 $\ln$ 模板。但是大部分人应该不会多项式全家桶,所以知道做法即可,马上会讲到这些东西。# 章三·多项式并不全家桶提示:1. 这部分篇幅可能稍长,如果你会多项式全家桶了,可以快进到后面。2. 这里仅给出模数为 $998244353$ 的 NTT 全家桶。757 行未改动全部展开return 0;}```## 六、多项式除法 & 取模式子长成这个模样:$F=G*Q+R$。若 $R=0$ 的话,那直接做一个多项式乘法逆就完成了。但这个情况并不普遍。更多情况下还是会出现余多项式的。我们构造一个变换 $F^R$,使得对于多项式 $H$,若它的阶为 $h$,则 $H^R=x^hH(\frac{1}{x})$。显然,这个变换就是将系数进行翻转。然后,我们将原来式子中的 $x$ 全部替换为 $\frac{1}{x}$,然后两边同乘以 $x^n$,可得:$$\begin{aligned}x^nF(\frac{1}{x})&=(x^mG(\frac{1}{x}))*(x^{n-m}Q(\frac{1}{x}))+x^{n-m+1}x^{m-1}R(\frac{1}{x})\\\Longrightarrow F^R&=G^R*Q^R+x^{n-m+1}R^R\end{aligned}$$然后两边都丢到 $\pmod{x^{n-m+1}}$ 意义下,$R^R$ 的影响就被消除了,然后直接多项式求逆就好了。余多项式就是在做一个减法的事。#### 模板```cpp/*我们所可以自慰的,想来想去,也还是所谓对于将来的希望。希望是附丽于存在的,有存在,便有希望,有希望,便是光明。*/#include<bits/stdc++.h>using namespace std;constexpr int N=5e5+5,p=998244353;int rev[N],len=1,m,n,f[N],g[N],ff[N],gg[N],q[N],r[N],tmp[N],inv[N];inline int qpw(int a,int T){int res=1;for(;T;T>>=1,a=1ll*a*a%p) if(T&1) res=1ll*res*a%p;return res;}inline void init(int n){int b=__lg(n);for(int i=1;i<n;++i) rev[i]=(rev[i>>1]>>1)+((i&1)<<(b-1));return;}inline void NTT(int*const& a,int n,int type){const int g=(~type)? 3:332748118;for(int i=0;i<n;++i) if(i<rev[i]) swap(a[i],a[rev[i]]);for(int len=2,wn,l,a0,a1;len<=n;len<<=1){l=len>>1,wn=qpw(g,(p-1)/len);for(int i=0;i<n;i+=len){for(int j=0,w=1;j<l;++j,w=1ll*w*wn%p){a0=a[i+j],a1=1ll*w*a[i+j+l]%p;a[i+j]=(0ll+a0+a1)%p,a[i+j+l]=((a0-a1)%p+p)%p;}}}if(!~type) for(int i=0,invn=qpw(n,p-2);i<n;++i) a[i]=1ll*a[i]*invn%p;return;}signed main(){cin.tie(nullptr)->sync_with_stdio(false);cin>>n>>m;for(int i=0;i<=n;++i) cin>>f[i],ff[i]=f[i];for(int i=0;i<=m;++i) cin>>g[i],gg[i]=g[i];reverse(f,f+n+1),reverse(g,g+m+1);for(len=1;len<=n-m;len<<=1);for(int i=n-m+1;i<=n;++i) f[i]=0;for(int i=n-m+1;i<=m;++i) g[i]=0;inv[0]=qpw(g[0],p-2);for(int t=2,k;t<=len;t<<=1){k=t<<1;init(k);copy(g,g+t,tmp),fill(tmp+t,tmp+k,0);NTT(inv,k,1),NTT(tmp,k,1);for(int i=0;i<k;++i) inv[i]=1ll*inv[i]*((2-1ll*inv[i]*tmp[i]%p)%p+p)%p;NTT(inv,k,-1);fill(inv+t,inv+k,0);}for(len=1;len<=(n-m+1)<<1;len<<=1);init(len);NTT(f,len,1),NTT(inv,len,1);for(int i=0;i<len;++i) q[i]=1ll*f[i]*inv[i]%p;NTT(q,len,-1),reverse(q,q+n-m+1);for(int i=0;i<=n-m;++i) cout<<q[i]<<' ';cout<<'\n';for(len=1;len<=n+1;len<<=1);for(int i=n-m+1;i<len;++i) q[i]=0;init(len),NTT(gg,len,1),NTT(q,len,1);for(int i=0;i<len;++i) gg[i]=1ll*q[i]*gg[i]%p;NTT(gg,len,-1);for(int i=0;i<m;++i) r[i]=((ff[i]-gg[i])%p+p)%p;for(int i=0;i<m;++i) cout<<r[i]<<' ';return 0;}```## 七、多项式开根 & 幂函数### 多项式幂函数显然,我们有 $O(n\log n\log k)$ 的~~优秀~~做法。然而,这道题可以做到更优。$$\begin{aligned}G&\equiv F^k\\f_0^{-k}G&\equiv(f_0^{-1}F)^{k}\end{aligned}$$两边取 $\ln$ 得$$\begin{aligned}\ln (f_0^{-k}G)\equiv k\ln(f_0^{-1}F)\end{aligned}$$再 $\exp$ 回去,可得$$\begin{aligned}f_0^{-k}G&\equiv\exp(k\ln(f_0^{-1}F))\\G&\equiv f_0^{k}\exp(k\ln(f_0^{-1}F))\end{aligned}$$但是我们忽略了一件事:$[x^0]F$ 可能为 $0$。此时我们只要将前面为 $0$ 的项先“删掉”,最后在补回来就好了。### 多项式开根直接跳过二次根,讨论开 $k$ 次根。$$G\equiv F^{\frac{1}{k}}$$可以和上面一样,直接两边求 $\ln$,然后再 $\exp$ 回去,但是我们没保证 $[x^0]F=1$。那还是和上面一样,两边都乘上 $[x^0]F$ 的逆。$$\begin{aligned}f_0^{-\frac{1}{k}}G&\equiv (f_0^{-1}F)^{\frac{1}{k}}\\\ln(f_0^{-\frac{1}{k}}G)&\equiv k^{-1}\ln(f_0^{-1}F)\\G&\equiv f_0^{\frac{1}{k}}\exp(k^{-1}\ln(f_0^{-1}F))\end{aligned}$$最难的就是要求一个 $f_0$ 的 $k$ 次剩余。## 八、多项式三角函数~~我恨三角函数!我恨三角恒等变换!我恨解三角形!~~话虽这么说,但是多项式三角函数还是比较优美的。首先我们可以将 $\sin$ 与 $\cos$ 的写另一种形式出来。$$\begin{aligned}\sin x&=\frac{e^{\text{i}x}-e^{-\text{i}x}}{2\text{i}}\\\cos x&=\frac{e^{\text{i}x}+e^{-\text{i}x}}{2}\\\end{aligned}$$然后将 $F$ 带入,可得。$$\begin{aligned}\sin F&=\frac{\exp(\text{i}F)-\exp(-\text{i}F)}{2\text{i}}\\\cos F &=\frac{\exp(\text{i}F)+\exp(-\text{i}F)}{2}\\\end{aligned}$$而 $\tan F$,可以由 $\dfrac{\sin F}{\cos F}$ 求出。## 九、多项式反三角函数一开始看,觉得没有什么办法。此时上万能的微积分就好了。先对其求导,在积分回去。$$\begin{aligned}\arcsin'x&=\dfrac{1}{\sqrt{1-x^2}}\\\arcsin x&=\int\dfrac{1}{\sqrt{1-x^2}}\text{d}x\\\arccos'x&=-\dfrac{1}{\sqrt{1-x^2}}\\\arccos x&=-\int\dfrac{1}{\sqrt{1-x^2}}\text{d}x\\\arctan'x&=\dfrac{1}{1+x^2}\\\arctan x&=\int\dfrac{1}{1+x^2}\text{d}x\\\end{aligned}$$然后将 $F$ 代进去得。$$\begin{aligned}\arcsin'F&=\dfrac{F'}{\sqrt{1-F^2}}\\\arcsin F&=\int\dfrac{F'}{\sqrt{1-F^2}}\text{d}x\\\arccos'F&=-\dfrac{F'}{\sqrt{1-F^2}}\\\arccos F&=-\int\dfrac{F'}{\sqrt{1-F^2}}\text{d}x\\\arctan'F&=\dfrac{F'}{1+F^2}\\\arctan F&=\int\dfrac{F'}{1+F^2}\text{d}x\\\end{aligned}$$# 章四·后话就先写到这里吧,已经带不动专栏了,而且也累了。在 NOIP 之后,要是没退役,会更新《浅谈多项式 & 生成函数科技(二)》,会将部分没给出的模板题代码、类包装的全家桶、应用等坑给填完,敬清期待。# 参考资料- [指数型生成函数 & 下降幂 学习笔记](https://www.luogu.com/article/p7qti8te)——by @[C_liar](https://www.luogu.com.cn/user/663681);- [快速傅里叶变换(FFT)学习笔记](https://www.luogu.com.cn/article/ne7zgqp6)——by @[KobeBeanBryantCox](https://www.luogu.com.cn/user/865625);- [多项式初等函数](https://oi.wiki/math/poly/elementary-func)——by [OI Wiki](https://oi.wiki);- 《算法导论·第三版》——by 机械工业出版社;- [题解 P3803 【【模板】多项式乘法(FFT)】](https://www.luogu.com.cn/article/pvwh9hpp)——by @[NaCly_Fish](https://www.luogu.com.cn/user/115864)- [[可能有用科技]多项式任意次幂&任意次根](https://www.luogu.com/article/00rovx59)——by @[望月Asta](https://www.luogu.com.cn/user/122520)