bzoj 3527

题意

给你一个长度为n的序列$a+i$,让求出$E_i$

$E_i = \sum_{k<i} \frac{a_k}{(i-k)^2} - \sum_{k>i} \frac{a_k}{(i-k)^2}$

n≤100000

分析

把$i-k$记做$d$

设$A[i] = a_i$, $B[d] = \frac{1}{d^2}$

那么前面那个式子直接卷积就可以了。

后面的式子因为是负数,所以要偏移一下

$A[i + N] = a_i$, $B[-d + N] = \frac{-1}{d^2}$

然后卷一下就好了。

主要学习一下这个偏移的操作。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
#define first fi
#define second se
#define my_unique(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))
#define my_sort_unique(c) (sort(c.begin(), c.end())), my_unique(c)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e6;
const double PI = acos(-1.0);
//复数结构体
struct Complex {
double x, y; //实部和虚部 x+yi
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);
}
};
/*
* 进行FFT和IFFT前的反转变换。
* 位置i和 (i二进制反转后位置)互换
* len必须取2的幂
*/
void change(Complex y[], int len) {
for (int i = 1, j = len / 2; i < len - 1; i++) {
if (i < j) swap(y[i], y[j]);
//交换互为小标反转的元素,i<j保证交换一次
// i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
int k = len / 2;
while (j >= k) j -= k, k /= 2;
if (j < k) j += k;
}
}
/*
* 做FFT
* len必须为2^k形式,
* on==1时是DFT,on==-1时是IDFT
* DFT:系数表示法->点值表示法
* IDFT:点值表示法->系数表示法
*/
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[maxn], x2[maxn];
double sum[maxn << 2];
double a[maxn];
int main() {
// /*
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
// */
std::ios::sync_with_stdio(false);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lf", a + i);
int len = 1;
while (len < n * 4) len <<= 1;
for (int i = 0; i < len; i++)
x1[i] = Complex(0, 0), x2[i] = Complex(0, 0), sum[i] = 0;
for (int i = 0; i < n; i++) x1[i + n].x = a[i];
for (int i = 1; i < n; i++) x2[i + n].x = 1.0 / (1.0 * i * i);
fft(x1, len, 1);
fft(x2, len, 1);
for (int i = 0; i < len; i++) x1[i] = x1[i] * x2[i];
fft(x1, len, -1);
for (int i = 0; i < len; i++) sum[i] += x1[i].x;

for (int i = 0; i < len; i++) x1[i] = Complex(0, 0), x2[i] = Complex(0, 0);
for (int i = 0; i < n; i++) x1[i + n].x = a[i];
for (int i = 1; i < n; i++) x2[n - i].x = -1.0 / (1.0 * i * i);
fft(x1, len, 1);
fft(x2, len, 1);
for (int i = 0; i < len; i++) x1[i] = x1[i] * x2[i];
fft(x1, len, -1);
for (int i = 0; i < len; i++) sum[i] += x1[i].x;

for (int i = 0; i < n; i++) {
printf("%.3lf\n", sum[i + 2 * n]);
}
}
Thank you for your support!