alexjercan/bugnet huggingface dataset:
Problem p02722 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\\n'
using namespace std;

/*  divisor(n)
    入力：整数 n
    出力：nのすべての約数
    計算量：O(√n)
*/
vector<long long> divisor(long long n) {
  vector<long long> ret;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}

using ll = long long;

int main() {
  ll N;
  cin >> N;
  ll ans = 0;

  // K で割り切れない時
  auto div = divisor(N - 1);
  ans += (int)div.size() - 1;

  // K で割り切れる時
  auto dv = divisor(N);
  for (auto k : dv) {
    if (k == N)
      ans++;
    if (k == 1 || k == N)
      continue;
    ll now = k;
    while (N % now == 0) {
      if ((N / now - 1) % k == 0) {
        ans++;
      }
      now *= k;
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\\n'
using namespace std;

/*  divisor(n)
    入力：整数 n
    出力：nのすべての約数
    計算量：O(√n)
*/
vector<long long> divisor(long long n) {
  vector<long long> ret;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}

using ll = long long;

int main() {
  ll N;
  cin >> N;
  ll ans = 0;

  // K で割り切れない時
  auto div = divisor(N - 1);
  ans += (int)div.size() - 1;

  // K で割り切れる時
  auto dv = divisor(N);
  for (auto k : dv) {
    if (k == 1)
      continue;
    ll now = k;
    while (N % now == 0) {
      if ((N / now + k - 1) % k == 0) {
        ans++;
      }
      now *= k;
    }
  }

  cout << ans << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool is(ll n, ll k) {
  while (n >= k) {
    if (n % k == 0) {
      n = n / k;
    } else {
      n %= k;
    }
  }
  return n == 1;
}

void solve(long long N) {
  if (N == 2) {
    cout << 1 << endl;
    return;
  }
  ll ans = 0;
  for (ll i = 2; i <= sqrt(N); i++) {
    if (N % i != 0)
      continue;
    if (is(N, i)) {
      ans++;
    }

    if (is(N, N / i)) {
      ans++;
      if (i * i == N)
        ans--;
    }
  }
  ans += 2;

  for (ll i = 2; i <= sqrt(N - 1); i++) {
    if ((N - 1) % i == 0) {
      ans += 2;
      if (i * i == N - 1)
        ans--;
    }
  }

  cout << ans << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  solve(N);
  return 0;
}


Error: TLE

Fixed submission:

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

bool is(ll n, ll k) {
  while (n >= k) {
    if (n % k == 0) {
      n = n / k;
    } else {
      n %= k;
    }
  }
  return n == 1;
}

void solve(long long N) {
  set<ll> v;
  for (int i = 2; i <= ceil(sqrt(N)); i++) {
    if (N % i != 0)
      continue;
    if (is(N, i)) {
      v.insert(i);
    }

    if (is(N, N / i)) {
      v.insert(N / i);
    }
  }
  v.insert(N);
  v.insert(N - 1);

  for (int i = 2; i <= ceil(sqrt(N - 1)); i++) {
    if ((N - 1) % i == 0) {
      v.insert(i);
      v.insert((N - 1) / i);
    }
  }

  cout << v.size() << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  solve(N);
  return 0;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

const string YES = "Yes";
const string NO = "No";
const ll mod = 1e9 + 7;

// map<ll,ll> primes(ll n){
//     map<ll, ll> r;
//     for(int i=2; i*i<=n; i++){
//         while(n % i ==0){
//             r[i]++;
//             n /= i;
//         }
//     }

//     if(n != 1) r[n] = 1;
//     return r;
// }

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  if (n == 2) {
    cout << 1;
    return 0;
  }
  ll ans = 1;
  ll tmp = n - 1;
  for (ll i = 2; i * i <= tmp; i++) {
    if (tmp % i == 0) {
      if (tmp / i == i)
        ans++;
      else
        ans += 2;
    }
  }

  set<ll> st;
  // cout << ans << endl;
  for (ll k = 2; k * k <= n; k++) {
    if (n % k == 0) {
      st.insert(k);
      st.insert(n / k);
    }
  }
  st.insert(n);
  ll a = n;
  for (auto k : st) {
    n = a;
    while (n % k == 0) {
      n /= k;
    }
    // cout << n << endl;
    if (n % k == 1)
      ans++;
    // cout << ans << endl;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

const string YES = "Yes";
const string NO = "No";
const ll mod = 1e9 + 7;

map<ll, ll> primes(ll n) {
  map<ll, ll> r;
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      r[i]++;
      n /= i;
    }
  }

  if (n != 1)
    r[n] = 1;
  return r;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  ll ans = 1;
  map<ll, ll> sub = primes(n - 1);
  for (auto p : sub) {
    ans *= (p.second + 1);
  }
  ans--;
  set<ll> st;
  // cout << ans << endl;
  for (ll k = 2; k * k <= n; k++) {
    if (n % k == 0) {
      st.insert(k);
      st.insert(n / k);
    }
  }
  st.insert(n);
  ll a = n;
  for (auto k : st) {
    n = a;
    while (n % k == 0) {
      n /= k;
    }
    // cout << n << endl;
    if (n % k == 1)
      ans++;
    // cout << ans << endl;
  }
  cout << ans;
  return 0;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  int check = 0;
  queue<long long> que;
  cin >> N;
  if (N == 2) {
    cout << 1 << endl;
  } else {
    for (long long i = 2; i * i <= N; i++) {
      if (N % i == 0) {
        que.push(i);
        if (i * i != N) {
          que.push(N / i);
        }
      }
      if ((N - 1) % i == 0) {
        que.push(i);
        if (i * i != N - 1) {
          que.push((N - 1) / i);
        }
      }
    }
    while (!(que.empty())) {
      long long n = N;
      long long num = que.front();
      que.pop();
      // cout << num << " ! " << endl;
      while (n % num == 0) {
        n /= num;
      }
      if (n % num == 1) {
        check++;
        // cout << num << endl;
      }
    }
    cout << check + 2 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int check = 0;
  queue<int> que;
  cin >> N;
  for (int i = 2; i <= N; i++) {
    if (N % i == 0) {
      que.push(i);
    }
    if ((N - 1) % i == 0) {
      que.push(i);
    }
  }
  while (!(que.empty())) {
    int n = N;
    int num = que.front();
    que.pop();
    while (n % num == 0) {
      n /= num;
    }
    while (n > num) {
      n -= num;
    }
    if (n == 1) {
      check++;
    }
  }
  cout << check << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <chrono>
#include <ctime>
#include <iostream>
#include <queue>
#include <random>
#include <set>
using namespace std;
typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> P;
bool cha(ll a, ll b) {
  // cout<<a<<" "<<b<<endl;
  while (a > 1) {
    // cout<<" "<<a<<" "<<b<<endl;
    if (a % b == 0)
      a /= b;
    else {
      a %= b;
      break;
    }
  }
  // cout<<a<<" "<<b<<" "<<endl;
  if (a == 1)
    return true;
  else
    return false;
}
int main() {
  ll n;
  cin >> n;
  ll n2 = n - 1;

  set<ll> s;
  if (n2 > 2)
    s.insert(n2);

  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      s.insert(i);
      s.insert(n / i);
      // if (i*i==n || i==1) an++;
      // else an+=2;
    }
  }

  for (ll i = 1; i * i <= n2; i++) {
    if (n2 % i == 0) {
      s.insert(i);
      s.insert(n / i);
      // if (i*i==n2 || i==1) an++;
      // else an+=2;
    }
  }
  if (s.find(1) != s.end())
    s.erase(1);
  ll an = 0;
  for (ll x : s) {
    if (cha(n, x))
      an++;
  }
  // cout<<s.size()<<endl;
  cout << an << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <chrono>
#include <ctime>
#include <iostream>
#include <queue>
#include <random>
#include <set>
using namespace std;
typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> P;
bool cha(ll a, ll b) {
  // cout<<a<<" "<<b<<endl;
  while (a > 1) {
    // cout<<" "<<a<<" "<<b<<endl;
    if (a % b == 0)
      a /= b;
    else
      a %= b;
  }
  // cout<<a<<" "<<b<<" "<<endl;
  if (a == 1)
    return true;
  else
    return false;
}
int main() {
  ll n;
  cin >> n;
  ll n2 = n - 1;

  set<ll> s;
  if (n2 > 2)
    s.insert(n2);

  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      s.insert(i);
      s.insert(n / i);
      // if (i*i==n || i==1) an++;
      // else an+=2;
    }
  }

  for (ll i = 1; i * i <= n2; i++) {
    if (n2 % i == 0) {
      s.insert(i);
      s.insert(n / i);
      // if (i*i==n2 || i==1) an++;
      // else an+=2;
    }
  }
  if (s.find(1) != s.end())
    s.erase(1);
  ll an = 0;
  for (ll x : s) {
    if (cha(n, x))
      an++;
  }
  // for(ll x:s) cout<<x<<" "; cout<<endl;
  // cout<<s.size()<<endl;
  cout << an << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define nmax_def 2000000
#define InfL 1000000000
#define InfLL 1000000000000000000LL
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--)
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

int main() {
  ll N;
  cin >> N;
  ll ans = 0;
  ll Ktmp = 2;
  if (N == 2)
    ans = 1;
  else {
    while (1) {
      if (N % Ktmp == 0) {
        ll n = N;
        while (1) {
          if (n % Ktmp != 0)
            break;
          n /= Ktmp;
        }
        if (n % Ktmp == 1) {
          ans++;
        }

        if (N != Ktmp * Ktmp) {
          n = N;
          while (1) {
            if (n % (N / Ktmp) != 0)
              break;
            n /= (N / Ktmp);
          }
          if (n % (N / Ktmp) == 1) {
            ans++;
          }
        }
      }
      Ktmp++;
      if (Ktmp * Ktmp > N)
        break;
    }
    Ktmp = 2;
    N--;
    while (1) {
      if (N % Ktmp == 0) {
        ans++;
        if (N != Ktmp * Ktmp) {
          ans++;
        }
      }
      Ktmp++;
      if (Ktmp * Ktmp > N)
        break;
    }
    ans += 2;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define nmax_def 2000000
#define InfL 1000000000
#define InfLL 1000000000000000000LL
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--)
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

int main() {
  ll N;
  cin >> N;
  ll ans = 0;
  ll Ktmp = 2;
  while (1) {
    if (N % Ktmp == 0) {
      ll n = N;
      while (1) {
        if (n % Ktmp != 0)
          break;
        n /= Ktmp;
      }
      if (n % Ktmp == 1) {
        ans++;
      }

      if (N != Ktmp * Ktmp) {
        n = N;
        while (1) {
          if (n % (N / Ktmp) != 0)
            break;
          n /= (N / Ktmp);
        }
        if (n % (N / Ktmp) == 1) {
          ans++;
        }
      }
    }
    Ktmp++;
    if (Ktmp * Ktmp > N)
      break;
  }
  Ktmp = 2;
  N--;
  while (1) {
    if (N % Ktmp == 0) {
      ans++;
      if (N != Ktmp * Ktmp) {
        ans++;
      }
    }
    Ktmp++;
    if (Ktmp * Ktmp > N)
      break;
  }
  ans += 2;
  cout << ans << endl;
  return 0;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

vector<ll> enum_divisor(ll N) {
  vector<ll> ans;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      ans.push_back(i);
      if (i != N / i) {
        ans.push_back(N / i);
      }
    }
  }
  return ans;
}

int main() {
  ll N;
  cin >> N;
  set<ll> res;
  vector<ll> A1 = enum_divisor(N);
  vector<ll> A2 = enum_divisor(N - 1);
  for (auto x : A1)
    res.insert(x);
  for (auto x : A2)
    res.insert(x);
  res.erase(1);
  ll ans = 0;
  for (auto K : res) {
    ll n = N;
    while (n % K == 0)
      n /= K;
    if (n % K == 1)
      ans++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

vector<ll> enum_divisor(ll N) {
  vector<ll> ans;
  for (ll i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      ans.push_back(i);
      if (i != N / i) {
        ans.push_back(N / i);
      }
    }
  }
  ans.push_back(N);
  return ans;
}

int main() {
  ll N;
  cin >> N;
  set<ll> res;
  vector<ll> A1 = enum_divisor(N);
  vector<ll> A2 = enum_divisor(N - 1);
  for (auto x : A1)
    res.insert(x);
  for (auto x : A2)
    res.insert(x);
  ll ans = 0;
  for (auto K : res) {
    ll n = N;
    while (n % K == 0)
      n /= K;
    if (n % K == 1)
      ans++;
  }
  cout << ans << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

using namespace std;

bool OK(ll N, ll K) {
  while (N != 1) {
    if (N % K == 0) {
      N /= K;
      continue;
    } else if (N > K) {
      N %= K;
      continue;
    } else {
      return false;
    }
  }
  return true;
}

map<ll, int> factorize(ll x) {
  map<ll, int> m;
  for (ll i = 2; i * i <= x;) {
    if (x % i == 0) {
      m[i]++;
      x /= i;
    } else {
      ++i;
    }
  }
  if (x > 1)
    m[x]++;
  return m;
}

using iter = map<ll, int>::const_iterator;

void dfs(set<ll> &s, ll K, iter it, iter end) {
  if (it == end) {
    if (K > 1) {
      s.insert(K);
    }
    return;
  }
  auto [k, v] = *it;
  ++it;
  dfs(s, K, it, end);
  for (int i = 0; i < v; ++i) {
    K *= k;
    dfs(s, K, it, end);
  }
}

int main() {
  ll N;
  cin >> N;

  map<ll, int> f1 = factorize(N - 1);
  set<ll> s1;
  dfs(s1, 1, f1.begin(), f1.end());

  map<ll, int> f2 = factorize(N);
  set<ll> s2;
  dfs(s2, 1, f2.begin(), f2.end());

  ll ans = s1.size();
  for (ll x : s2) {
    if (OK(N, x)) {
      ++ans;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

using namespace std;

bool OK(ll N, ll K) {
  while (N != 1) {
    if (N % K == 0) {
      N /= K;
      continue;
    } else if (N > K) {
      N %= K;
      continue;
    } else {
      return false;
    }
  }
  return true;
}

vector<ll> factorize(ll x) {
  vector<ll> v;
  for (ll i = 2; i * i <= x;) {
    if (x % i == 0) {
      v.push_back(i);
      x /= i;
    } else {
      ++i;
    }
  }
  if (x > 1)
    v.push_back(x);
  /*
  for (ll x : v) {
    cout << x << " ";
  }
  cout << endl;
  */
  return v;
}

void dfs(set<ll> &s, int depth, ll K, const vector<ll> &factors) {
  if (depth == factors.size()) {
    if (K > 1) {
      // cout << "dfs1:" << K << endl;
      s.insert(K);
    }
    return;
  }
  dfs(s, depth + 1, K, factors);
  dfs(s, depth + 1, K * factors[depth], factors);
}

void dfs2(set<ll> &s, int depth, ll K, const vector<ll> &factors) {
  if (depth == factors.size()) {
    if (K > 1) {
      // cout << "dfs2:" << K << endl;
      s.insert(K);
    }
    return;
  }
  dfs(s, depth + 1, K, factors);
  dfs(s, depth + 1, K * factors[depth], factors);
}

int main() {
  ll N;
  cin >> N;

  set<ll> solutions;
  dfs(solutions, 0, 1, factorize(N - 1));

  set<ll> s2;
  dfs2(s2, 0, 1, factorize(N));

  ll ans = solutions.size();
  for (ll x : s2) {
    if (OK(N, x)) {
      ++ans;
    }
  }
  cout << ans << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using P = pair<ll, ll>;

// chmin, chmax関数
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

// Yes-Noを出力する問題で楽をする
void Ans(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

//---------------------------

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並び替える
  sort(res.begin(), res.end());
  return res;
}

int main() {
  ll n;
  cin >> n;
  ll ans = 0; // self

  const auto &res = enum_divisors(n - 1);
  // for (int i = 0; i < res.size(); ++i) cout << res[i] << " ";
  // cout << endl;
  ans += res.size() - 1;

  const auto &res2 = enum_divisors(n);
  for (auto x : res2) {
    if (x == 1)
      continue;
    ll nc = n;
    while (nc % x == 0) {
      nc = nc / x;
    }
    if (nc % x == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using P = pair<ll, ll>;

// chmin, chmax関数
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

// Yes-Noを出力する問題で楽をする
void Ans(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

//---------------------------

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並び替える
  sort(res.begin(), res.end());
  return res;
}

int main() {
  ll n;
  cin >> n;
  ll ans = 1; // self

  const auto &res = enum_divisors(n - 1);
  // for (int i = 0; i < res.size(); ++i) cout << res[i] << " ";
  // cout << endl;
  ans += res.size() - 1;

  const auto &res2 = enum_divisors(n);
  rep2(i, 1, res2.size()) {
    ll nc = n, d = res[i];
    while (nc % d == 0)
      nc /= d;
    if (nc % d == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N;
  cin >> N;
  vector<long long> ANS;
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      ANS.push_back(i);
      if (i * i != N)
        ANS.push_back(N / i);
    } else if ((N - 1) % i == 0) {
      ANS.push_back(i);
      if (i * i + 1 != N)
        ANS.push_back((N - 1) / i);
    }
  }
  ANS.push_back(N - 1);
  ANS.push_back(N);
  sort(ANS.begin(), ANS.end());
  ANS.erase(unique(ANS.begin(), ANS.end()), ANS.end());
  long long COUNT = 0;
  for (int i = 0; i < ANS.size(); i++) {
    if (ANS[i] == 1)
      continue;
    long long NOW = N;
    while (NOW % ANS[i] == 0)
      NOW /= ANS[i];
    if (NOW % ANS[i] == 1)
      COUNT++;
  }
  cout << COUNT << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long N;
  cin >> N;
  vector<long long> ANS;
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      ANS.push_back(i);
      if (i * i != N)
        ANS.push_back(N / i);
    } else if ((N - 1) % i == 0) {
      ANS.push_back(i);
      if (i * i + 1 != N)
        ANS.push_back((N - 1) / i);
    }
  }
  ANS.push_back(N - 1);
  ANS.push_back(N);
  sort(ANS.begin(), ANS.end());
  ANS.erase(unique(ANS.begin(), ANS.end()), ANS.end());
  long long COUNT = 0;
  for (int i = 0; i < ANS.size(); i++) {
    long long NOW = N;
    while (NOW % ANS[i] == 0)
      NOW /= ANS[i];
    if (NOW % ANS[i] == 1)
      COUNT++;
  }
  cout << COUNT << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<int, ll>;
using Graph = vector<vector<P>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }

int main() {
  ll n;
  cin >> n;
  ll k = n - 1;
  // n の約数
  vector<ll> a;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      a.pb(i);
      if (i * i != n)
        a.pb(n / i);
    }
  }
  ll ans = 0;
  a.pb(n);
  ll nd = n;
  for (ll v : a) {
    nd = n;
    while (nd % v == 0) {
      nd /= v;
    }
    if (nd % v == 1)
      ans++;
  }
  // cout<<ans<<endl ;
  // n-1の約数

  for (ll i = 2; i * i <= k; i++) {
    if (k % i == 0) {
      ans += 2;
      if (i * i == k)
        ans--;
    }
  }
  if (n != 2)
    ans++;
  cout << ans << endl;
  return 0;
}

Error: -8

Fixed submission:

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

int main() {
  long long int n, k;
  cin >> n >> k;
  int ans = 0;
  for (int i = 2; i < 1000001; i++) {
    if (n - 1 % i == 0)
      ans++;
  }
  for (int i = 0; i < 1000001; i++) {
    while (n % i == 0) {
      n = n % i;
    }
    if (n == 1)
      ans++;
  }

  cout << ans << endl;
}