Problem p02722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

// #define _USE_MATH_DEFINES
// #include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>
#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

// const i64 mod = 1000000007ll;
const i64 smod = 11; //  998244353ll;
// const i64 inf = 1000000007; // 10000000000000007ll;

const double eps = 1e-8;

const i64 MAXN = 4 * 1'000'000 + 6;

i64 T;

i64 n, k, c;

void divs(i64 v, set<i64> &q) {
  for (i64 i = 2; i <= sqrt(v) + 2; i++) {
    if (v % i == 0) {
      q.insert(i);
      q.insert(v / i);
    }
  }
  q.insert(v);
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);

  cin >> n;

  set<i64> p1;
  divs(n - 1, p1);

  i64 R = p1.size();

  set<i64> p;
  divs(n, p);

  for (auto t : p) {
    i64 v = n;
    while (v % t == 0) {
      v /= t;
    }
    if (v > t) {
      v -= v / t * t;
    }
    if (v == 1) {
      R += 1;
    }
  }

  cout << R << endl;

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

// #define _USE_MATH_DEFINES
// #include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>
#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

// const i64 mod = 1000000007ll;
const i64 smod = 11; //  998244353ll;
// const i64 inf = 1000000007; // 10000000000000007ll;

const double eps = 1e-8;

const i64 MAXN = 4 * 1'000'000 + 6;

i64 T;

i64 n, k, c;

void divs(i64 v, set<i64> &q) {
  for (i64 i = 2; i <= sqrt(v) + 2; i++) {
    if (v % i == 0) {
      q.insert(i);
      q.insert(v / i);
    }
  }
  q.insert(v);
  q.erase(1);
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);

  cin >> n;

  set<i64> p1;
  divs(n - 1, p1);

  i64 R = p1.size();

  set<i64> p;
  divs(n, p);

  for (auto t : p) {
    i64 v = n;
    while (v % t == 0) {
      v /= t;
    }
    if (v > t) {
      v -= v / t * t;
    }
    if (v == 1) {
      R += 1;
    }
  }

  cout << R << endl;

  return 0;
}