Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, m, n) for (ll i = m; i > n; i--)
#define Rep(i, m, n) for (ll i = m; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define lmax(x, y) max<ll>(x, y)
#define lmin(x, y) min<ll>(x, y)
#define tmax(x, y, z) lmax((x), lmax((y), (z)))
#define tmin(x, y, z) lmin((x), lmin((y), (z)))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define ZERO(a) memset(a, 0, sizeof(a))
const ll INF = 1LL << 60;
const ll MM = 1000000000;
const ll MOD = MM + 7;
const ll MMM = 9223372036854775807; // 2^63 -1
#define ADD(a, b) a = (a + ll(b)) % MOD
#define MUL(a, b) a = (a * ll(b)) % MOD
ll GCD(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return GCD(y, x % y);
}
ll LCM(ll x, ll y) { return x / GCD(x, y) * y; }
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;
}
template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
// struct edge{ ll from, to, cost; };
// struct edge{ ll to, cost; };
// using Graph = vector<vector<ll>>;  // 重み無し
// using Graph = vector<edge>;  // 辺
// using Graph = vector<vector<edge>>;  // 重み付き
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll sum = 0, more = 0, bef = 0, shu = 0, bef_long = 0, shu_long = 0;
  // set<ll> s;
  vector<ll> shu_v, A(100000);
  map<ll, ll> mp;
  bool found = false, shuuki = false;
  Rep(i, 1, n + 1) {
    if (i == 1)
      A[i - 1] = x;
    else
      A[i - 1] = A[i - 2] * A[i - 2] % m;
    ll tmp = A[i - 1];
    mp[tmp]++;
    if (mp[tmp] == 2) {
      if (!shuuki)
        bef_long = i - 1;
      shuuki = true;
      shu += tmp;
      sum += tmp;
      shu_v.push_back(tmp);
    } else if (mp[tmp] == 3) {
      shu_long = i - bef_long - 1;
      found = true;
      break;
    } else {
      sum += tmp;
      bef += tmp;
    }
  }
  if (mp.count(0)) {
    cout << sum << endl;
    return 0;
  }
  if (!found) {
    cout << sum << endl;
    return 0;
  }
  rep(i, (n - bef_long) % shu_long) { more += shu_v[i]; }
  cout << bef + shu * ((n - bef_long) / shu_long) + more << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, m, n) for (ll i = m; i > n; i--)
#define Rep(i, m, n) for (ll i = m; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define lmax(x, y) max<ll>(x, y)
#define lmin(x, y) min<ll>(x, y)
#define tmax(x, y, z) lmax((x), lmax((y), (z)))
#define tmin(x, y, z) lmin((x), lmin((y), (z)))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define ZERO(a) memset(a, 0, sizeof(a))
const ll INF = 1LL << 60;
const ll MM = 1000000000;
const ll MOD = MM + 7;
const ll MMM = 9223372036854775807; // 2^63 -1
#define ADD(a, b) a = (a + ll(b)) % MOD
#define MUL(a, b) a = (a * ll(b)) % MOD
ll GCD(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return GCD(y, x % y);
}
ll LCM(ll x, ll y) { return x / GCD(x, y) * y; }
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;
}
template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
// struct edge{ ll from, to, cost; };
// struct edge{ ll to, cost; };
// using Graph = vector<vector<ll>>;  // 重み無し
// using Graph = vector<edge>;  // 辺
// using Graph = vector<vector<edge>>;  // 重み付き
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  ll ans = 0;
  Rep(i, 1, n + 1) {
    ll tmp = modpow(x, pow(2, i - 1), m);
    if (tmp == 0) {
      cout << ans << endl;
      return 0;
    }
    if (tmp == 1) {
      cout << ans + n - i + 1 << endl;
      // cout << "1_out" << endl;
      return 0;
    }
    ans += tmp;
  }
  cout << ans << endl;
}