zoukankan      html  css  js  c++  java
  • P3338 [ZJOI2014]力 (FFT)

    题意:$$F(i)= sum_{j=0} ^{i - 1} frac{qi * qj}{(j - i)^{2}} - sum_{j = i + 1}^{n} frac{qi * qj}{(j - i)^{2}}$$
    求Fi/qi
    题解:可以分开来算 前面一坨 后面一坨

    [f(i) = qi ]

    [g(i)=frac{1}{i^2} ]

    [F(i) = sum_{j = 0}^{i - 1} f(i) * g(i - j) - sum_{j = i + 1}^{n} f(i) * g(j - i) ]

    前面已经可以算了 然后后面的g(j - i) = g(i - j) 套路的把f翻转一下

    [sum_{j = i + 1}^{n} f(n - j + 1) * g(j - i) = F(n + 1 - i) ]

    [然后就都可以卷了! ]

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    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[400005], x2[400005], x3[400005];
    int main() {
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            double x;
            scanf("%lf", &x);
            x1[i] = Complex(x, 0);
            x2[n - i + 1] = Complex(x, 0);
            x3[i] = Complex(1.0 / (1.0 * i * i), 0);
        }
        int len = 1;
        while(len <= n + n) len <<= 1;
        fft(x1, len, 1);
        fft(x2, len, 1);
        fft(x3, len, 1);
        for(int i = 0; i <= len; i++) x1[i] = x1[i] * x3[i];
        for(int i = 0; i <= len; i++) x2[i] = x2[i] * x3[i];
        fft(x1, len, -1);
        fft(x2, len, -1);
        for(int i = 1; i <= n; i++) {
            printf("%.3lf
    ", x1[i].x - x2[n - i + 1].x);
        }
        return 0;
    }
    
  • 相关阅读:
    LeetCode题解之Flipping an Image
    LeetCode 之Find Minimum in Rotated Sorted Array
    LeetCode题解Transpose Matrix
    LeetCode 题解之Minimum Index Sum of Two Lists
    LeetCode题解之Intersection of Two Linked Lists
    LeetCode 题解之Add Two Numbers II
    LeetCode题解之Add two numbers
    href="#"与href="javascript:void(0)"的区别
    有关ie9 以下不支持placeholder属性以及获得焦点placeholder的移除
    ie7下属性书写不规范造成的easyui 弹窗布局紊乱
  • 原文地址:https://www.cnblogs.com/lwqq3/p/11349255.html
Copyright © 2011-2022 走看看