【bzoj2179】FFT快速傅立叶 FFT模板
2016-06-01 09:34:54
很久很久很久以前写的了。。。
今天又比较了一下效率,貌似手写复数要快很多。
贴一下模板:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<complex>
#define ll long long
#define N 500020
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct CD{
double a,b;
CD(double x=,double y=){a=x;b=y;}
friend CD operator +(CD n1,CD n2){return CD(n1.a+n2.a,n1.b+n2.b);}
friend CD operator -(CD n1,CD n2){return CD(n1.a-n2.a,n1.b-n2.b);}
friend CD operator *(CD n1,CD n2){return CD(n1.a*n2.a-n1.b*n2.b,n1.a*n2.b+n1.b*n2.a);}
};
int n,m,bit=;
const long double Pi=acos(-1.0); void FFT(CD *a,int n,int type){
for(int i=,j=;i<n;i++) {
if(j>i)swap(a[i],a[j]);
int k=n;
while(j&(k >>= ))j&=~k;
j|=k;
}
for(int i=;i<=bit;i++){
CD w_n(cos(*type*Pi/(<<i)),sin(*type*Pi/(<<i)));
for(int j=;j<(<<bit);j+=(<<i)){
CD w(,);
for(int k=j;k<j+(<<(i-));k++){
CD tmp=a[k],tt=w*a[k+(<<(i-))];
a[k]=a[k]+tt;
a[k+(<<(i-))]=tmp-tt;
w=w*w_n;
}
}
}
if(type<) for(int i=;i<(<<bit);i++) a[i].a=(a[i].a+0.5)/(<<bit);
} CD poly1[N],poly2[N];
int c[N*]; char ch[N];
int main (){
n=read();
scanf("%s",ch+);
for(int i=;i<n;i++)poly1[i]=(double)(ch[n-i]-'');
scanf("%s",ch+);
for(int i=;i<n;i++)poly2[i]=(double)(ch[n-i]-'');
bit=;
while(<<bit<(n<<))bit++;
n=<<bit;
FFT(poly1,n,);FFT(poly2,n,);
for(int i=;i<n;i++)poly1[i]=poly1[i]*poly2[i];
FFT(poly1,n,-);
int jin=,top=;
for(int i=;i<n;i++){
jin+=(int)(poly1[i].a+0.5);
c[++top]=jin%;
jin/=;
}
while(top&&c[top]==) top--;
while(top)putchar(c[top--]+'');
return ;
}
2179: FFT快速傅立叶
Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 2552 Solved: 1299
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
3
4
Sample Output
数据范围:
n<=60000
【bzoj2179】FFT快速傅立叶 FFT模板的更多相关文章
- 【BZOJ 2179】 2179: FFT快速傅立叶 (FFT)
2179: FFT快速傅立叶 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 3308 Solved: 1720 Description 给出两个n位 ...
- bzoj 2179: FFT快速傅立叶 -- FFT
2179: FFT快速傅立叶 Time Limit: 10 Sec Memory Limit: 259 MB Description 给出两个n位10进制整数x和y,你需要计算x*y. Input ...
- BZOJ2179:FFT快速傅立叶(FFT)
Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. Output 输出 ...
- spoj VFMUL FFT快速傅立叶变换模板题
题意:求两个数相乘. 第一次写非递归的fft,因为一个数组开小了调了两天TAT. #include<iostream> #include<cstring> #include&l ...
- 【bzoj2179】FFT快速傅立叶 FFT
题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. 输出 输出一行,即x*y的结果. 样例 ...
- BZOJ2179: FFT快速傅立叶 FFT实现高精度乘法
Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...
- BZOJ 2179 FFT快速傅立叶 ——FFT
[题目分析] 快速傅里叶变换用于高精度乘法. 其实本质就是循环卷积的计算,也就是多项式的乘法. 两次蝴蝶变换. 二进制取反化递归为迭代. 单位根的巧妙取值,是的复杂度成为了nlogn 范德蒙矩阵计算逆 ...
- bzoj 2179 FFT快速傅立叶 —— FFT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2179 默写板子,注释的是忘记的地方. 代码如下: #include<iostream& ...
- [bzoj2179]FFT快速傅立叶_FFT
FFT快速傅立叶 bzoj-2179 题目大意:给出两个n位10进制整数x和y,你需要计算x*y. 注释:$1\le n\le 6\times 10^4$. 想法: $FFT$入门题. $FFT$实现 ...
随机推荐
- poj 1701【数学几何】
The area Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- Ubuntu下配置samba实现文件夹共享
转自:http://www.cnblogs.com/phinecos/archive/2009/06/06/1497717.html 一. samba的安装: sudo apt-get insall ...
- Runtime 类
Runtime代表Java程序的运行时环境,每一个Java程序在运行时都有一个Runtime实例与之对应.Java程序通过它可以和运行时环境相连 1,和JVM进行交互,通知JVM进行垃圾回收等 2,获 ...
- C++中单例模式
//C++单例模式:指一个类只生成一个对象 #include <iostream> using namespace std; class A{ public: static A* getA ...
- Android:dimen尺寸资源文件的使用(转)
为了适配不同的分辨率. dimen.xml在values文件夹下面 <resources> <!-- Default screen margins, per the Android ...
- RMI的概念
RMI(Remote Method Invocation)远程方法调用是一种计算机之间利用远程对象互相调用实现双方通讯的一种通讯机制.使用这种机制,某一台计算机上的对象可以调用另外一台计算机上的对象来 ...
- Angular.js 以及个人学习网站
Angular.js 教程 http://www.360doc.com/content/14/0414/15/14416931_368816305.shtml web前端学习: 慕课网:http:/ ...
- 2016.8.16 HTML5重要标签及其属性学习
1.运用BootStrap的基本布局: 2.基本布局第二步: ] 3.BootStrap提供了一个class=”well“”类,可以给你种深度的感觉: 4.不是每一个类都是为了CSS,有些类创建出来只 ...
- node express新项目默认主文件app.js
var express = require('express'); var path = require('path'); var favicon = require('serve-favicon') ...
- 8611 大牛之路I
时间限制:500MS 内存限制:1000K 提交次数:157 通过次数:62 题型: 编程题 语言: C++;C Description 要成为ACM大牛,要掌握很多必需的知识点.某些知识点可以 ...