Language: C++

#include <bits/stdc++.h>
 
using namespace std;
#define ll long long


// 組み合わせ数を計算する。nCr
map<ll, vector<ll>> _combinations;
ll combination(int n, int r) {
    if(n < 1 || r < 1) return int(0);
    if(n == r) return int(1);
    if(r > n - r) r = n - r;
    if(!_combinations.count(n)) {
        vector<int> vec = {0, n};
        _combinations.emplace_hint(_combinations.end(), n, vec);
    }
    auto&& vec = _combinations.find(n)->second;
    if(r > n - r) r = n - r;
    if(vec.size() <= r) {
        int i = vec.size();
        vec.resize(r + 1);
        for(; i <= r; i++) {
            vec[i] = vec[i - 1];
            vec[i] *= (n - i + 1);
            vec[i] /= i;
        }
    }

    return vec[r];
}
int main() {
    cin.tie(0); ios::sync_with_stdio(false); cout << fixed << std::setprecision(20);

    int n;
    cin >> n;
    vector<int> as(n), counts(n + 1, 0);
    
    for(auto&& a : as) {
        cin >> a;
        counts[a]++;
    }
    
    ll total = 0;
    
    for(auto&& count : counts) {
        total += combination(count, 2);
    }

    for(auto&& a : as) {
        cout << total - combination(counts[a], 2) + combination(counts[a] - 1, 2) << endl;
    }
}

Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const long long int, std::vector<long long int> >; _Args = {int&, std::vector<int, std::allocator<int> >&}; _Tp = std::_Rb_tree_node<std::pair<const long long int, std::vector<long long int> > >]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const long long int, std::vector<long long int> >; _Args = {int&, std::vector<int, std::allocator<int> >&}; _Tp = std::_Rb_tree_node<std::pair<const long long int, std::vector<long long int> > >; allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const long long int, std::vector<long long int> > > >]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:593:32:   required from 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(_Link_type, _Args&& ...) [with _Args = {int&, std::vector<int, std::allocator<int> >&}; _Key = long long int; _Val = std::pair<const long long int, std::vector<long long int> >; _KeyOfValue = std::_Select1st<std::pair<const long long int, std::vector<long long int> > >; _Compare = std::less<long long int>; _Alloc = std::allocator<std::pair<const long long int, std::vector<long long int> > >; _Link_type = std::_Rb_tree_node<std::pair<const long long int, std::vector<long long int> > >*]'
  593 |               _Alloc_traits::construct(_M_get_Node_allocator(),
      |               ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
  594 |                                        __node->_M_valptr(),
      |                                        ~~~~~~~~~~~~~~~~~~~~
  595 |                                        std::forward<_Args>(__args)...);
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:610:21:   required from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {int&, std::vector<int, std::allocator<int> >&}; _Key = long long int; _Val = std::pair<const long long int, std::vector<long long int> >; _KeyOfValue = std::_Select1st<std::pair<const long long int, std::vector<long long int> > >; _Compare = std::less<long long int>; _Alloc = std::allocator<std::pair<const long long int, std::vector<long long int> > >; _Link_type = std::_Rb_tree_node<std::pair<const long long int, std::vector<long long int> > >*]'
  610 |           _M_construct_node(__tmp, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1633:32:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Auto_node::_Auto_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, _Args&& ...) [with _Args = {int&, std::vector<int, std::allocator<int> >&}; _Key = long long int; _Val = std::pair<const long long int, std::vector<long long int> >; _KeyOfValue = std::_Select1st<std::pair<const long long int, std::vector<long long int> > >; _Compare = std::less<long long int>; _Alloc = std::allocator<std::pair<const long long int, std::vector<long long int> > >]'
 1633 |             _M_node(__t._M_create_node(std::forward<_Args>(__args)...))
      |                     ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:2460:13:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(const_iterator, _Args&& ...) [with _Args = {int&, std::vector<int, std::allocator<int> >&}; _Key = long long int; _Val = std::pair<const long long int, std::vector<long long int> >; _KeyOfValue = std::_Select1st<std::pair<const long long int, std::vector<long long int> > >; _Compare = std::less<long long int>; _Alloc = std::allocator<std::pair<const long long int, std::vector<long long int> > >; iterator = std::_Rb_tree<long long int, std::pair<const long long int, std::vector<long long int> >, std::_Select1st<std::pair<const long long int, std::vector<long long int> > >, std::less<long long int>, std::allocator<std::pair<const long long int, std::vector<long long int> > > >::iterator; const_iterator = std::_Rb_tree<long long int, std::pair<const long long int, std::vector<long long int> >, std::_Select1st<std::pair<const long long int, std::vector<long long int> > >, std::less<long long int>, std::allocator<std::pair<const long long int, std::vector<long long int> > > >::const_iterator]'
 2460 |         _Auto_node __z(*this, std::forward<_Args>(__args)...);
      |                    ^~~
