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

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target(                                                            \\
    "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
// #pragma comment(linker, "/stack:200000000"]

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define hm unordered_map
#define pii pair<int, int>
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define cinv(v)                                                                \\
  for (auto &x : v)                                                            \\
  cin >> x
#define fr(i, n) for (int i = 0; i < n; ++i)
#define fl(i, l, n) for (int i = l; i < n; ++i)

#define int ll

using namespace std;

#ifdef __LOCAL
#define dbg(x) cerr << #x << " : " << x << '\\n'
const int maxn = 20;
#else
#define dbg(x)
const int maxn = 2e5 + 20;
#endif

// tg: @galebickosikasa

const ll inf = (ll)2e9;
const ld pi = 3.14159265358979323846264;
const ld eps = 1e-8;
const ll mod = (ll)1e9 + 7;
const ll ns = 97;

random_device rd;
mt19937 SuperRandom(rd());

int ans = 2;
unordered_set<int> a;

void kek(int x) {
  for (int i = 1; i * i <= x; ++i) {
    if (x % i == 0)
      a.insert(i), a.insert(x / i);
  }
}

void kek1(int x) {
  for (int i = 2; i * i <= x; ++i) {
    int y = x;
    while (y % i == 0)
      y /= i;
    if (y % i == 1)
      a.insert(i);
    // if (x % i == 0 && (x / i - 1) % i == 1) {
    // 	a.insert (i);
    // }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int x;
  cin >> x;
  kek(x - 1);
  kek1(x);
  a.insert(x);
  a.erase(1);
  for (auto &x : a)
    dbg(x);
  cout << sz(a);
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target(                                                            \\
    "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
// #pragma comment(linker, "/stack:200000000"]

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define hm unordered_map
#define pii pair<int, int>
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define cinv(v)                                                                \\
  for (auto &x : v)                                                            \\
  cin >> x
#define fr(i, n) for (int i = 0; i < n; ++i)
#define fl(i, l, n) for (int i = l; i < n; ++i)

#define int ll

using namespace std;

#ifdef __LOCAL
#define dbg(x) cerr << #x << " : " << x << '\\n'
const int maxn = 20;
#else
#define dbg(x)
const int maxn = 2e5 + 20;
#endif

// tg: @galebickosikasa

const ll inf = (ll)2e9;
const ld pi = 3.14159265358979323846264;
const ld eps = 1e-8;
const ll mod = (ll)1e9 + 7;
const ll ns = 97;

random_device rd;
mt19937 SuperRandom(rd());

int ans = 2;
unordered_set<int> a;

void kek(int x) {
  for (int i = 1; i * i <= x; ++i) {
    if (x % i == 0)
      a.insert(i), a.insert(x / i);
    while (x % i == 0)
      x /= i;
  }
  if (x > 1)
    a.insert(x);
}

void kek1(int x) {
  for (int i = 1; i * i <= x; ++i) {
    if (x % i == 0 && (x / i - 1) % i == 1) {
      a.insert(i);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int x;
  cin >> x;
  kek(x - 1);
  kek1(x);
  a.insert(x);
  for (auto &x : a)
    dbg(x);
  cout << sz(a);
}