您的位置:首页 > 其它

acm fft简单理解和相关题目

2017-05-26 19:48 260 查看
Fast Fourier Transformation FFT 

快速傅里叶变换 ——一种算法

它是解决DFT的

Discrete Fourier transform DFT 

离散傅里叶变换 ——一种过程

 我不知道fft具体是怎么做的,我会通过下面这几个例子来告诉你fft是做什么的;

你可以理解为就是在Θ(nlogn)O(nlogn)的时间算出两个多项式相乘。

多项式乘法

A*B=C

A = a0 + a1 x^1 + a2 x^2 + a(n-1) x^(n-1)

B = b0 + ...

C = c0 + ... +c(n-1) x^(n-1) + cn x^n + ... +c(2n-1) x^(2n-1)

我模拟一下 就是 A,B分别存在一个数组里,i次项的系数j存在a[i]里面,(a[i] = j),比如 A:a[0] = a0, a[1] = a1, b[0] = b1;

然后就可以在O(nlogn)的时间算出C;c[0] = a0+b0........

下面看具体经典题目

一: hdu 1402 求大数乘法(O(nlogn)).

就是裸的fft ,一般的高精度乘法复杂度是O(n^2).

先用fft求出每位的值,在进位,具体看代码注释;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;

// 这一大坨fft的代码实现不要动的
const double PI = acos(-1.0);
struct Complex {
double x, y;
Complex(double _x = 0.0, double _y = 0.0) {
x = _x;
y = _y;
}
Complex operator - (const Complex &b)const {
return Complex(x-b.x, y-b.y);
}
Complex operator + (const Complex &b)const {
return Complex(x+b.x, y+b.y);
}
Complex operator * (const Complex &b)const {
return Complex(x*b.x-y*b.y, x*b.y+y*b.x);
}
};

void change(Complex y[], int len) {
int i, j, k;
for(i = 1, j = len/2; i < len-1; i++) {
if (i < j) swap(y[i], y[j]);
k = len/2;
while(j >= k) {
j -= k;
k /= 2;
}
if (j < k) j += k;
}
}

void fft(Complex y[], int len, int on) {
change(y, len);
for(int h = 2; h <= len; h <<= 1) {
Complex wn(cos(-on*2*PI/h), sin(-on*2*PI/h));
for(int j = 0; j < len; j += h) {
Complex w(1, 0);
for(int k = j; k < j+h/2; k++) {
Complex u = y[k];
Complex t = w*y[k+h/2];
y[k] = u+t;
y[k+h/2] = u-t;
w = w*wn;
}
}
}
if (on == -1)
for(int i = 0; i < len; i++)
y[i].x /= len;
}
//到这里
//下面这些数组的大小不是乱开的,下面会讲
const int MAXN = 50002;
LL num[MAXN<<2];//保存结果的数组,要开4*MAXN
Complex x1[MAXN<<2], x2[MAXN<<2];//模板里需要的数组,也要开4*MAXN
char str1[MAXN], str2[MAXN];

int main() {
//freopen("in.txt", "r", stdin);
while (scanf("%s%s", str1, str2) == 2) {
int ls1 = strlen(str1), ls2 = strlen(str2);
int len = 1;
//下面也是模板,别问为什么就是这样的
while (len < 2*ls1 || len < 2*ls2) len <<= 1;//上面数组的大小是因为这里
int i;
for(i = 0; i < ls1; i++) {
x1[i] = Complex(str1[ls1-i-1]-'0', 0);
}
for(; i < len; i++)
x1[i] = Complex(0, 0);
fft(x1, len, 1);
for(i = 0; i < ls2; i++) {
x2[i] = Complex(str2[ls2-i-1]-'0', 0);
}
for(; i < len; i++)
x2[i] = Complex(0, 0);
fft(x2, len, 1);
for(i = 0; i < len; i++)
x1[i] = x1[i]*x2[i];
fft(x1, len, -1);
for(i = 0; i < len; i++) {
num[i] = (LL)(x1[i].x+0.5);
}
//到这里num里面保存的就是结果
for(i = 0; i < len; i++) { //进位
num[i+1] += num[i]/10;
num[i] %= 10;
}
len = ls1+ls2-1;
while(num[len] <= 0 && len > 0) len--;//去前置零;
for(i = len; i >= 0; i--)
printf("%I64d", num[i]);
puts("");
}
return 0;
}


例二:
Gym - 100783C https://vjudge.net/problem/Gym-100783C

题意:一个机器人打高尔夫只往一个方向打,每次只能打固定的距离,求最多两杆能打进的洞的个数;差不多可以理解为一个数列相加;

题解:利用fft算法的特点,ax * by 放在 c的第 x+y位; x,y就是那些固定的距离,x+y就是可以打进的洞;还不懂的话具体看代码;

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;

/*
这里省略了fft
*/
const int MAXN = 400002;//注意数组大小
LL num[MAXN*2];
int a[MAXN/2];
Complex x1[MAXN*2];
int m, n;

int main() {
scanf("%d", &n);
int len1 = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", a+i);
num[a[i]] = 1; //这里赋值为一就好了
len1 = max(len1, a[i]+1);//找到最大值就是len1
}
num[0] = 1;//这里要赋值为1,求出1杆可以打进的洞
//下面就是套路了
int len = 1;
while (len < 2*len1) len <<= 1;
for(int i = 0; i < len1; i++)
x1[i] = Complex(num[i], 0);
for(int i = len1; i < len; i++)
x1[i] = Complex(0, 0);
fft(x1, len, 1);
for(int i = 0; i < len; i++)
x1[i] = x1[i]*x1[i];
fft(x1, len, -1);
for(int i = 0; i < len; i++) {
num[i] = (LL)(x1[i].x+0.5);
}

scanf("%d", &m);
int res = 0;
for(int i = 1; i <= m; i++) {
int t;
scanf("%d", &t);
if (num[t])//这里写起来就比较简单了
res++;
}
printf("%d\n", res);
return 0;
}

例三: hdu 4609  3-idiots http://acm.hdu.edu.cn/showproblem.php?pid=4609
题意: 给出一些边的长度,求任取3条边可以形成三角形的概率;

题解: 用古典概型;求出可以形成三角形的个数再除以总个数;

这个人解释的很好,直接看他的吧:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  fft hdu
相关文章推荐