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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
vector<ll> adj[1000005];
ll expo(ll b, ll n, ll m) {
  ll r, p;
  p = 1;
  b = b % m;
  while (n > 0) {
    r = n % 2;
    if (r == 1)
      p = ((p % m) * (b % m)) % m;
    b = ((b % m) * (b % m)) % m;
    n = n / 2;
  }
  return (p % m);
}
ll divide1(ll a, ll b, ll m) {
  ll r, p, n;
  p = a % m;
  n = m - 2;
  while (n > 0) {
    r = n % 2;
    if (r == 1)
      p = ((p % m) * (b % m)) % m;
    b = ((b % m) * (b % m)) % m;
    n = n / 2;
  }
  return (p % m);
}
bool check(ll x, ll n) {
  while (n >= x && n % x == 0) {
    n /= x;
  }
  return n % x == 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  ll n1;
  cin >> n1;
  for (ll n = n1; n <= n1; n++) {
    ll ans = 0;
    for (ll i = 2; i * i <= n; i++) {
      if (n % i > 0) {
        continue;
      }
      ans += (check(i, n));
      if (i != n / i) {
        ans += (check(n / i, n));
      }
    }
    ans += check(n, n);
    --n;
    for (ll i = 1; i * i <= n; i++) {
      if (n % i > 0) {
        continue;
      }
      if (i > 1)
        ans++;
      if (i != n / i && n / i > 1) {
        ans++;
      }
    }
    ++n;
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
vector<ll> adj[1000005];
ll expo(ll b, ll n, ll m) {
  ll r, p;
  p = 1;
  b = b % m;
  while (n > 0) {
    r = n % 2;
    if (r == 1)
      p = ((p % m) * (b % m)) % m;
    b = ((b % m) * (b % m)) % m;
    n = n / 2;
  }
  return (p % m);
}
ll divide1(ll a, ll b, ll m) {
  ll r, p, n;
  p = a % m;
  n = m - 2;
  while (n > 0) {
    r = n % 2;
    if (r == 1)
      p = ((p % m) * (b % m)) % m;
    b = ((b % m) * (b % m)) % m;
    n = n / 2;
  }
  return (p % m);
}
bool check(ll x, ll n) {
  while (n >= x) {
    n /= x;
  }
  return n == 1;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  ll n, ans = 0;
  cin >> n;
  for (int i = 2; i <= n; ++i) {
    if (n % i == 0) {
      ans += check(i, n);
    } else {
      ll t = n;
      while (t >= i) {
        t -= i;
      }
      ans += (t == 1);
    }
  }
  cout << ans << endl;
  return 0;
}