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

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define Mp make_pair
#define pb push_back

using ll = long long;
using db = double;
using pii = pair<int, int>;
using vi = vector<int>;

ll n, m;
bool judge(ll x) {
  ll tmp = n;
  while (tmp % x == 0)
    tmp /= x;
  return tmp % x == 1;
}
signed main() {
  scanf("%lld", &n);
  if (n == 2)
    return puts("1"), 0;
  m = n - 1;
  int cnt = 2;
  for (ll i = 2; i * i <= m; i++)
    if (i * i == m)
      cnt++;
    else if (m % i == 0)
      cnt += 2;
  for (ll i = 2; i * i <= n; i++)
    if (i * i == n)
      cnt += judge(i);
    else if (n % i == 0)
      cnt += judge(i), cnt += judge(n / i);
  printf("%d\\n", cnt);
  fprintf(stderr, "time=%.4f\\n", (db)clock() / CLOCKS_PER_SEC);
  return 0;
  /* 取模直接除，爆零两行泪
   * 不开ll见祖宗
   */
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
%:pragma GCC optimize(3)
%:pragma GCC optimize("Ofast")
%:pragma GCC optimize("inline")
%:pragma GCC optimize("-fgcse")
%:pragma GCC optimize("-fgcse-lm")
%:pragma GCC optimize("-fipa-sra")
%:pragma GCC optimize("-ftree-pre")
%:pragma GCC optimize("-ftree-vrp")
%:pragma GCC optimize("-fpeephole2")
%:pragma GCC optimize("-ffast-math")
%:pragma GCC optimize("-fsched-spec")
%:pragma GCC optimize("unroll-loops")
%:pragma GCC optimize("-falign-jumps")
%:pragma GCC optimize("-falign-loops")
%:pragma GCC optimize("-falign-labels")
%:pragma GCC optimize("-fdevirtualize")
%:pragma GCC optimize("-fcaller-saves")
%:pragma GCC optimize("-fcrossjumping")
%:pragma GCC optimize("-fthread-jumps")
%:pragma GCC optimize("-funroll-loops")
%:pragma GCC optimize("-fwhole-program")
%:pragma GCC optimize("-freorder-blocks")
%:pragma GCC optimize("-fschedule-insns")
%:pragma GCC optimize("inline-functions")
%:pragma GCC optimize("-ftree-tail-merge")
%:pragma GCC optimize("-fschedule-insns2")
%:pragma GCC optimize("-fstrict-aliasing")
%:pragma GCC optimize("-fstrict-overflow")
%:pragma GCC optimize("-falign-functions")
%:pragma GCC optimize("-fcse-skip-blocks")
%:pragma GCC optimize("-fcse-follow-jumps")
%:pragma GCC optimize("-fsched-interblock")
%:pragma GCC optimize("-fpartial-inlining")
%:pragma GCC optimize("no-stack-protector")
%:pragma GCC optimize("-freorder-functions")
%:pragma GCC optimize("-findirect-inlining")
%:pragma GCC optimize("-fhoist-adjacent-loads")
%:pragma GCC optimize("-frerun-cse-after-loop")
%:pragma GCC optimize("inline-small-functions")
%:pragma GCC optimize("-finline-small-functions")
%:pragma GCC optimize("-ftree-switch-conversion")
%:pragma GCC optimize("-foptimize-sibling-calls")
%:pragma GCC optimize("-fexpensive-optimizations")
%:pragma GCC optimize("-funsafe-loop-optimizations")
%:pragma GCC optimize("inline-functions-called-once")
%:pragma GCC optimize("-fdelete-null-pointer-checks")
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define Mp make_pair
#define pb push_back

using ll = long long;
using db = double;
using pii = pair<int, int>;
using vi = vector<int>;

ll n;
int ans;
signed main() {
  scanf("%lld", &n);
  set<ll> pr = {n, n - 1};
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0) {
      pr.insert(i);
      if (i * i != n)
        pr.insert(n / i);
    }
  for (ll i = 2; i * i <= n - 1; i++)
    if ((n - 1) % i == 0) {
      pr.insert(i);
      if (i * i != n - 1)
        pr.insert((n - 1) / i);
    }
  for (ll i : pr) {
    ll tmp = n;
    while (tmp % i == 0)
      tmp /= i;
    // printf("%lld %lld\\n", i, tmp);
    ans += tmp % i == 1;
  }
  printf("%d\\n", ans);
  fprintf(stderr, "time=%.4f\\n", (db)clock() / CLOCKS_PER_SEC);
  return 0;
  /* 取模直接除，爆零两行泪
   * 不开ll见祖宗
   */
}