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

// By Mohit Gupta
#include <bits/stdc++.h>
using namespace std;
#define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " "))
#define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " "))
#define forstl(i, v) for (auto &i : v)
#define forn(i, e) for (int i = 0; i < e; i++)
#define forsn(i, s, e) for (int i = s; i < e; i++)
#define rforn(i, s) for (int i = s; i >= 0; i--)
#define rforsn(i, s, e) for (int i = s; i >= e; i--)
#define clz(a) __builtin_clz(a) // count leading zeroes
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \\
  __builtin_popcount(a) // count set bits (for ints only diff for ll)
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built
#define ln "\\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int, int> m32;
#pragma GCC optimize("-O2")
const int LIM = 5e5 + 5, MOD = 998244353;
int t, n, m, k, x, y;

bool check(int k) {
  int x = n;
  while (x % k == 0) {
    x /= k;
  }
  if (x % k == 1)
    return true;
  else
    return false;
}

int32_t main() {
  IOS;
  cin >> n;
  // set<int> factors;
  set<int> works;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (check(i) && i > 1 && i <= n)
        works.insert(i);
      if (check(n / i))
        works.insert(n / i);
    }
  }
  works.insert(n);

  for (int i = 2; i <= sqrt(n - 1); i++) {
    if ((n - 1) % i == 0) {
      // if(check(i)) works.insert(i);
      // if(check((n-1)/i)) works.insert((n-1)/i);
      if (i > 1 && i <= n && n % i == 1)
        works.insert(i);
      int f = (n - 1) / i;
      if (f > 1 && f <= n && n % f == 1)
        works.insert(f);
    }
  }
  if (n > 2)
    works.insert(n - 1);
  // int x=2;
  // while(x<=n){
  // 	if(check(x)) works.insert(x);
  // 	x*=2;
  // }
  // if(check(n/2)) works.insert(n/2);
  cout << works.size() << ln;
}


Error: TLE

Fixed submission:

// By Mohit Gupta
#include <bits/stdc++.h>
using namespace std;
#define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " "))
#define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " "))
#define forstl(i, v) for (auto &i : v)
#define forn(i, e) for (int i = 0; i < e; i++)
#define forsn(i, s, e) for (int i = s; i < e; i++)
#define rforn(i, s) for (int i = s; i >= 0; i--)
#define rforsn(i, s, e) for (int i = s; i >= e; i--)
#define clz(a) __builtin_clz(a) // count leading zeroes
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \\
  __builtin_popcount(a) // count set bits (for ints only diff for ll)
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built
#define ln "\\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int, int> m32;
#pragma GCC optimize("-O2")
const int LIM = 5e5 + 5, MOD = 998244353;
int t, n, m, k, x, y;

bool check(int k) {
  int x = n;
  while (x % k == 0) {
    x /= k;
  }
  if (x % k == 1)
    return true;
  else
    return false;
}

int32_t main() {
  IOS;
  cin >> n;
  // set<int> factors;
  set<int> works;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (check(i))
        works.insert(i);
      if (check(n / i))
        works.insert(n / i);
    }
  }
  works.insert(n);

  for (int i = 2; i <= sqrt(n - 1); i++) {
    if ((n - 1) % i == 0) {
      if (check(i))
        works.insert(i);
      if (check((n - 1) / i))
        works.insert((n - 1) / i);
    }
  }
  works.insert(n - 1);
  int x = 2;
  while (x <= n) {
    if (check(x))
      works.insert(x);
    x *= 2;
  }
  if (check(n / 2))
    works.insert(n / 2);
  cout << works.size() << ln;
}