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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)
template <typename T> void scan(T &x) {
  x = 0;
  bool _ = 0;
  T c = gc;
  _ = c == 45;
  c = _ ? gc : c;
  while (c < 48 || c > 57)
    c = gc;
  for (; c < 48 || c > 57; c = gc)
    ;
  for (; c > 47 && c < 58; c = gc)
    x = (x << 3) + (x << 1) + (c & 15);
  x = _ ? -x : x;
}
template <typename T> void printn(T n) {
  bool _ = 0;
  _ = n < 0;
  n = _ ? -n : n;
  char snum[65];
  int i = 0;
  do {
    snum[i++] = n % 10 + 48;
    n /= 10;
  } while (n);
  --i;
  if (_)
    pc(45);
  while (i >= 0)
    pc(snum[i--]);
}
template <typename First, typename... Ints>
void scan(First &arg, Ints &...rest) {
  scan(arg);
  scan(rest...);
}
template <typename T> void print(T n) {
  printn(n);
  pc(10);
}
template <typename First, typename... Ints>
void print(First arg, Ints... rest) {
  printn(arg);
  pc(32);
  print(rest...);
}

long long n, cnt;
std::set<long long> done;

typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
  ull b, m;
  FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
  ull reduce(ull a) {
    ull q = (ull)((L(m) * a) >> 64);
    ull r = a - q * b; // can be proven that 0 <= r < 2*b
    return r >= b ? r - b : r;
  }
};

FastMod F(2);

void go(long long i) {
  if (done.count(i))
    return;
  done.insert(i);
  long long m = n;
  F = FastMod(i);
  while (F.reduce(m) == 0)
    m /= i;
  //    while(m % i == 0)
  //        m /= i;
  //    m %= i;
  m = F.reduce(m);

  if (m == 1) {
    //        print(i);
    cnt++;
  }
}

int main() {
  scan(n);
  if (n == 2) {
    print(1);
    return 0;
  } else if (n == 3) {
    print(2);
    return 0;
  }

  go(n);
  go(n - 1);

  for (long long i = 2; i * i <= n; i++) {
    go(i);
    go(n / i);
  }

  print(cnt);

  return 0;
}
/*
 n, n-1, then factor

1000000000000
 */

Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)
template <typename T> void scan(T &x) {
  x = 0;
  bool _ = 0;
  T c = gc;
  _ = c == 45;
  c = _ ? gc : c;
  while (c < 48 || c > 57)
    c = gc;
  for (; c < 48 || c > 57; c = gc)
    ;
  for (; c > 47 && c < 58; c = gc)
    x = (x << 3) + (x << 1) + (c & 15);
  x = _ ? -x : x;
}
template <typename T> void printn(T n) {
  bool _ = 0;
  _ = n < 0;
  n = _ ? -n : n;
  char snum[65];
  int i = 0;
  do {
    snum[i++] = n % 10 + 48;
    n /= 10;
  } while (n);
  --i;
  if (_)
    pc(45);
  while (i >= 0)
    pc(snum[i--]);
}
template <typename First, typename... Ints>
void scan(First &arg, Ints &...rest) {
  scan(arg);
  scan(rest...);
}
template <typename T> void print(T n) {
  printn(n);
  pc(10);
}
template <typename First, typename... Ints>
void print(First arg, Ints... rest) {
  printn(arg);
  pc(32);
  print(rest...);
}

long long n, cnt;
std::set<long long> done;

void go(long long i) {
  if (done.count(i))
    return;
  done.insert(i);
  long long m = n;
  while (m % i == 0)
    m /= i;
  m %= i;
  if (m == 1) {
    //        print(i);
    cnt++;
  }
}

int main() {
  scan(n);

  go(n);
  go(n - 1);

  for (long long i = 2; i * i <= n; i++) {
    go(i);
    go(n / i);
  }

  print(cnt);

  return 0;
}
/*
 n, n-1, then factor

1000000000000
 */