Language: C++

#include<iostream>
#include<map>
#include<vector>

int main(){
  int n,m;
  std::cin >> n >> m;
  std::vector<int> x(n);
  std::map<int,std::map<int,int> > units;
  std::map<int,std::map<int,int> > buf;
  std::vector<int> oddExist(m);
  for(int i=0;i<n;i++){
    std::cin >> x[i];
    if(buf[x[i]%m].find(x[i])==buf[x[i]%m].end()){
      if(units[x[i]%m].find(x[i])!=units[x[i]%m].end()){
        buf[x[i]%m][x[i]] = 2;
        units[x[i]%m].erase(x[i]);
      }else{
        units[x[i]%m][x[i]] = 1;
      }
    }else{
      buf[x[i]%m][x[i]]++;
    }
  }
  /*for(int i=0;i<m;i++){
    std::cout << "[" << i << "]";
    auto u = buf[i];
    for(auto it=u.begin();it!=u.end();it++){
      std::cout << "(" << it->first << "," << it->second << ")";
    }
    std::cout << std::endl;
  }*/
  int count = 0;
  for(int i=0;i<m;i++){
    if(units.find(i)==units.end())continue;
    auto &u = units[i];
    if(i==0 || i*2==m){
      count += u.size()/2;
      if(u.size()%2==1){
        buf[i][0]++;
      }
    }else if(units.find(m-i)!=units.end()){
      auto &v = units[m-i];
      count += std::min(u.size(),v.size());
      if(u.size() > v.size()){
        buf[i][0]+=u.size()-v.size();
      }else if(u.size() < v.size()){
        buf[m-i][0]+=v.size()-u.size();
      }
      units.erase(i);
      units.erase(m-i);
    }else{
      buf[i][0]+=u.size();
    }
  }
  for(int i=0;i<m;i++){
    oddExist[i] = false;
    if(buf.find(i)==buf.end())continue;
    auto &u = buf[i];
    if(u.find(0)!=u.end()){
      oddExist[i] = true;
      continue;
    }
    for(auto it=u.begin();it!=u.end();it++){
      if(it->second%2==1){
        oddExist[i] = true;
        break;
      }
    }
  }
  /*std::cout << count << std::endl;
  for(int i=0;i<m;i++){
    std::cout << "[" << i << "]";
    auto u = buf[i];
    for(auto it=u.begin();it!=u.end();it++){
      std::cout << "(" << it->first << "," << it->second << ")";
    }
    std::cout << std::endl;
  }*/
  while(1){
    bool update = false;
    for(auto pt=buf.begin();pt!=buf.end();pt++){
      int i = pt->first;
      auto &u = pt->second;
      if(i==0 || i*2==m){
        if(oddExist[i])for(auto it=u.begin();it!=u.end();it++){
          if(it->second%2==1 || it->first==0){
            bool po = false;
            for(auto jt=it;jt!=u.end();jt++){
              if(it!=jt && (jt->second%2==1 || jt->first==0)){
                it->second--;
                jt->second--;
                count++;
                update = po = true;
                if(jt->second==0){
                  u.erase(jt);
                }
                break;
              }
            }
            if(!po){
              for(auto jt=u.begin();jt!=u.end();jt++){
                if(it==jt)continue;
                it->second--;
                jt->second--;
                count++;
                update = po = true;
                if(jt->second==0){
                  u.erase(jt);
                }
                break;
              }
            }
            if(po){
              if(it->second==0){
                it = u.erase(it);
                if(it==u.end())break;
                else it--;
              }
            }
          }
        }
      }else{
        int j = m-i;
        if(buf.find(j)==buf.end())continue;
        auto &v = buf[j];
        if(oddExist[i])for(auto it=u.begin();it!=u.end();it++){
          if(it->second%2==1 || it->first==0){
            bool po = false;
            if(oddExist[j])for(auto jt=v.begin();jt!=v.end();jt++){
              if(jt > v.begin()+100)break;
              if(jt->second%2==1 || jt->first==0){
                it->second--;
                jt->second--;
                count++;
                update = po = true;
                if(jt->second==0){
                  v.erase(jt);
                }
                break;
              }
            }
            if(!po){
              for(auto jt=v.begin();jt!=v.end();jt++){
                it->second--;
                jt->second--;
                count++;
                update = po = true;
                if(jt->second==0){
                  v.erase(jt);
                }else oddExist[j] = true;
                break;
              }
            }
            if(po){
              if(it->second==0){
                it = u.erase(it);
                if(it==u.end())break;
                else it--;
              }
            }
          }
        }
      }
      if(u.size()==0){
        pt = buf.erase(pt);
        if(pt==buf.end())break;
        else pt--;
      }
    }
    if(!update)break;
  }
  for(auto pt=buf.begin();pt!=buf.end();pt++){
    int i = pt->first;
    auto &u = pt->second;
    for(auto it=u.begin();it!=u.end();it++){
      if(it->first!=0)count += it->second/2;
    }
  }
  std::cout << count << std::endl;
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:131:32: error: no match for 'operator+' (operand types are 'std::map<int, int>::iterator' {aka 'std::_Rb_tree<int, std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >, std::less<int>, std::allocator<std::pair<const int, int> > >::iterator'} and 'int')
  131 |               if(jt > v.begin()+100)break;
      |                       ~~~~~~~~~^~~~
      |                              |  |
      |                              |  int
      |                              std::map<int, int>::iterator {aka std::_Rb_tree<int, std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >, std::less<int>, std::allocator<std::pair<const int, int> > >::iterator}
In file included from /usr/include/c++/14/string:48,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator.h:627:5: note: candidate: 'template<class _Iterator> constexpr std::reverse_iterator<_Iterator> std::operator+(typename reverse_iterator<_Iterator>::difference_type, const reverse_iterator<_Iterator>&)'
  627 |     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:627:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   mismatched types 'const std::reverse_iterator<_Iterator>' and 'int'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note: candidate: 'template<class _Iterator> constexpr std::move_iterator<_IteratorL> std::operator+(typename move_iterator<_IteratorL>::difference_type, const move_iterator<_IteratorL>&)'
 1798 |     operator+(typename move_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   mismatched types 'const std::move_iterator<_IteratorL>' and 'int'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
In file included from /usr/include/c++/14/string:54:
/usr/include/c++/14/bits/basic_string.h:3598:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3598 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3598:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   'std::map<int, int>::iterator' {aka 'std::_Rb_tree<int, std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >, std::less<int>, std::allocator<std::pair<const int, int> > >::iterator'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/basic_string.h:3616:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3616 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3616:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   mismatched types 'const _CharT*' and 'std::_Rb_tree_iterator<std::pair<const int, int> >'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/basic_string.h:3635:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3635 |     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3635:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'int'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/basic_string.h:3652:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)'
 3652 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3652:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   'std::map<int, int>::iterator' {aka 'std::_Rb_tree<int, std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >, std::less<int>, std::allocator<std::pair<const int, int> > >::iterator'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/basic_string.h:3670:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 3670 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3670:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   'std::map<int, int>::iterator' {aka 'std::_Rb_tree<int, std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >, std::less<int>, std::allocator<std::pair<const int, int> > >::iterator'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/basic_string.h:3682:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3682 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3682:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   'std::map<int, int>::iterator' {aka 'std::_Rb_tree<int, std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >, std::less<int>, std::allocator<std::pair<const int, int> > >::iterator'} is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/basic_string.h:3689:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3689 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3689:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   'std::map<int, int>::iterator' {aka 'std::_Rb_tree<int, std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >, std::less<int>, std::allocator<std::pair<const int, int> > >::iterator'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/basic_string.h:3696:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3696 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3696:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   'std::map<int, int>::iterator' {aka 'std::_Rb_tree<int, std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >, std::less<int>, std::allocator<std::pair<const int, int> > >::iterator'} is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/basic_string.h:3719:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3719 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3719:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   mismatched types 'const _CharT*' and 'std::_Rb_tree_iterator<std::pair<const int, int> >'
  131 |               if(jt > v.begin()+100)break;
      |                                 ^~~
/usr/include/c++/14/bits/basic_string.h:3726:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3726 |     operator+(_CharT __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3726:5: note:   template argument deduction/substitution failed:
a.cc:131:33: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'int'
  131 |               if(jt > v.begin()+