Advertisement
Guest User

Untitled

a guest
Feb 14th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main() {  
  8.     ios_base::sync_with_stdio(false);
  9.     cin.tie(nullptr);
  10.     int n;
  11.     cin >> n;
  12.     using ll = int64_t;
  13.     unordered_map<int, ll> count;
  14.     for (int i = 0; i < n; ++i) {
  15.         int a;
  16.         cin >> a;
  17.         count[a]++;
  18.     }
  19.    
  20.     vector<int> stick;
  21.     for (auto it : count) {
  22.         stick.push_back(it.first);
  23.     }
  24.     sort(stick.begin(), stick.end());
  25.     int m = (int)stick.size();
  26.    
  27.     ll res = 0;
  28.     // Case 1: Two single-stick sides
  29.     // Goal: Count pair of pair (a1, b1) (a2, b2) s.t. a1+b1=a2+b2=stick[i]
  30.     for (int i = 1; i < m; ++i) {
  31.         if (count[stick[i]] < 2) continue;
  32.         ll cur = 0;
  33.         ll ans = 0;
  34.         for (int j = i-1; j >= 0; --j) {
  35.             if (2 * stick[j] < stick[i]) break;
  36.             if (2 * stick[j] == stick[i]) {
  37.                 ll c = count[stick[j]];
  38.                 ans += c * (c - 1LL) * (c - 2LL) * (c - 3LL) / 24LL;
  39.                 ans += c * (c - 1LL) / 2LL * cur;
  40.                 cur += c * (c - 1LL) / 2LL;
  41.             }
  42.             else {
  43.                 ll a = count[stick[i] - stick[j]];
  44.                 ll b = count[stick[j]];
  45.                 ans += a * (a - 1LL) * b * (b - 1LL) / 4LL;
  46.                 ans += a * b * cur;
  47.                 cur += a * b;
  48.             }
  49.         }
  50.         ll t = count[stick[i]];
  51.         res += t * (t - 1LL) / 2LL * ans;
  52.     }
  53.    
  54.     // Case 2: Three single-stick sides
  55.     // Goal: Count number of triplets (a, b, c) s.t. a+b+c=stick[i]
  56.     unordered_map<int, ll> two;
  57.     for (int i = 0; i < m; ++i) {
  58.         int k = 2 * stick[i];
  59.         ll c = count[stick[i]];
  60.         two[k] += c * (c - 1LL) / 2LL;
  61.         for (int j = i+1; j < m; ++j) {
  62.             int k = stick[i] + stick[j];
  63.             if (k > stick[m-1]) break;
  64.             two[k] += count[stick[i]] * count[stick[j]];
  65.         }
  66.     }
  67.    
  68.     for (int i = 1; i < m; ++i) {
  69.         if (count[stick[i]] < 3) continue;
  70.         ll ans = 0;
  71.         for (int j = i-1; j >= 0; --j) {
  72.             int k = stick[i] - stick[j];
  73.             ll a = two[k];
  74.             if (k > stick[j]) {
  75.                 a -= count[k - stick[j]];
  76.                 if (2 * stick[j] == k) ++a;
  77.             }
  78.             ans += count[stick[j]] * a;
  79.         }
  80.         ll c = count[stick[i]];
  81.         ans = ans / 3LL;
  82.         ans = ans * c * (c - 1LL) * (c - 2LL) / 6LL;
  83.         res += ans;
  84.     }
  85.    
  86.     cout << res << '\n';
  87.     return 0;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement