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

#include <bits/stdc++.h>
using namespace std;

#define mainspring cout << "mainspring\\n";
#define barrel cout << "barrel\\n";
#define escapement cout << "escapement\\n";

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
#define fs first
#define sd second
#define tik(i, a, b) for (int i = a; i < b; i++)
#define tok(i, a, b) for (int i = a; i >= b; i--)
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> pi;
typedef pair<pi, int> ppi;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << " ";
  err(++it, args...);
}

ll pow(ll x, ll y) {
  if (y == 0)
    return 1;
  ll temp = pow(x, y / 2);
  temp = (temp * temp);
  if (y % 2 == 1)
    temp *= x;
  return temp;
}

#define int ll

void solve(ll n) {
  if (n == 2) {
    cout << 1;
    return;
  }
  vi div, div2;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      div.pb(i);
      if (i != n / i)
        div.pb(n / i);
    }
  }
  for (ll i = 2; i * i <= n - 1; i++) {
    if ((n - 1) % i == 0) {
      div2.pb(i);
      if (i != (n - 1) / i)
        div2.pb((n - 1) / i);
    }
  }
  ll res = 0;
  for (ll x : div) {
    ll tmp = n;
    while (tmp % x == 0)
      tmp /= x;
    res += (tmp % x == 1);
  }
  res += sz(div2);
  cout << res + 2;
}

int32_t main() {
  ll n;
  cin >> n;
  solve(n);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define mainspring cout << "mainspring\\n";
#define barrel cout << "barrel\\n";
#define escapement cout << "escapement\\n";

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
#define fs first
#define sd second
#define tik(i, a, b) for (int i = a; i < b; i++)
#define tok(i, a, b) for (int i = a; i >= b; i--)
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> pi;
typedef pair<pi, int> ppi;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << " ";
  err(++it, args...);
}

ll pow(ll x, ll y) {
  if (y == 0)
    return 1;
  ll temp = pow(x, y / 2);
  temp = (temp * temp);
  if (y % 2 == 1)
    temp *= x;
  return temp;
}

int main() {
  ll n;
  cin >> n;
  ll res = 0;
  for (ll i = 2; i <= n; i++) {
    bool ok = 1;
    ll tmp = n;
    while (tmp % i == 0)
      tmp /= i;
    res += (tmp % i == 1);
  }
  cout << res;
}