• 洛谷 [P3338] 力


    FFT

    [E_i = F_i / q_i = sum_{i<j} frac {q_j} {(i - j)^2} - sum _{ i > j} frac{q _ j} {(i - j)^2} ]

    (p _ i = q_{n - i}) (g(i) = frac 1 {i^2})

    (E_i = sum_{j=1}^{i-1} q _ i g(j - i) - sum _ {j = i + 1} ^ n q_j g(i - j))

    (E_i = sum_{j=1}^{i-1} q_i g(i-1) - sum_{j=1}^{n - i} p_i g(i - j))

    我们发现这是一个卷积的形式, 可以用 FFT 处理

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    const int MAXN = 400005;
    const double PI = acos(-1);
    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], c[MAXN], buf[MAXN];
    int n, lim = 1, limcnt, rev[MAXN];
    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 = a[i].x / lim;
    	}
    }
    int main() {
    	cin >> n;
    	for(int i = 1; i <= n; i++) {
    		scanf("%lf", &a[i].x);
    		b[n - i].x = a[i].x;
    		c[i].x = (1.0 / (double)i) / (double)i;
    	}
    	while(lim <= (n << 1)) 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); fft(c, 1);
     	for(int i = 0; i <= lim; i++) a[i] = a[i] * c[i];
     	for(int i = 0; i <= lim; i++) b[i] = b[i] * c[i];
     	fft(a, -1); fft(b, -1);
     	for(int i = 1; i <= n; i++) {
     		printf("%.4f
    ", a[i].x - b[n - i].x);
     	}
    	return 0;
    }```
  • 相关阅读:
    Http和Socket连接区别
    解决TCP网络传输“粘包”问题
    c# 获取MAC IP TCP列表
    《你不常用的c#之四》:Array的小抽屉ArraySegment
    《你不常用的c#之三》:Action 之怪状
    《你不常用的c#之二》:略谈GCHandle
    c#中var关键字用法
    Combotree--别样的构建层级json字符串
    一周代码秀之[11.18~11.24 linq2xml面向对象]
    这个季节的这些时候
  • 原文地址:https://www.cnblogs.com/Mr-WolframsMgcBox/p/9080982.html
Copyright © 2020-2023  润新知