洛谷 [P3723] 礼物

FFT

https://www.luogu.org/problemnew/solution/P3723
重点在于构造卷积的形式

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int MAXN = 400005;
const double PI = acos(-1);
int init() {
    int rv = 0, fh = 1;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') fh = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        rv = (rv<<1) + (rv<<3) + c - '0';
        c = getchar();
    }
    return fh * rv;
}
struct Complex {
    double x, y;
    Complex(double xx = 0.0, double yy = 0.0) {
        x = xx; y = yy;
    }
    Complex operator + (const Complex &u) const {
        return Complex(x + u.x, y + u.y);
    }
    Complex operator - (const Complex &u) const {
        return Complex(x - u.x, y - u.y);
    }
    Complex operator * (const Complex &u) const {
        return Complex(x * u.x - y * u.y, x * u.y + y * u.x);
    }
}a[MAXN], b[MAXN];
int n, m, lim = 1, limcnt, rev[MAXN], num1[MAXN], num2[MAXN], ttt;
long long ans = 0, c;
void fft(Complex a[], int opt) {
    for(int i = 0; i <= lim; i++) {
        if(i < rev[i]) swap(a[i], a[rev[i]]);
    }
    for(int mid = 1; mid < lim; mid <<= 1) {
        Complex wn = Complex(cos(PI / mid), opt * sin(PI / mid));
        for(int R = mid << 1, j = 0; j < lim; j += R) {
            Complex w = Complex(1.0, 0.0);
            for(int k = 0; k < mid; k++) {
                Complex x = a[j + k], y = w * a[j + mid + k];
                a[j + k] = x + y;
                a[j + mid + k] = x - y;
                w = w * wn;
            }
        }
    }
    if(opt == -1) {
        for(int i = 0; i <= lim; i++) {
            a[i].x /= lim;
        }
    }
}
int main() {
    n = init(); m = init();
    for(int i = 1; i <= n; i++) {
        num1[i] = init();
        a[n - i].x = num1[i];
        ans += num1[i] * num1[i];
        ttt += num1[i];
    }
    for(int i = 0; i < n; i++) {
        num2[i] = init();
        b[i].x = b[i + n].x = num2[i];
        ans += num2[i] * num2[i];
        ttt -= num2[i];
    }
    double t = -(double)ttt / n;
    if(t > 0.0) c = (int)(t + 0.5);
    else c = (int) (t - 0.5);
    ans += n * c * c + 2 * c * ttt; 
    while(lim <= n * 3) lim <<= 1, limcnt++;
    for(int i = 0; i <= lim; i++)
        rev[i] = (rev[i>>1]>>1) | ((i&1) << (limcnt - 1)); 
    fft(a, 1); fft(b, 1);
    for(int i = 0; i <= lim; i++) {
        a[i] = a[i] * b[i];
    }
    fft(a, -1);
    int tmp = 0;
    for(int i = n - 1; i <= 2 * n - 1; i++) {
        tmp = max(tmp, (int)(a[i].x + 0.01));
    }
    ans -= 2 * tmp;
    cout << ans << endl;
    return 0;
}

转载于:https://www.cnblogs.com/Mr-WolframsMgcBox/p/9081283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值