luogu P4726 多项式指数函数(模板题FFT、多项式求逆、多项式对数函数)
手动博客搬家: 本文发表于20181127 08:39:42, 原地址https://blog.csdn.net/suncongbo/article/details/84559818
题目链接: https://www.luogu.org/problem/show?pid=4726
题意: 给定\(n\)次多项式\(A(x)\) 求多项式\(f(x)\)满足\(f(x)\equiv e^{A(x)} (\mod x^n)\)
题解
这个比对数函数复杂一些。。
前铺知识
泰勒展开
对于一个函数,我们可以用以下方式用它的高阶导数进行无穷逼近:
\]
牛顿迭代
已知要求的多项式\(f\)满足\(g(f(x))\equiv 0(\mod x^n)\) \(g\)已知,那么可以通过如下的方式倍增求解:
假设求出了原问题\(\mod x^n\)的答案\(f_0(x)\), 考虑转移到\(f(x) \mod x^{2n}\).
根据泰勒展开公式: $$0=g(f)=g(f_0)+g'(f_0)(f-f_0)+g''(f_0)\frac{(f-f_0)^2}{2!}+...$$
但是,由于一个显然的结论——\(f\equiv f_0(\mod x^n)\), 因此\((f-f_0)^2\equiv 0(\mod x^{2n})\), 因此公式里只需要计算前两项,后面的项都在\(\mod x^{2n}\)意义下为\(0\)!
即\(g(f_0)+g'(f_0)(f-f_0)=0\)
\(f=f_0-\frac{g(f_0)}{g'(f_0)}\)
如此即可求解。
本题题解
根据牛顿迭代的法则,令\(g(f)=\ln f-A\), 则\(f=f_0-\frac{\ln f_0-A}{\frac{1}{f_0}}=f_0(1-\ln f_0+A)\)
递归求解即可。
值得注意的是FFT的大小
\(A\)应该带入\(2n\)次, \(\ln f_0\)应该带入\(2n\)次, \(f_0\)应该带入\(n\)次, FFT乘法因为是\(2n\)次乘以\(n\)次,所以应该取到\(4n\)个单位根。
FFT这个地方太容易出错了!范围大了常数大好几倍,范围小了就会出错。
时间复杂度为\(T(n)=T(\frac{n}{2})+O(n\log n)\), \(T(n)=O(n\log n)\)
常数我算的应该是\(48\)倍。每次\(n\)到\(2n\)的迭代需要做一次\(2n\)的\(\ln\)和三次\(4n\)的DFT/IDFT. 因此\(18(2n\log n)+3(4n\log n)=48n\log n\).
(飞了……)
代码
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define llong long long
#define ldouble long double
#define uint unsigned int
#define ullong unsigned long long
#define udouble unsigned double
#define uldouble unsigned long double
#define modinc(x) {if(x>=P) x-=P;}
#define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define piiii pair<pair<int,int>,pair<int,int> >
#define pli pair<llong,int>
#define pll pair<llong,llong>
#define Memset(a,x) {memset(a,x,sizeof(a));}
using namespace std;
const int N = 1<<19;
const int LGN = 19;
const int G = 3;
const int P = 998244353;
llong tmp1[N+3],tmp2[N+3],tmp3[N+3],tmp4[N+3]; //inv
llong tmp5[N+3],tmp6[N+3],tmp7[N+3],tmp8[N+3]; //ln
llong tmp9[N+3],tmp10[N+3],tmp11[N+3],tmp12[N+3]; //exp
llong a[N+3],b[N+3];
int id[N+3];
int n;
llong quickpow(llong x,llong y)
{
llong cur = x,ret = 1ll;
for(int i=0; y; i++)
{
if(y&(1ll<<i))
{
ret = ret*cur%P;
y-=(1ll<<i);
}
cur = cur*cur%P;
}
return ret;
}
llong mulinv(llong x) {return quickpow(x,P-2);}
void initid(int dgr)
{
int len = 0;
for(int i=0; i<=LGN; i++) if(dgr==(1<<i)) {len = i; break;}
id[0] = 0;
for(int i=1; i<dgr; i++) id[i] = (id[i>>1]>>1)|((i&1)<<(len-1));
}
void ntt(int dgr,int coe,llong poly[],llong ret[])
{
initid(dgr);
for(int i=0; i<dgr; i++) ret[i] = poly[i];
for(int i=0; i<dgr; i++) if(i<id[i]) swap(ret[i],ret[id[i]]);
for(int i=1; i<=(dgr>>1); i<<=1)
{
llong tmp = quickpow(G,(P-1)/(i<<1));
if(coe==-1) tmp = mulinv(tmp);
for(int j=0; j<dgr; j+=(i<<1))
{
llong expn = 1ll;
for(int k=0; k<i; k++)
{
llong x = ret[j+k],y = ret[j+k+i]*expn%P;
ret[j+k] = x+y; modinc(ret[j+k]);
ret[j+i+k] = x-y+P; modinc(ret[j+i+k]);
expn = (expn*tmp)%P;
}
}
}
if(coe==-1)
{
llong tmp = mulinv(dgr);
for(int j=0; j<dgr; j++) ret[j] = ret[j]*tmp%P;
}
}
void polyinv(int dgr,llong poly[],llong ret[])
{
for(int i=0; i<dgr; i++) ret[i] = 0ll;
ret[0] = mulinv(poly[0]);
for(int i=1; i<=(dgr>>1); i<<=1)
{
for(int j=0; j<(i<<2); j++) tmp1[j] = j<i ? ret[j] : 0ll;
for(int j=0; j<(i<<2); j++) tmp2[j] = j<(i<<1) ? poly[j] : 0ll;
ntt((i<<2),1,tmp1,tmp3); ntt((i<<2),1,tmp2,tmp4);
for(int j=0; j<(i<<2); j++) tmp3[j] = tmp3[j]*tmp3[j]%P*tmp4[j]%P;
ntt((i<<2),-1,tmp3,tmp4);
for(int j=0; j<(i<<1); j++) ret[j] = (tmp1[j]+tmp1[j]-tmp4[j]+P)%P;
}
for(int i=dgr; i<(dgr<<1); i++) ret[i] = 0ll;
}
void polyder(int dgr,llong poly[],llong ret[])
{
for(int i=0; i<dgr-1; i++) ret[i] = poly[i+1]*(i+1)%P;
}
void polyint(int dgr,llong poly[],llong ret[])
{
for(int i=1; i<dgr; i++) ret[i] = poly[i-1]*mulinv(i)%P;
}
void polyln(int dgr,llong poly[],llong ret[])
{
for(int i=0; i<dgr; i++) ret[i] = 0ll;
polyder(dgr,poly,tmp5);
polyinv(dgr,poly,tmp6);
ntt((dgr<<1),1,tmp5,tmp7); ntt((dgr<<1),1,tmp6,tmp8);
for(int i=0; i<(dgr<<1); i++) tmp7[i] = tmp7[i]*tmp8[i]%P;
ntt((dgr<<1),-1,tmp7,tmp8);
polyint(dgr,tmp8,ret);
}
void polyexp(int dgr,llong poly[],llong ret[])
{
for(int i=0; i<dgr; i++) ret[i] = i==0;
for(int i=1; i<=(dgr>>1); i<<=1)
{
for(int j=0; j<(i<<2); j++) tmp9[j] = j>=(i<<1) ? 0ll : ((j==0)+poly[j])%P;
for(int j=0; j<(i<<2); j++) tmp10[j] = j>=i ? 0ll : ret[j];
polyln((i<<1),tmp10,tmp11);
for(int j=0; j<(i<<1); j++) {tmp9[j] = tmp9[j]-tmp11[j]+P; modinc(tmp9[j]);}
ntt((i<<2),1,tmp10,tmp12); ntt((i<<2),1,tmp9,tmp11);
for(int j=0; j<(i<<2); j++) tmp12[j] = tmp12[j]*tmp11[j]%P;
ntt((i<<2),-1,tmp12,tmp11);
for(int j=0; j<(i<<1); j++) ret[j] = tmp11[j];
}
}
int main()
{
scanf("%d",&n); int dgr = 1; while(dgr<=n) dgr<<=1;
for(int i=0; i<n; i++) scanf("%lld",&a[i]);
polyexp(dgr,a,b);
for(int i=0; i<n; i++) printf("%lld ",b[i]);
return 0;
}
luogu P4726 多项式指数函数(模板题FFT、多项式求逆、多项式对数函数)的更多相关文章
- FFT模板 生成函数 原根 多项式求逆 多项式开根
FFT #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> ...
- 【BZOJ3456】轩辕朗的城市规划 无向连通图计数 CDQ分治 FFT 多项式求逆 多项式ln
题解 分治FFT 设\(f_i\)为\(i\)个点组成的无向图个数,\(g_i\)为\(i\)个点组成的无向连通图个数 经过简单的推导(枚举\(1\)所在的连通块大小),有: \[ f_i=2^{\f ...
- bzoj 3456 城市规划——分治FFT / 多项式求逆 / 多项式求ln
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3456 分治FFT: 设 dp[ i ] 表示 i 个点时连通的方案数. 考虑算补集:连通的方 ...
- NTT+多项式求逆+多项式开方(BZOJ3625)
定义多项式$h(x)$的每一项系数$h_i$,为i在c[1]~c[n]中的出现次数. 定义多项式$f(x)$的每一项系数$f_i$,为权值为i的方案数. 通过简单的分析我们可以发现:$f(x)=\fr ...
- 2019.01.01 bzoj3625:小朋友和二叉树(生成函数+多项式求逆+多项式开方)
传送门 codeforces传送门codeforces传送门codeforces传送门 生成函数好题. 卡场差评至今未过 题意简述:nnn个点的二叉树,每个点的权值KaTeX parse error: ...
- [Codeforces438E][bzoj3625] 小朋友和二叉树 [多项式求逆+多项式开根]
题面 传送门 思路 首先,我们把这个输入的点的生成函数搞出来: $C=\sum_{i=0}^{lim}s_ix^i$ 其中$lim$为集合里面出现过的最大的数,$s_i$表示大小为$i$的数是否出现过 ...
- P6295-有标号 DAG 计数【多项式求逆,多项式ln】
正题 题目链接:https://www.luogu.com.cn/problem/P6295 题目大意 求所有\(n\)个点的弱联通\(DAG\)数量. \(1\leq n\leq 10^5\) 解题 ...
- UVA 796 Critical Links(模板题)(无向图求桥)
<题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...
- UVA 315 Network (模板题)(无向图求割点)
<题目链接> 题目大意: 给出一个无向图,求出其中的割点数量. 解题分析: 无向图求割点模板题. 一个顶点u是割点,当且仅当满足 (1) u为树根,且u有多于一个子树. (2) u不为树根 ...
随机推荐
- Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)
接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...
- extjs_07_combobox&tree&chart
1.combobox <%@ page language="java" import="java.util.*" pageEncoding="U ...
- QT 4.53 for VS2005 编译包
最近学QT,装了QT5,结果打开下载的项目(特别是那些书的源代码)有很多兼容性问题.于是又下载4.83,都是4版本系列的,应该会好很多吧,但居然也是这样.于是看了一下时间,觉得4.53于2009年底发 ...
- wox 快速搜索程序
windows启动栏的搜索,会经常找不到exe. 使用wox可以非常快速的找到启动程序 https://github.com/Wox-launcher/Wox/ 安装完成后,默认alt+space出现 ...
- bzoj4241 历史研究——分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4241 就是分块,预处理出从第 i 块到 j 位置的答案,以及从第 i 块到最后位置间每个数出 ...
- EOJ 1641/UVa The SetStack Computer
Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...
- E20170826-hm
squash vt. 挤进; 将(某人[某物])压扁; 使沉默; 平定(叛乱等); meld vt. (使) 融合,合并,结合; n. 混合,合并; amend vt. 修订; 改良,修改; a ...
- Spell checker(串)
http://poj.org/problem?id=1035 题意:给定一个单词判断其是否在字典中,若存在输出"%s is correct",否则判断该单词删掉一个字母,或增加一个 ...
- ASP.NET的Eval方法和Bind方法的区别
Eval是只读的方法(单向数据在邦定),所邦定的内容为不会提交回服务器. 比如图书的ISBN,并不想让用户做任何修改,可以使用<%# Eval('ISBN').TOString().Trim() ...
- C#微信公众号的开发——服务配置
最近因为需要用C#开发微信公众号的一些功能,记录一下开发公众号的一些坑..... 首先先介绍一下,微信公众号的官方文档.虽然这个文档我感觉比较糙,但是还是可以借鉴一下让我们摸着石头过河的. 首先我们得 ...