FFT学习&总结(模板:见kuangbin)

这篇博客介绍了如何利用快速傅里叶变换(FFT)来解决一道组合计数的问题。题目涉及在y=-1,0,1三条直线上选取点的组合数量,通过将问题转化为在[-3e4,3e4]区间内的点的组合,博主给出了使用FFT的解决方案,并提供了详细的代码实现。博客中还提到了模板题目的来源和参考资源。
摘要由CSDN通过智能技术生成

前言

  1. zxy大佬给我讲了讲,冲一波吧。
  2. 参考博客:【zxy大佬给我讲了一个模板题】

题目

题目1(模板题):cf-gym-首尔icpc-H

  1. 题意:y=-1,0,1这条直线上分别有一些点,三条直线上各取一个点,在一条直线上的组合有多少。
    1. x维为整数,在 [ − 3 e 4 , 3 e 4 ] [-3e4,3e4] [3e4,3e4]范围内。
  2. 题解
    1. 只看-1,1上的点,然后先增加3e4再多项式乘一下,得到答案(懒得叙述了emm)
  3. 代码
/*
链接:https://codeforces.com/gym/102920/problem/H
模板:kuangbin
*/
#include <bits/stdc++.h>
#define dbg(x) cout << #x << "===" << x << endl
const int N = (1 << 17) + 5;  // DFT的结果会变成6e4的2倍
using namespace std;

int n, m, k;
int a[N], b[N], c[N];
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;
}
Complex x1[N], x2[N];
int va[N], vb[N], vc[N];
int len = (1 << 17);  // len必须是2^k的形式(直接取比结果数组的大小大的2^k)
signed main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i], a[i] += 3e4, va[a[i]] = 1;
    cin >> m;
    for (int i = 1; i <= m; i++) cin >> b[i], b[i] += 3e4, vb[b[i]] = 1;
    cin >> k;
    for (int i = 1; i <= k; i++) cin >> c[i], c[i] += 3e4, vc[c[i]] = 1;

    for (int i = 0; i < len; i++) {
        x1[i] = Complex(va[i], 0);
    }
    for (int i = 0; i < len; i++) {
        x2[i] = Complex(vc[i], 0);
    }
    //求DFT
    fft(x1, len, 1);
    fft(x2, len, 1);
    for (int i = 0; i < len; i++) x1[i] = x1[i] * x2[i];
    fft(x1, len, -1);  //将最终结果放入len中
    //求完了DFT(离散傅里叶变换,FFT:快速求DFT)
    int ans = 0;
    for (int i = 0; i < N; i++) {
        // if (i < 10) dbg(x1[i].x);
        if (i % 2 == 1) continue;
        if (vb[i / 2]) ans += int(x1[i].x + 0.5);
    }
    cout << ans << endl;
    return 0;
}
/*
input:::
1
1
1
2
1
1
##################
3
4 -3 2
2
4 1
3
-3 4 0
####################
3
-1 1 0
3
0 1 -1
3
0 -1 1
output:::
0
################
2
##################
5
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值