Language: C++

#include <bits/stdc++.h>

using namespace std;

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
    os << '{';
    string sep;
    for (const auto &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr)
{
    os << '{';
    string sep;
    for (const auto &x : arr)
        os << sep << x, sep = ", ";
    return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}

// #define DEBUG
#ifdef DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

int main()
{
    ios::sync_with_stdio(false);
#ifndef DEBUG
    cin.tie(nullptr);
#endif
    int64_t N;
    int X, M;
    cin >> N >> X >> M;

    vector<int> v;
    unordered_map<int, int> index;
    v.push_back(X);
    index[X] = 0;
    int cycleStart = -1, cycleLen = -1;
    int64_t cycleSum = 0;
    for (int i = 1;; i++)
    {
        int next = (1LL * v[i - 1] * v[i - 1]) % M;
        auto it = index.find(next);
        if (it == index.end())
        {
            v.push_back(next);
            index[next] = i;
        }
        else
        {
            cycleStart = index[next];
            cycleLen = i - cycleStart;
            for (int j = cycleStart; j < i; j++)
                cycleSum += v[j];
            break;
        }
    }
    dbg(v);
    int64_t sum = 0;
    for (int i = 0; i < min(1LL * cycleStart, N); i++)
    {
        sum += v[i];
    }
    if (N >= cycleStart)
    {
        int64_t left = N - cycleStart;
        sum += (left / cycleLen) * cycleSum;
        for (int i = 0; i < left % cycleLen; i++)
        {
            sum += v[cycleStart + i];
        }
    }
    cout << sum << endl;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:77:28: error: no matching function for call to 'min(long long int, int64_t&)'
   77 |     for (int i = 0; i < min(1LL * cycleStart, N); i++)
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:77:28: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int64_t' {aka 'long int'})
   77 |     for (int i = 0; i < min(1LL * cycleStart, N); i++)
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:77:28: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   77 |     for (int i = 0; i < min(1LL * cycleStart, N); i++)
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~