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

#include <bits/stdc++.h>
#define endl "\\n"
#define pb push_back
#define ll long long
#define d1(x) cerr << #x << "--> " << x << endl
#define d2(x, y) cerr << #x << "--> " << x << " | " << #y << "--> " << y << endl
#define d3(x, y, z)                                                            \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << endl
#define d4(x, y, z, w)                                                         \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << " | " << #w << "--> " << w << endl
#define vpll vector<pair<ll, ll>>
#define F first
#define S second
#define T                                                                      \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  while (tt--)
using namespace std;
ll mode = 1ll << 32;
const ll maxn = 1e3 + 10;
ll __lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getRand(ll l, ll r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

void solve() {

  ll n;
  cin >> n;

  if (n == 2) {
    cout << 1 << endl;
    return;
  }

  ll x = n - 1, ans = 0;
  for (ll i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      if ((x / i) == i)
        ans++;
      else
        ans += 2;
    }
  }

  ans += 2;

  vector<ll> v;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      if ((n / i) == i)
        v.pb(i);
      else {
        v.pb(i);
        v.pb(n / i);
      }
    }
  }

  for (auto i : v) {
    ll prev = ans;
    ll z = n;
    while (z % i == 0)
      z = z / i;

    if (z == 1 || (z - 1) % i == 0)
      ans++;
    if (ans == prev) {
      ll xx = 0;
      ll l = 0, r = z / i;
      while (l <= r) {
        ll m = (l + r) / 2;
        if ((z - m * i) >= 1) {
          xx = m;
          l = m + 1;
        } else {
          r = m - 1;
        }
      }
      // d2(i,z);
      z = z - xx * i;

      while (z % i == 0)
        z = z / i;
      if (z == 1 || (z - 1) % i == 0)
        ans++;
    }
  }

  cout << ans << endl;

  return;
}

int main() {

#ifndef ONLINE_JUDGE
  //	freopen("input.txt", "r", stdin);
#endif
  srand(time(0));

  ios::sync_with_stdio(0);
  cin.tie(0);

  // T{ solve(); }
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
#define pb push_back
#define ll long long
#define d1(x) cerr << #x << "--> " << x << endl
#define d2(x, y) cerr << #x << "--> " << x << " | " << #y << "--> " << y << endl
#define d3(x, y, z)                                                            \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << endl
#define d4(x, y, z, w)                                                         \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << " | " << #w << "--> " << w << endl
#define vpll vector<pair<ll, ll>>
#define F first
#define S second
#define T                                                                      \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  while (tt--)
using namespace std;
ll mode = 1ll << 32;
const ll maxn = 1e3 + 10;
ll __lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getRand(ll l, ll r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

void solve() {

  ll n;
  cin >> n;

  if (n == 2) {
    cout << 1 << endl;
    return;
  }

  ll x = n - 1, ans = 0;
  for (ll i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      if ((x / i) == i)
        ans++;
      else
        ans += 2;
    }
  }

  ans += 2;

  vector<ll> v;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      if ((n / i) == i)
        v.pb(i);
      else {
        v.pb(i);
        v.pb(n / i);
      }
    }
  }

  for (auto i : v) {
    ll prev = ans;
    ll z = n;
    while (z % i == 0)
      z = z / i;
    if (z == 1 || (z - 1) % i == 0)
      ans++;
    if (ans == prev) {
      while ((z - i) >= 1) {
        z -= i;
      }
      while (z % i == 0)
        z = z / i;
      if (z == 1 || (z - 1) % i == 0)
        ans++;
    }
  }

  cout << ans << endl;

  return;
}

int main() {

#ifndef ONLINE_JUDGE
  //	freopen("input.txt", "r", stdin);
#endif
  srand(time(0));

  ios::sync_with_stdio(0);
  cin.tie(0);

  // T{ solve(); }
  solve();

  return 0;
}