zoukankan      html  css  js  c++  java
  • FFT板子

    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const double Pi = acos(-1.0);
    int n,m,rev[2100005];
    
    struct complex{
    	double x,y;
    	inline complex operator + (const complex &a) const{return complex{x + a.x,y + a.y};}
    	inline complex operator - (const complex &a) const{return complex{x - a.x,y - a.y};}
    	inline complex operator * (const complex &a) const{return complex{x * a.x - y * a.y,x * a.y + y * a.x};}
    }a[2100005],b[2100005];
    void FFT(complex *a,int lent,int inv)
    {
    	if (lent == 1) return;
    	for (int i = 0; i < lent; ++i)
    		if (i < rev[i]) swap(a[i],a[rev[i]]);
    	for (int l = 1; l < lent; l <<= 1)
    	{
    		complex I = complex{cos(Pi / l),inv * sin(Pi / l)};
    		for (int i = 0; i < lent; i += (l << 1))
    		{
    			complex w = complex{1,0};
    			for (int j = 0; j < l; ++j,w = w * I)
    			{
    				complex x = a[i + j],y = w * a[i + j + l];
    				a[i + j] = x + y,a[i + j + l] = x - y;
    			}
    		}
    	}
    }
    int main()
    {
    	scanf("%d%d",&n,&m);
    	for (int i = 0; i <= n; ++i) scanf("%lf",&a[i].x);
    	for (int i = 0; i <= m; ++i) scanf("%lf",&b[i].x);
    	int len = 1,bit = 0;
    	while (len <= n + m) len <<= 1,++bit;
    	for (int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));
    	FFT(a,len,1),FFT(b,len,1);
    	for (int i = 0; i < len; ++i) a[i] = a[i] * b[i];
    	FFT(a,len,-1);
    	for (int i = 0; i <= n + m; ++i) printf("%d ",(int)(a[i].x / len + 0.5));
    }
    
  • 相关阅读:
    html <applet>元素属性介绍
    C#内存释放(转)
    mongodb for linux (安装)
    WCF客户端搭建(通过自定义WCF Client封装) wu
    SQL常用语句 wu
    任务调度平台 wu
    将DataSet 纵向显示数据
    关于OP和SI项目的记录点,防止遗忘难以查找
    java进阶
    git代码量统计
  • 原文地址:https://www.cnblogs.com/nibabadeboke/p/15169926.html
Copyright © 2011-2022 走看看