http://uoj.ac/problem/34 (题目链接)
题意
求两个多项式的乘积
Solution
挂个FFT板子。当然,是hzwer的板子→_→
细节
FFT因为要满足n是2的幂,所以注意数组大小。
代码
// uoj34 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<complex> #include<cstdio> #include<cmath> #define LL long long #define inf 2147483640 #define Pi acos(-1.0) #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); using namespace std; typedef complex<double> E; const int maxn=300010; E a[maxn],b[maxn]; int n,m; namespace FFT { int rev[maxn],L; void FFT(E *a,int f) { for (int i=0;i<n;i++) if (i<rev[i]) swap(a[i],a[rev[i]]); for (int i=1;i<n;i<<=1) { E wn(cos(Pi/i),f*sin(Pi/i)); for (int p=i<<1,j=0;j<n;j+=p) { E w(1,0); for (int k=0;k<i;k++,w*=wn) { E x=a[j+k],y=w*a[j+k+i]; a[j+k]=x+y;a[j+k+i]=x-y; } } } } void Init() { m=n+m; for (n=1;n<=m;n<<=1) L++; for (int i=0;i<n;i++) rev[i]=(rev[i>>1]>>1) | ((i&1)<<(L-1)); FFT(a,1);FFT(b,1); for (int i=0;i<=n;i++) a[i]=a[i]*b[i]; FFT(a,-1); } } int main() { scanf("%d%d",&n,&m); for (int i=0,x;i<=n;i++) scanf("%d",&x),a[i]=x; for (int i=0,x;i<=m;i++) scanf("%d",&x),b[i]=x; FFT::Init(); for (int i=0;i<=m;i++) printf("%d ",(int)(a[i].real()/n+0.5)); return 0; }
时间: 2024-11-05 14:58:37