[xsy2978]Product of Roots
$\newcommand{align}[1]{\begin{align*}#1\end{align*}}$题意:给出$f(x)=\prod\limits_{i=1}^n(a_ix+1)$和$g(x)=\prod\limits_{j=1}^m(b_jx+1)$的各项系数,求$h(x)=\prod\limits_{i=1}^n\prod\limits_{j=1}^m(a_ib_jx+1)$的前$k$项系数
乘积的形式不好处理,取个$\ln$就可以化为和的形式
$\align{\ln(a_ix+1)=\int\frac{a_i}{a_ix+1}\mathrm dx=\sum\limits_{k\geq1}\dfrac{(-1)^{k+1}}ka_i^kx^k}$
$\align{\ln(b_jx+1)=\sum\limits_{k\geq1}\dfrac{(-1)^{k+1}}kb_j^kx^k}$
$\align{\ln(a_ib_jx+1)=\sum\limits_{k\geq1}\dfrac{(-1)^{k+1}}ka_i^kb_j^kx^k}$
容易发现我们把前两个式子的系数做点积,再对$\forall k\geq1$把第$k$位乘上$(-1)^{-k+1}k$就得到了第三个式子的系数
所以我们把$\ln f(x)$和$\ln g(x)$的系数如此处理后就得到了$\ln h(x)$的系数,$\exp$回去即可
#include<stdio.h> #include<string.h> typedef long long ll; const int mod=998244353,maxn=262144; void swap(int&a,int&b){a^=b^=a^=b;} int mul(int a,int b){return a*(ll)b%mod;} int ad(int a,int b){return(a+b)%mod;} int de(int a,int b){return(a-b)%mod;} int pow(int a,int b){ int s=1; while(b){ if(b&1)s=mul(s,a); a=mul(a,a); b>>=1; } return s; } int rev[maxn],N,iN; void pre(int n){ int i,k; for(N=1,k=0;N<n;N<<=1)k++; for(i=0;i<N;i++)rev[i]=(rev[i>>1]>>1)|((i&1)<<(k-1)); iN=pow(N,mod-2); } void ntt(int*a,int on){ int i,j,k,t,w,wn; for(i=0;i<N;i++){ if(i<rev[i])swap(a[i],a[rev[i]]); } for(i=2;i<=N;i<<=1){ wn=pow(3,on==1?(mod-1)/i:(mod-1-(mod-1)/i)); for(j=0;j<N;j+=i){ w=1; for(k=0;k<i>>1;k++){ t=mul(w,a[i/2+j+k]); a[i/2+j+k]=de(a[j+k],t); a[j+k]=ad(a[j+k],t); w=mul(w,wn); } } } if(on==-1){ for(i=0;i<N;i++)a[i]=mul(a[i],iN); } } int t0[maxn]; void getinv(int*a,int*b,int n){ if(n==1){ b[0]=pow(a[0],mod-2); return; } int i; getinv(a,b,n>>1); pre(n<<1); memset(t0,0,N<<2); memcpy(t0,a,n<<2); ntt(t0,1); ntt(b,1); for(i=0;i<N;i++)b[i]=mul(b[i],2-mul(b[i],t0[i])); ntt(b,-1); for(i=n;i<N;i++)b[i]=0; } int t1[maxn],inv[maxn]; void getln(int*a,int*b,int n){ int i; memset(t1,0,n<<3); getinv(a,t1,n); for(i=1;i<n;i++)b[i-1]=mul(i,a[i]); ntt(b,1); ntt(t1,1); for(i=0;i<N;i++)b[i]=mul(b[i],t1[i]); ntt(b,-1); for(i=n-1;i>0;i--)b[i]=mul(b[i-1],inv[i]); b[0]=0; for(i=n;i<N;i++)b[i]=0; } int t2[maxn]; void exp(int*a,int*b,int n){ if(n==1){ b[0]=1; return; } int i; exp(a,b,n>>1); memset(t2,0,n<<3); getln(b,t2,n); for(i=0;i<n;i++)t2[i]=de(a[i],t2[i]); t2[0]++; ntt(b,1); ntt(t2,1); for(i=0;i<N;i++)b[i]=mul(b[i],t2[i]); ntt(b,-1); for(i=n;i<N;i++)b[i]=0; } int f[maxn],g[maxn],lf[maxn],lg[maxn],lh[maxn],h[maxn]; int main(){ int N,n,m,k,i; scanf("%d%d%d",&n,&m,&k); for(i=0;i<=n;i++)scanf("%d",f+i); for(i=0;i<=m;i++)scanf("%d",g+i); for(N=1;N<n+1||N<m+1||N<k+1;N<<=1); inv[1]=1; for(i=2;i<N;i++)inv[i]=-mul(mod/i,inv[mod%i]); getln(f,lf,N); getln(g,lg,N); for(i=0;i<N;i++)lh[i]=mul(mul(lf[i],lg[i]),i&1?i:-i); exp(lh,h,N); for(i=0;i<k;i++)printf("%d ",ad(h[i],mod)); }
[xsy2978]Product of Roots的更多相关文章
- 【xsy2978】Product of Roots 生成函数+多项式ln+多项式exp
题目大意:给你两个多项式$f(x)$和$g(x)$,满足$f(x)=\prod\limits_{i=1}^{n}(a_i+1)$,$g(x)=\prod\limits_{i=1}^{m}(b_i+1) ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Peterhof
A. City Wall 找规律. #include<stdio.h> #include<iostream> #include<string.h> #include ...
- gc roots 垃圾回收
gc roots包括以下几个: 虚拟机栈(栈桢中的本地变量表)中的引用对象 方法区中的类静态属性引用的对象 方法区中的常量引用的对象 本地方法栈中JNI(即native方法)的引用的对象 java,c ...
- uva 11059 maximum product(水题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK
- [LeetCode] Product of Array Except Self 除本身之外的数组之积
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- vector - vector product
the inner product Givens two vectors \(x,y\in \mathbb{R}^n\), the quantity \(x^\top y\), sometimes c ...
- 1 Maximum Product Subarray_Leetcode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- [ST表/贪心] NOI2010 超级钢琴
[NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i ...
- java常用的时间格式
年月日时分秒毫秒:yyyyMMddHHmmssSSS 毫秒用SSS表示.
- eclipse关闭错误警告提示
- C++开源库,欢迎补充。
转载自:http://blog.csdn.net/kobejayandy/article/details/8681741 C++在"商业应用"方面,曾经是天下第一的开发语言,但这一 ...
- Spring学习--引用其他Bean , 内部Bean
引用其他Bean: 组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能 , 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用. 在 Bean 的配置文 ...
- es6+最佳入门实践(11)
11.async函数 async 函数是什么?一句话,它就是 Generator 函数的语法糖.通俗的说就是Generator函数的另一种写法,这种写法更简洁,除此之外,async函数还对Genrat ...
- [洛谷P3942] 将军令
洛谷题目链接:将军令 题目背景 历史/落在/赢家/之手 至少/我们/拥有/传说 谁说/败者/无法/不朽 拳头/只能/让人/低头 念头/却能/让人/抬头 抬头/去看/去爱/去追 你心中的梦 题目描述 又 ...
- eclipse怎样快速的给代码段添加try catch
打开要进行异常处理的java代码页面. 选中要添加try..catch的代码段,然后点击鼠标右键,选择[Sourround With]选项. 然后选择[Try/Catch Block]或者[6 try ...
- [bzoj3132]上帝造题的七分钟——二维树状数组
题目大意 你需要实现一种数据结构,支援以下操作. 给一个矩阵的子矩阵的所有元素同时加一个数. 计算子矩阵和. 题解 一看这个题,我就首先想到用线段树套线段树做. 使用二维线段树的错误解法 其实是第一次 ...
- thinkphp 随机获取一条数据
$data=$AD->field("ID,Answer,State")->limit(1)->order('rand()')->select();