/usr/include/c++/14/bits/stl_map.h:640:38:   required from 'std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::emplace_hint(const_iterator, _Args&& ...) [with _Args = {int&, std::vector<int, std::allocator<int> >&}; _Key = long long int; _Tp = std::vector<long long int>; _Compare = std::less<long long int>; _Alloc = std::allocator<std::pair<const long long int, std::vector<long long int> > >; iterator = std::_Rb_tree<long long int, std::pair<const long long int, std::vector<long long int> >, std::_Select1st<std::pair<const long long int, std::vector<long long int> > >, std::less<long long int>, std::allocator<std::pair<const long long int, std::vector<long long int> > > >::iterator; const_iterator = std::_Rb_tree<long long int, std::pair<const long long int, std::vector<long long int> >, std::_Select1st<std::pair<const long long int, std::vector<long long int> > >, std::less<long long int>, std::allocator<std::pair<const long long int, std::vector<long long int> > > >::const_iterator]'
  640 |           return _M_t._M_emplace_hint_unique(__pos,
      |                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
  641 |                                              std::forward<_Args>(__args)...);
      |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:15:35:   required from here
   15 |         _combinations.emplace_hint(_combinations.end(), n, vec);
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to 'std::pair<const long long int, std::vector<long long int> >::pair(int&, std::vector<int>&)'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_pair.h:913:28: note: candidate: 'template<class _U1, class _U2, typename std::enable_if<(std::_PCC<((! std::is_same<const long long int, _U1>::value) || (! std::is_same<std::vector<long long int>, _U2>::value)), const long long int, std::vector<long long int> >::_MoveConstructiblePair<_U1, _U2>() && (! std::_PCC<((! std::is_same<const long long int, _U1>::value) || (! std::is_same<std::vector<long long int>, _U2>::value)), const long long int, std::vector<long long int> >::_ImplicitlyMoveConvertiblePair<_U1, _U2>())), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&) [with _U2 = _U1; typename std::enable_if<(std::_PCC<((! std::is_same<_T1, _U1>::value) || (! std::is_same<_T2, _U2>::value)), _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && (! std::_PCC<((! std::is_same<_T1, _U1>::value) || (! std::is_same<_T2, _U2>::value)), _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>())), bool>::type <anonymous> = _U2; _T1 = const long long int; _T2 = std::vector<long long int>]'
  913 |         explicit constexpr pair(pair<_U1, _U2>&& __p)
      |                            ^~~~
/usr/include/c++/14/bits/stl_pair.h:913:28: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_pair.h:902:19: note: candidate: 'template<class _U1, class _U2, typename std::enable_if<(std::_PCC<((! std::is_same<const long long int, _U1>::value) || (! std::is_same<std::vector<long long int>, _U2>::value)), const long long int, std::vector<long long int> >::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<((! std::is_same<const long long int, _U1>::value) || (! std::is_same<std::vector<long long int>, _U2>::value)), const long long int, std::vector<long long int> >::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&) [with _U2 = _U1; typename std::enable_if<(std::_PCC<((! std::is_same<_T1, _U1>::value) || (! std::is_same<_T2, _U2>::value)), _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && std::_PCC<((! std::is_same<_T1, _U1>::value) || (! std::is_same<_T2, _U2>::value)), _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>()), bool>::type <anonymous> = _U2; _T1 = const long long int; _T2 = std::vector<long long int>]'
  902 |         constexpr pair(pair<_U1, _U2>&& __p)
      |                   ^~~~
/usr/include/c++/14/bits/stl_pair.h:902:19: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_pair.h:891:28: note: candidate: 'template<class _U1, class _U2, typename std::enable_if<(_MoveConstructiblePair<_U1, _U2>() && (! _ImplicitlyMoveConvertiblePair<_U1, _U2>())), bool>::type <anonymous> > constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U2 = _U1; typename std::enable_if<(std::_PCC<true, _T1, _T2>::_MoveConstructiblePair<_U1, _U2>() && (! std::_PCC<true, _T1, _T2>::_ImplicitlyMoveConvertiblePair<_U1, _U2>())), bool