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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// マクロ&定数 ============================================================
typedef unsigned int uint;
typedef long long ll;
// typedef pair<int, int> P;

typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;

typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;

typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<bool>> vvbool;

const int c_YET = -1;
const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const ll MOD = 1e9 + 7;                           // 10^9 + 7
const ll MAX = 1e9;
const double PI = 3.14159265358979323846264338327950288;
//========================================================================

vll yakusuu(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

ll N;
bool check(ll n) {
  ll k = N;
  while (k >= n) {
    if (k % n == 0) {
      k /= n;
    } else {
      k %= n;
    }
  }

  if (k == 1) {
    return true;
  } else {
    return false;
  }
}

int main() {

  ////==================================
  // cin.tie(nullptr);
  // ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(30);
  ////==================================

  cin >> N;

  vll yaku_N = yakusuu(N);
  vll yaku_N1 = yakusuu(N - 1);

  ll ans = yaku_N1.size() - 1;

  for (int i = 1; i < yaku_N.size(); i++) {
    if (yaku_N[i] == 6) {
      int df = 0;
    }

    if (check(yaku_N[i])) {
      ans++;
    }
  }

  cout << ans;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// マクロ&定数 ============================================================
typedef unsigned int uint;
typedef long long ll;
typedef pair<int, int> P;

typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;

typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;

typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<bool>> vvbool;

const int c_YET = -1;
const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const ll MOD = 1e9 + 7;                           // 10^9 + 7
const ll MAX = 1e9;
const double PI = 3.14159265358979323846264338327950288;
//========================================================================

vll yakusuu(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

ll N;
bool check(ll n) {
  ll k = N;
  while (k > 1) {
    if (k % n == 0) {
      k /= n;
    } else {
      k -= n;
    }
  }

  if (k == 1) {
    return true;
  } else {
    return false;
  }
}

int main() {

  ////==================================
  // cin.tie(nullptr);
  // ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(30);
  ////==================================

  cin >> N;

  vll yaku_N = yakusuu(N);
  vll yaku_N1 = yakusuu(N - 1);

  ll ans = 0;
  for (int i = 1; i < yaku_N.size(); i++) {
    if (check(yaku_N[i])) {
      ans++;
    }
  }
  for (int i = 1; i < yaku_N1.size(); i++) {
    if (check(yaku_N1[i])) {
      ans++;
    }
  }

  cout << ans;
}