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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll m = 1000000007;

ll geom(ll n, ll r, ll mod = m) {
  ll T = 1, x = r % mod;
  ll total = 0;
  while (n) {
    if (n & 1) {
      total = (x * total + T) % mod;
    }
    T = ((x + 1) * T) % mod;
    x = (x * x) % mod;
    n = n / 2;
  }
  return total;
}

ll mul(ll a, ll b, ll mod = m) {
  ll r = 0;
  a %= mod;
  while (b) {
    if (b & 1) {
      r = (r + a) % mod;
    }
    a = (a + a) % mod;
    b /= 2;
  }
  return r;
}

ll power(ll x, ll n, ll mod = m) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    n = n / 2;
    x = (x * x) % mod;
  }
  return res;
}

ll power2(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x);
    n = n / 2;
    x = (x * x);
  }
  return res;
}

bool is_prime(long long x) {
  if (x == 1 || x == 0)
    return false;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

ll totient(ll n, vector<ll> primes) {
  ll res = n;
  for (ll i = 0; primes[i] * primes[i] <= n; i++) {
    if (n % primes[i] == 0) {
      res -= (res / primes[i]);
      while (n % primes[i] == 0) {
        n /= primes[i];
      }
    }
  }
  if (n > 1) {
    res -= (res / n);
  }
  return res;
}

void sieve(vector<ll> &primes, vector<bool> &prime, ll lim) {
  for (int i = 2; i * i <= lim; i++) {
    if (prime[i]) {
      for (int j = 2; j * i <= lim; j++) {
        prime[j * i] = false;
      }
    }
  }
  for (int i = 2; i <= lim; i++) {
    if (prime[i]) {
      primes.push_back(i);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  ll n;
  cin >> n;
  ll count = (n >= 3 ? 2 : (n >= 2 ? 1 : 0));
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 1) {
      count++;
      if (i * i != (n - 1)) {
        count++;
      }
    } else if (n % i == 0) {
      ll x = n;
      while (x % i == 0) {
        x /= i;
      }
      if (x % i == 1) {
        count++;
      }
      if (n / (n / i) > n / i) {
        x = n;
        while (x % (n / i) == 0) {
          x /= (n / i);
        }
        if (x % (n / i) == 1) {
          count++;
        }
      }
    }
  }
  cout << count << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

bool is_prime(long long x) {
  if (x == 1 || x == 0)
    return false;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  ll n;
  cin >> n;
  ll count = (n >= 3 ? 2 : 1);
  ll dividend = (is_prime(n - 1) ? n : n - 1);
  if (dividend == n - 1) {
    for (ll i = 2; i * i <= n; i++) {
      if (dividend % i == 0) {
        count++;
        if (i * i != dividend) {
          count++;
        }
      } else if (n % i == 0) {
        ll x = n;
        while (x % i == 0) {
          x /= i;
        }
        if (x % i == 1) {
          count++;
        }
      }
    }
  } else {
    for (ll i = 2; i * i <= dividend; i++) {
      if (dividend % i == 0) {
        ll x = dividend;
        while (x % i == 0) {
          x /= i;
        }
        if (x % i == 1) {
          count++;
        }
        if (i * i != dividend) {
          x = dividend;
          while (x % (dividend / i) == 0) {
            x /= (dividend / i);
          }
          if (x % (dividend / i) == 0) {
            count++;
          }
        }
      }
    }
  }
  if (n <= 1) {
    cout << 0 << "\\n";
  } else {
    cout << count << "\\n";
  }
  return 0;
}