Language: C++

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

struct Edge {
	ll to;
    ll cost;
};

struct edge {
    ll from;
    ll to;
    ll cost;
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define mp make_pair
#define pb push_back
/***** define constant start *******/
const int MAX = 10000000;
const ll MOD = 998244353;
const double PI = 3.1415926535897932;
const ll INF = 1LL << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/****** define constant end ********/

template <class T>
inline bool chmin(T &a, T b){
    if (a > b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b){
    if (a < b){
        a = b;
        return true;
    }
    return false;
}

void debug_out() { std::cout << std::endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cout << H << " ";
    debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cout << "debug: "; debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (long long i = (long long)(a); i< (long long)(n); i++)
#define RRep(i, a, n) for (long long i = ((long long)(n-1ll)); i>=(long long)(a); i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}

// 負の数にも対応した % 演算
long long mod(long long val, long long m) {
    long long res = val % m;
    if (res < 0)
        res += m;
    return res;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1)
        res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
	    a -= t * b;
        swap(a, b);
	    u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0)
        u += m;
    return u;
}
/****** define variable start ******/
ll n, x, m;
/******* define variable end *******/
int solve() {
	cin >> n >> x >> m;
	ll loop_start = -1;
	ll before_sum = x;
	ll tmp = x;
	map<ll, bool> ma;
	ma[x] = true;
	REP(i, n){
		if(ma[((tmp * tmp) % m)]){
			tmp = tmp * tmp % m;
			loop_start = i;
			break;
		}
		before_sum += tmp * tmp % m;
		tmp = tmp * tmp % m;
		ma[tmp] = true;
	}
	ll loop_num = -1;
	ll loop_sum = tmp;
	map<ll, bool> ma2;
	ma2[tmp] = true;
	REP(i, n){
		if(ma2[((tmp * tmp) % m)]){
			loop_num = i;
			tmp = tmp * tmp % m;
			break;
		}
		loop_sum += tmp * tmp % m;
		tmp = tmp * tmp % m;
		ma2[tmp] = true;
	}
	loop_start -= loop_num;
	before_sum -= loop_sum;
	loop_num++;

	debug(loop_start, before_sum, loop_num, loop_sum, tmp);

	if(loop_start > n || loop_start < 0){
		ll sum = x;
		ll tmp2 = x;
		REP(i, n-1){
			sum += tmp2 * tmp2 % m;
			tmp2 = tmp2 * tmp2 % m;
		}
		cout << sum << endl;
		return 0;
	}
	ll ans = before_sum + (n-loop_start) / loop_num * loop_sum;
	REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num))){
		ans += tmp;
		tmp = tmp * tmp % m;
	}
	cout << ans << endl;
    return 0;
}

int main() {
	// clock_t start = clock();
	cout << std::fixed << std::setprecision(10);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int end_solve = solve();
	/*
	clock_t end = clock();
	const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
	1000.0; printf("time %lf[ms]\n", time);
	*/

	return end_solve;
}


Compiler output:
a.cc: In function 'int solve()':
a.cc:167:19: error: no matching function for call to 'max(int, ll)'
  167 |         REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num))){
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:26:44: note: in definition of macro 'FOR'
   26 | #define FOR(i, a, b) for (ll i = (a); i < (b); i++)
      |                                            ^
a.cc:167:9: note: in expansion of macro 'REP'
  167 |         REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num))){
      |         ^~~
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:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:167:19: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
  167 |         REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num))){
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:26:44: note: in definition of macro 'FOR'
   26 | #define FOR(i, a, b) for (ll i = (a); i < (b); i++)
      |                                            ^
a.cc:167:9: note: in expansion of macro 'REP'
  167 |         REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num))){
      |         ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303: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:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:167:19: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  167 |         REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num))){
      |                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:26:44: note: in definition of macro 'FOR'
   26 | #define FOR(i, a, b) for (ll i = (a); i < (b); i++)
      |                                            ^
a.cc:167:9: note: in expansion of macro 'REP'
  167 |         REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num))){
      |         ^~~