BZOJ3527: [Zjoi2014]力(FFT)
题意
Sol
直接把\(q_i\)除掉
那么\(E_j = \sum_{i = 1}^{j - 1} q_i \frac{1}{(i - j)^2} - \sum_{i = j + 1}^n q_i \frac{1}{(i - j)^2}\)
设\(f_i = q_i, g_i = \frac{1}{i^2}\)
带入原式发现原式变成了卷积的形式
\(E_j = f_i g_{i - j}\)
然后像\(BZOJ2194\)那样把\(g\)给翻转掉,就成了标准卷积形式
FFT一波
// luogu-judger-enable-o2
#include<bits/stdc++.h>
const double Pi = acos(-1);
using namespace std;
const int MAXN = 1e6 + 10;
int N, M, r[MAXN];
struct com {
double x, y;
com(double xx = 0, double yy = 0) {x = xx; y = yy;}
com operator + (com &rhs) {
return com(x + rhs.x, y + rhs.y);
}
com operator - (com &rhs) {
return com(x - rhs.x, y - rhs.y);
}
com operator * (com &rhs) {
return com(x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x);
}
}a[MAXN], b[MAXN], c[MAXN];
void FFT(com *a, int N, int type) {
for(int i = 0; i < N; i++) if(i < r[i]) swap(a[i], a[r[i]]);
for(int mid = 1; mid < N; mid <<= 1) {
com Wn(cos(Pi / mid), type * sin(Pi / mid));
for(int R = mid << 1, j = 0; j < N; j += R) {//这里要写<N
com w(1, 0);
for(int k = 0; k < mid; k++, w = w * Wn) {
com x = a[j + k], y = w * a[j + k + mid];
a[j + k] = x + y;
a[j + k + mid] = x - y;
}
}
}
if(type == -1) {
for(int i = 0; i <= N; i++) a[i].x /= N;
}
}
int Mul(com *c, com *a, com *b, int N, int M) {
int ret = 1, l = 0;
while(ret <= N + M) ret <<= 1, l++;
for(int i = 0; i < ret; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << l - 1);
FFT(a, ret, 1);
FFT(b, ret, 1);
for(int i = 0; i <= ret; i++) c[i] = a[i] * b[i];
FFT(c, ret, -1);
return ret;
}
int main() {
scanf("%d", &N); N -= 1;
for(int i = 0; i <= N; i++) scanf("%lf", &a[i].x);
for(int i = 0; i < N; i++) b[i].x = -1.0 / (double)(N - i) / (double)(N - i);
for(int i = N + 1; i <= 2 * N; i++) b[i].x = -b[2 * N - i].x;
Mul(c, a, b, N, 2 * N);
for(int i = N; i <= N * 2; i++) printf("%.5lf\n", c[i].x);
return 0;
}
作者:自为风月马前卒
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。