acwing2241 礼物( FFT)

该篇文章介绍了一种使用快速傅立叶变换(FFT)算法处理复数数据的计算方法,用于求解两个复数序列的特定问题,并通过并行计算优化了计算过程,最终输出结果是两个复数序列的平方和减去相关系数的平方的最大整数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

题目:https://www.acwing.com/problem/content/2243/

思路: 

 代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
using namespace std;
#define LL  long long
const int N = 4e5 + 10;
const LL mod = 998244353;
const double PI = acos(-1);
struct Complex
{
    double x, y;
    Complex operator +(const Complex& t) const
    {  
        return { x + t.x,y + t.y };
     }
    Complex operator -(const Complex& t) const
    {
        return { x - t.x,y -t.y };
    }
    Complex operator *(const Complex& t) const
    {
        return { x*t.x - y*t.y,x*t.y+y*t.x};
    }
}a[N], b[N];
int n, m;
LL ans1, ans2;
int tot, bit;
int rel[N];
LL gg(int c,int bb)
{
    return (LL)n * c * c + 2 * c * bb;
}
void fft(Complex a[], int op)
{   
    for (int i = 0; i < tot; i++)
        if (i < rel[i])swap(a[i], a[rel[i]]);
    for (int m = 2; m <= tot; m *= 2)
    {
        Complex w1 ={ cos(2 * PI / m),op * sin(2 * PI / m) };
        for (int i = 0; i < tot; i+=m)
        {
            Complex wk = { 1,0 };
            for (int j = 0; j < m / 2; j++)
            {
                Complex x = a[i + j], y = a[i + j + m / 2]*wk;
                a[i + j] = x + y;
                a[i + j + m / 2] = x - y;
                wk = w1 * wk;
            }
        }

    }

}
int main() {
    cin >> n >> m;
    LL bb = 0;
    for (int i = 0; i <n; i++)
    {
        cin >> a[n - 1 - i].x;
        ans1 += (LL)a[n - 1 - i].x * a[n - 1 - i].x;
        bb +=(LL)a[n - 1 - i].x;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i].x;
        ans1 += b[i].x* b[i].x;
        bb -= b[i].x;
    }
    for (int i = 0; i <= n - 1; i++)
        a[i + n] = a[i];
    int cc = -bb / n;
    ans1 += min(gg(cc,bb), min(gg(cc+1,bb), gg(cc-1,bb)));
    n--;
    while ((1 << bit) < 3 * n + 1) bit++;
    tot = 1 << bit;
    for (int i = 0; i < tot; i++) rel[i] = rel[i / 2] / 2 + (i & 1 ? tot / 2 : 0);
    fft(a, 1);
    fft(b, 1);
    for (int i = 0; i < tot; i++) a[i] = a[i] * b[i];
    fft(a, -1);
    for (int i = n - 1; i <= 2 * (n - 1); i++)
    { 
        ans2 = max((int)ans2, int(a[i].x / tot + 0.5));
    }
    ans1 = ans1 - 2 * ans2;
    cout << ans1 << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值