HDOJ 4609

题目

从N个数中选3个,能组成三角形的方案数有多少。

数据范围

$3\leq N \leq 10^5$

$1\leq 数的大小 \leq 10^5$

做法

因为数的大小不超过$10^5$,所以可以用num[i]表示长度为i的数有多少个。用FFT求出num数组与自身的卷积,此卷积的第i个数即是“任意取2个数,长度为i的方案数”。而这里面包含了同一个数选2次的方案,要减去。这里面第一次选a和第二次选b与第一次选b和第二次选a算做了不同的方案,所以要将所有方案数/2。

将原来的数组排序,从小到大枚举每个数作为选择的三个数中最右边的数,不断累加答案。假设此时枚举到$a_i$,那么我们要求的就是它左边的数选2个有多少种选法使得两数之和大于$a_i$。而通过FFT的预处理,我们可以知道当2个数的位置没有要求时,和大于$a_i$的方案数为$\sum_{i>a_i}卷积[i]$,这个可以通过前缀和O(1)得到。我们只要从中减去位置不合法的方案数即可。

不合法的位置有以下几种:

  1. 2个数都在$a_i​$右边
  2. 1个在左,1个在右
  3. 1个就是$a_i$,另一个随意

代码

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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_NUM = 1e5 + 5;
const int MAX_N = 1e5 + 5;
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);
}
};
int N;
int a[MAX_N];
ll num[MAX_NUM << 2];
ll sum[MAX_NUM << 2];
Complex x[MAX_NUM << 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]);
int 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;
}
}
int main()
{
int T; cin >> T;
while (T--) {
scanf("%d", &N);
memset(num, 0, sizeof num);
for (int i = 0; i < N; ++i) {
scanf("%d", &a[i]);
++num[a[i]];
}
sort(a, a + N);
int len = 1;
int len1 = a[N - 1] + 1;
while (len < len1 * 2) len <<= 1;
for (int i = 0; i < len1; ++i) x[i] = Complex(num[i], 0);
for (int i = len1; i < len; ++i) x[i] = Complex(0, 0);
fft(x, len, 1);
for (int i = 0; i < len; ++i) x[i] = x[i] * x[i];
fft(x, len, -1);
for (int i = 0; i < len; ++i) num[i] = (ll)(x[i].x + 0.5);
for (int i = 0; i < N; ++i) num[a[i] + a[i]]--;
len = 2 * a[N - 1];
for (int i = 1; i <= len; ++i) num[i] /= 2;
sum[len + 1] = 0;
for (int i = len; i >= 1; --i) sum[i] = sum[i + 1] + num[i];
ll ans = 0;
for (int i = 2; i < N; ++i) {
ans += sum[a[i] + 1];
ll nl = i, nr = N - 1 - i;
ans -= nr * (nr - 1) / 2;
ans -= N - 1;
ans -= nl * nr;
}
double tot = (double)N * (N - 1) * (N - 2) / 6;
printf("%.7f\n", ans / tot);
}
return 0;
}

注意

  1. FFT的数组要开4倍原数组的大小
  2. 当有$10^5$个相同的数时,做卷积的结果会爆int,需要用long long