Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <algorithm>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int n;
- cin >> n;
- using ll = int64_t;
- unordered_map<int, ll> count;
- for (int i = 0; i < n; ++i) {
- int a;
- cin >> a;
- count[a]++;
- }
- vector<int> stick;
- for (auto it : count) {
- stick.push_back(it.first);
- }
- sort(stick.begin(), stick.end());
- int m = (int)stick.size();
- ll res = 0;
- // Case 1: Two single-stick sides
- // Goal: Count pair of pair (a1, b1) (a2, b2) s.t. a1+b1=a2+b2=stick[i]
- for (int i = 1; i < m; ++i) {
- if (count[stick[i]] < 2) continue;
- ll cur = 0;
- ll ans = 0;
- for (int j = i-1; j >= 0; --j) {
- if (2 * stick[j] < stick[i]) break;
- if (2 * stick[j] == stick[i]) {
- ll c = count[stick[j]];
- ans += c * (c - 1LL) * (c - 2LL) * (c - 3LL) / 24LL;
- ans += c * (c - 1LL) / 2LL * cur;
- cur += c * (c - 1LL) / 2LL;
- }
- else {
- ll a = count[stick[i] - stick[j]];
- ll b = count[stick[j]];
- ans += a * (a - 1LL) * b * (b - 1LL) / 4LL;
- ans += a * b * cur;
- cur += a * b;
- }
- }
- ll t = count[stick[i]];
- res += t * (t - 1LL) / 2LL * ans;
- }
- // Case 2: Three single-stick sides
- // Goal: Count number of triplets (a, b, c) s.t. a+b+c=stick[i]
- unordered_map<int, ll> two;
- for (int i = 0; i < m; ++i) {
- int k = 2 * stick[i];
- ll c = count[stick[i]];
- two[k] += c * (c - 1LL) / 2LL;
- for (int j = i+1; j < m; ++j) {
- int k = stick[i] + stick[j];
- if (k > stick[m-1]) break;
- two[k] += count[stick[i]] * count[stick[j]];
- }
- }
- for (int i = 1; i < m; ++i) {
- if (count[stick[i]] < 3) continue;
- ll ans = 0;
- for (int j = i-1; j >= 0; --j) {
- int k = stick[i] - stick[j];
- ll a = two[k];
- if (k > stick[j]) {
- a -= count[k - stick[j]];
- if (2 * stick[j] == k) ++a;
- }
- ans += count[stick[j]] * a;
- }
- ll c = count[stick[i]];
- ans = ans / 3LL;
- ans = ans * c * (c - 1LL) * (c - 2LL) / 6LL;
- res += ans;
- }
- cout << res << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement