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

#include <bits/stdc++.h>
using namespace std;

#define INF 1001001001
#define LINF 1001001001001001001
#define MOD 1000000007
#define MOD2 998244353

template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rep1(i, a, b) for (int a_ = (a), b_ = (b), i = a_; i < b_; ++i)
#define repr(i, n) for (int _i = (n), i = _i; i > 0; --i)

#define db(x) cerr << #x << " = " << x << " ";
#define db2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ") ";
#define db3(x, y, z)                                                           \\
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y   \\
       << ", " << z << ") ";
#define ln cout << endl;

#define all(a) (a).begin(), (a).end()
#define dig(n) to_string(n).length()

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define se second
#define fi first

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppii;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {

  bool flag = false;
  ll ans = 0, sum = 1;

  ll n;
  cin >> n;

  ll p = n;
  vector<ll> divis1;
  vector<ll> dsmall;
  vector<ll> dlarge;

  rep1(i, 2, sqrt(p) + 1) {
    if (p % i == 0) {
      dsmall.emplace_back(i);
      if (i != p / i)
        dlarge.emplace_back(p / i);
    }
  }
  dlarge.emplace_back(p);

  p = n - 1;
  rep1(i, 2, sqrt(p) + 1) {
    if (p % i == 0) {
      dsmall.emplace_back(i);
      if (i != p / i)
        dlarge.emplace_back(p / i);
    }
  }
  dlarge.emplace_back(p);

  for (ll can : dsmall) {
    ll c = n;
    while (c % can == 0)
      c /= can;
    if (c % can == 1) {
      ans++;
      // db(can);ln;
    }
  }

  for (ll can : dlarge) {
    if (n % can == 1) {
      ans++;
      // db(can);ln;
    }
  }

  cout << ans + 1 << endl;
  // cout <<fixed<<setprecision(16)<< << endl;
  // if(flag)cout << "Yes" <<endl;
  // else cout << "No" <<endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define INF 1001001001
#define LINF 1001001001001001001
#define MOD 1000000007
#define MOD2 998244353

template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rep1(i, a, b) for (int a_ = (a), b_ = (b), i = a_; i < b_; ++i)
#define repr(i, n) for (int _i = (n), i = _i; i > 0; --i)

#define db(x) cerr << #x << " = " << x << " ";
#define db2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ") ";
#define db3(x, y, z)                                                           \\
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y   \\
       << ", " << z << ") ";
#define ln cout << endl;

#define all(a) (a).begin(), (a).end()
#define dig(n) to_string(n).length()

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define se second
#define fi first

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppii;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {

  bool flag = false;
  ll ans = 0, sum = 1;

  ll n;
  cin >> n;

  ll p = n;
  vector<ll> divis1;

  rep1(i, 2, sqrt(p) + 1) {
    if (p % i == 0) {
      divis1.emplace_back(i);
      if (i != p / i)
        divis1.emplace_back(p / i);
    }
  }
  divis1.emplace_back(p);

  p = n - 1;
  rep1(i, 2, sqrt(p) + 1) {
    if (p % i == 0) {
      divis1.emplace_back(i);
      if (i != p / i)
        divis1.emplace_back(p / i);
    }
  }
  divis1.emplace_back(p);

  for (ll can : divis1) {
    ll c = n;
    while (c % can == 0)
      c /= can;
    if (c % can == 1) {
      ans++;
      // db(can);ln;
    }
  }

  cout << ans << endl;
  // cout <<fixed<<setprecision(16)<< << endl;
  // if(flag)cout << "Yes" <<endl;
  // else cout << "No" <<endl;

  return 0;
}