forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaplusb.cpp
123 lines (107 loc) · 2.98 KB
/
aplusb.cpp
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
115
116
117
118
119
120
121
122
123
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef complex<double> cd;
const ll Max = 1e6+10;
ll bound, logBound;
const double pi = 4*atan(1.0);
cd root[Max], arrA[Max], arrB[Max];
ll perm[Max];
ll prod[Max];
void fft(cd* arr) {
for(ll i = 0; i < bound; i++) {
if(i < perm[i]) {
swap(arr[i], arr[perm[i]]);
}
}
for(ll len = 1; len < bound; len *= 2) {
for(ll pos = 0; pos < bound; pos += 2 * len) {
for(ll i = 0; i < len; i++) {
cd x = arr[pos + i], y = arr[pos + i + len] * root[bound / len / 2 * i];
arr[pos + i] = x + y;
arr[pos + i + len] = x - y;
}
}
}
}
void preCalc() {
ll hb = -1;
root[0] = 1;
double angle = 2 * pi / bound;
for(ll i = 1; i < bound; i++) {
if((i & (i - 1)) == 0) hb++;
root[i] = cd(cos(angle * i), sin(angle * i));
perm[i] = perm[i ^ (1 << hb)] + (1 << (logBound - hb - 1));
}
}
void mult(vector<ll> &a, vector<ll> &b, vector<ll> &c) {
logBound = 0;
while((1<<logBound) < a.size() || (1<<logBound) < b.size()) logBound++;
logBound++;
bound = (1<<logBound);
preCalc();
for(ll i = 0; i < a.size(); i++) {
arrA[i] = cd(a[i], 0);
}
for(ll i = a.size(); i < bound; i++) {
arrA[i] = cd(0, 0);
}
for(ll i = 0; i < b.size(); i++) {
arrB[i] = cd(b[i], 0);
}
for(ll i = b.size(); i < bound; i++) {
arrB[i] = cd(0, 0);
}
fft(arrA);
fft(arrB);
for(ll i = 0; i < bound; i++) {
arrA[i] *= arrB[i];
}
fft(arrA);
reverse(arrA + 1, arrA + bound);
c.resize(bound);
for(ll i = 0; i < bound; i++) {
arrA[i] /= bound;
ll temp = (arrA[i].real() > 0 ? arrA[i].real()+.5 : arrA[i].real() - .5);
c[i] = temp;
}
while(c.size() && c.back() == 0) c.pop_back();
}
ll ADD = 50001;
int main() {
ll n;
cin >> n;
// Count Zeros
int zeroes = 0;
// Memory for FFT Arrays
vector<ll> freq(200008,0);
vector<ll> res;
// Keep input
vector<ll> all;
// Input
for(ll i = 0; i < n; i++) {
ll t; cin >> t;
// Add "ADD" to all numbers so that there's no negatives
all.push_back(t+ADD);
// Frequency represents count of each unique number
freq[t+ADD]++;
// Count zeros
if(t == 0) zeroes++;
}
// Multiply frequency by itself (represents frequency of unique numbers after adding
// all numbers to all other numbers
mult(freq,freq,res);
// For each number, remove it added to itself (basically, don't allow i+i=k. only i+j=k)
for(auto t : all) {
res[t*2]--;
}
// Answer - for each number in the input, add the number of ways
// we can add two numbers to get it
ll ans = 0;
for(auto t : all) {
ans += res[t+ADD];
}
// Remove the number of ways zeros can be added to all other numbers
ans -= (ll)2 * zeroes * (n-1);
cout << ans << endl;
}