Mahmoud_Hawara

Untitled

Mar 7th, 2022
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  4. const long long N = 2e5 + 5, MOD = 1e9 + 7, OO = 1e18;
  5. const double PI = acos(-1);
  6. const int dx[4] = {0, 0, 1, -1};
  7. const int dy[4] = {1, -1, 0, 0};
  8.  
  9. double t, n, a[N];
  10.  
  11. int main() {
  12.     IO
  13.     cin >> t;
  14.     while (t--) {
  15.         map<double, long long>mp;
  16.         map<double, bool>isUsed;
  17.         long long sm = 0;
  18.         cin >> n;
  19.         for (int i = 1; i <= n; i++) {
  20.             cin >> a[i];
  21.             mp[a[i]]++;
  22.             sm += a[i];
  23.         }
  24.         double k = (double)sm / n;
  25.         long long ans = 0;
  26.         for (int i = 1; i <= n; i++) {
  27.             if (isUsed[a[i]])continue;
  28.             double x = (double)sm - a[i] - k * (n - 2);
  29.             if (mp[x])
  30.                 if (x == a[i])ans += mp[a[i]] * (mp[a[i]] - 1) / 2;
  31.                 else ans += mp[a[i]] * mp[x];
  32.             isUsed[x] = isUsed[a[i]] = 1;
  33.         }
  34.         cout << ans << '\n';
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment