alexjercan/bugnet huggingface dataset:
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N;
  cin >> N;
  if (N == 2) {
    cout << "1" << endl;
    return 0;
  }
  vector<long long> v;

  // long long k=N-1;
  int cnt = 0;
  int n = 0;
  int m = 0;
  // m = 0 or 1
  cnt++; // m=0, n=1, K=N-1
  cnt++; // m=1,n=0,K=N

  long long dev = 2;
  long long k = N / dev;
  while (k >= 2) {
    if ((N - 1) % k == 0) { // m = 0
      // cout << " " << k << endl;
      cnt++;
    } else if (N % k == 0 && (N / k - 1) % k == 0) { // m = 1
      // cout << " " << k << endl;
      cnt++;
    }

    if (N > dev * (dev + 1)) { // N/(dev) - N/(dev+1) > 1
      dev++;
      k = N / dev;
    } else {
      k--;
    }
  }

  // m >= 2
  int max_m = (int)log2(N);
  for (int m = 2; m <= max_m; m++) {

    long long k = 2;
    long long km = pow(k, m);
    while (km <= N) {
      if (N % km == 0) {
        long long dev = N / km;
        if ((dev - 1) % k == 0) {
          // cout << " "  << k << endl;
          cnt++;
        }
      }
      k++;
      km = pow(k, m);
    }
  }

  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

long long func(long long k, int n, int m) { return (k * n + 1) * pow(k, m); }

int main() {
  long long N;
  cin >> N;
  vector<long long> v;

  // long long k=N-1;
  int cnt = 0;
  int n = 0;
  int m = 0;
  int max_m = (int)log2(N);
  for (int m = 0; m <= max_m; m++) {
    int max_n = (int)((N / pow(2, m) - 1) / 2);
    for (int n = 0; n <= max_n; n++) {
      long long kl = 2;
      long long kr = N + 1;
      bool flag = false;
      while (true) {
        long long km = (long long)(kl + kr) / 2;
        long long f = func(km, n, m);
        if (f == N) {
          cnt++;
          flag = true;
          break;
        } else if (f > N) {
          kr = km;
        } else {
          if (kl == km) {
            break;
          }
          kl = km;
        }
      }
    }
  }

  cout << cnt << endl;
  return 0;
}
Problem p02722 (C++) — buggy submission, verdict: Runtime Error

// cout << "Case #" << k << ": ";
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
vector<ll> divisor(ll n) {
  vector<ll> 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);
}
int main() {
  ll n;
  cin >> n;
  if (n == 2) {
    cout << 1 << endl;
    return 0;
  }
  ll ans = 0;
  auto res = divisor(n - 1);
  ans += res.size() - 1;
  res = divisor(n);
  for (auto e : res) {
    if (e == 1) {
      continue;
    }
    ll a = 1;
    while (n % (a * e) == 0) {
      a *= e;
      if (a * e == 0) {
        break;
      }
    }
    ll m = n / a;
    if (m % e == 1) {
      ans++;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

// cout << "Case #" << k << ": ";
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
vector<ll> divisor(ll n) {
  vector<ll> 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);
}
int main() {
  ll n;
  cin >> n;
  if (n == 2) {
    cout << 1 << endl;
    return 0;
  }
  ll ans = 0;
  auto res = divisor(n - 1);
  ans += res.size() - 1;
  res = divisor(n);
  for (auto e : res) {
    if (e == 1) {
      continue;
    }
    ll a = 1;
    while (n % (a * e) == 0) {
      a *= e;
    }
    ll m = n / a;
    if (m % e == 1) {
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")

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

#define len(x) (int)((x).size())
#define endl '\\n'
#define int long long
#define x first
#define y second
#define dbg(x) cout << #x << " = " << x << endl;

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

const int MOD = 998244353, MAXN = 1e6 + 10, INF = 1e18 + 10, BASE = 37;

void solve() {
  int n;
  cin >> n;

  set<int> se;
  int d = 1;
  while (d * d <= n - 1) {
    if ((n - 1) % d == 0) {
      se.insert(d);
      se.insert((n - 1) / d);
    }
    d++;
  }

  se.erase(1);

  // dbg(len(se))

  d = 1;
  while (d * d <= n) {
    if (n % d == 0) {
      int d1 = n / d;
      int d2 = d;

      // dbg(d1 << " " << d2)

      int x = n;
      if (d1 != 1) {
        while (x % d1 == 0)
          x /= d1;
      }

      if (x % d1 == 1) {
        se.insert(d1);
      }
      x = n;
      if (d2 != 1) {
        while (x % d2 == 0)
          x /= d2;
      }
      if (x % d2 == 1) {
        se.insert(d2);
      }
    }
    d++;
  }

  se.erase(1);

  cout << len(se) << endl;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")

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

#define len(x) (int)((x).size())
#define endl '\\n'
#define int long long
#define x first
#define y second
#define dbg(x) cout << #x << " = " << x << endl;

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

const int MOD = 998244353, MAXN = 1e6 + 10, INF = 1e18 + 10, BASE = 37;

void solve() {
  int n;
  cin >> n;

  set<int> se;
  int d = 1;
  while (d * d <= n - 1) {
    if ((n - 1) % d == 0) {
      se.insert(d);
      se.insert((n - 1) / d);
    }
    d++;
  }

  se.erase(1);

  d = 1;
  while (d * d <= n - 1) {
    if (n % d == 0) {
      int d1 = n / d;
      int d2 = d;

      int x = n / d1;
      if (x % d1 == 1) {
        se.insert(d1);
      }
      x = n / d2;
      if (x % d2 == 1) {
        se.insert(d2);
      }
    }
  }

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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define usecppio                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) ((x).begin()), ((x).end())
#define int ll
using pii = pair<int, int>;
#define INF 0x7f7f7f7f7f7f7f7f
const bool debug = 0;
const int mod = 1000000007;
vector<int> candidate;

void comp_all_div(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      candidate.push_back(i);
      candidate.push_back(n / i);
    }
  }
  if (n > 1)
    candidate.push_back(n);
}

int32_t main() {
  int n;
  cin >> n;
  comp_all_div(n);
  sort(all(candidate));
  candidate.erase(unique(all(candidate)), candidate.end());
  int ct = 0;
  for (auto it : candidate) {
    int x = n;
    while (x % it == 0)
      x /= it;
    if (x % it == 1)
      ct++;
  }
  candidate.clear();
  comp_all_div(n - 1);
  sort(all(candidate));
  candidate.erase(unique(all(candidate)), candidate.end());
  ct += candidate.size();
  cout << ct << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define usecppio                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) ((x).begin()), ((x).end())
#define int ll
using pii = pair<int, int>;
#define INF 0x7f7f7f7f7f7f7f7f
const bool debug = 0;
const int mod = 1000000007;
vector<int> candidate;

void comp_all_div(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      candidate.push_back(i);
      candidate.push_back(n / i);
    }
  }
  candidate.push_back(n);
}

int32_t main() {
  int n;
  cin >> n;
  comp_all_div(n);
  comp_all_div(n - 1);
  sort(all(candidate));
  candidate.erase(unique(all(candidate)), candidate.end());
  int ct = 0;
  for (auto it : candidate) {
    int x = n;
    while (x % it == 0)
      x /= it;
    if (x % it == 1)
      ct++;
  }
  cout << ct << '\\n';
}
Problem p02722 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pf printf
#define sc(x) scanf("%d", &x);
#define scs(x) scanf("%s", x);
#define scl(x) scanf("%lld", &x);
#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define dep(i, s, e) for (int i = s; i >= e; --i)
typedef long long ll;
using namespace std;
const ll mod = 998244353;
int main() {
  ll n, cou = 2;
  scl(n);
  if (n == 2) {
    pf("1\\n");
    return 0;
  }
  for (ll i = 2; i * i <= n; i++) {
    ll u = n;
    if (u % i == 0) {
      while (u % i == 0)
        u /= i;
      if (u % i == 1)
        cou++;
    } else {
      if (u % i == 1)
        cou += 2;
      if (i * i == n - 1)
        cou--;
    }
  }
  pf("%lld\\n", cou);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pf printf
#define sc(x) scanf("%d", &x);
#define scs(x) scanf("%s", x);
#define scl(x) scanf("%lld", &x);
#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define dep(i, s, e) for (int i = s; i >= e; --i)
typedef long long ll;
using namespace std;
const ll mod = 998244353;
int main() {
  ll n, cou = 1;
  scl(n);
  if (n > 1)
    cou++;
  for (ll i = 2; i * i <= n; i++) {
    ll u = n;
    if (u % i == 0) {
      while (u % i == 0)
        u /= i;
      if (u % i == 1)
        cou++;
    } else {
      if (u % i == 1)
        cou++;
    }
    u /= i;
    if (n % u == 1)
      cou++;
  }
  pf("%lld\\n", cou);
}
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 rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;

// ABC161 F
/*
long long replace(long long n,long long i){

    while(n>=i){

        if(n%i==0)n/=i;

        else{
            n-=i;
        }

    }
    return n;
}
*/
vector<long long> prime1;
vector<long long> prime2;

vector<long long> eratos(long long n, vector<long long> prime) {
  long long tmp = sqrt(n);
  for (long long i = 2; i <= tmp; i++) {
    if (n % i == 0) {
      prime.push_back(i);
      // while(n%i==0)n/=i;
      // prime.push_back(n/i);
    }
  }
  // if(n!=1)prime.push_back(n);
  return prime;
}

long long cal(long long n) {

  long long tmp = sqrt(n);

  long long ret = 1;
  for (long long i = 2; i <= tmp; i++) {
    long long now = 1;
    while (n % i == 0) {
      n /= i;
      now++;
    }
    ret *= now;
  }
  if (n != 1) {
    ret *= 2;
  }

  return ret;
}

int main() {

  long long n;
  cin >> n;

  long long ans = 1;
  if (n == 2) {
    cout << "1" << endl;
    return 0;
  }

  prime1 = eratos(n, prime1);

  // nの約数について
  for (long long i = 0; i < prime1.size(); i++) {
    long long p = n / prime1[i];

    // cout<<prime1[i]<<endl;
    // cout<<p<<endl;
    if (p % prime1[i] == 1) {
      ans++;
    } else if (p == prime1[i]) {
      ans++;
    } else if (p % prime1[i] == 0) {

      while (p % prime1[i] == 0) {
        p /= prime1[i];
      }
      p %= prime1[i];
      if (p == 1)
        ans++;
    }
  }
  // cout<<ans<<endl;
  // nの約数以外について

  ans += cal(n - 1);
  ans--;

  cout << ans << endl;
  return 0;
}
/*

*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;

// ABC161 F
long long replace(long long n, long long i) {

  while (n >= i) {
    if (n % i == 0)
      n /= i;

    else {
      n -= i;
    }
  }
  return n;
}
int main() {

  long long n;
  cin >> n;

  long long ans = 0;

  for (long long i = 2; i <= n; i++) {
    if (replace(n, i) == 1)
      ans++;
  }

  cout << ans << endl;
  return 0;
}
/*

*/
Problem p02722 (C++) — buggy submission, verdict: Runtime Error

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() { cout << '\\n'; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}

//------------------------- Libraries -------------------------//

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;
}

//--------------------------- Solve ---------------------------//

void solve() {
  long long N;
  cin >> N;

  set<long long> ans;

  vector<long long> v0 = enum_divisors(N - 1);
  for (auto K : v0) {
    if (K == 1)
      continue;
    ans.insert(K);
  }

  vector<long long> v = enum_divisors(N);
  for (auto K : v) {
    if (K == 1)
      continue;
    if (K == N) {
      ans.insert(K);
      break;
    }

    long long n = N;
    while (n % K == 0) {
      n /= K;
      if ((n - 1) % K == 0)
        ans.insert(K);
    }

    n = N;
    while (n % K == 0) {
      n /= K;
      if (n == 1)
        ans.insert(K);
    }
  }

  cout << ans.size() << '\\n';
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  solve();

  return 0;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() { cout << '\\n'; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}

//------------------------- Libraries -------------------------//

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;
}

//--------------------------- Solve ---------------------------//

void solve() {
  long long N;
  cin >> N;

  long long ans = 0;

  vector<long long> v0 = enum_divisors(N - 1);
  for (auto K : v0) {
    if (K == 1)
      continue;
    ans++;
  }

  vector<long long> v = enum_divisors(N);
  for (auto K : v) {
    if (K == 1)
      continue;
    if (K == N) {
      ans++;
      break;
    }

    long long k = K;
    while (k < N) {
      if (((N - k) % (k * K)) == 0)
        ans++;
      k *= K;
    }

    long long n = 1;
    while (n < N) {
      n *= K;
      if (n == N)
        ans++;
    }
  }

  cout << ans << '\\n';
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  solve();

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

#include <iostream>
#include <vector>

using namespace std;

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);

      // 重複しないならそれも入れる
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並べる
  // sort(res.begin(), res.end());
  return res;
}

// bool can_be_one( long long N, long long K){
//     if(K == 1) return false;

//     while( N % K == 0){
//         N /= K;
//     }

//     if(N == 1 || N % K == 1) return true;
//     else return false;
// }

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

  // debug
  // for(long long i=2; i<=N; ++i){
  //     long long n = N;
  //     while(n >1){
  //         if(n %i == 0){
  //             n /= i;
  //         }else{
  //             n = n -i;
  //         }
  //     }
  //     if( n == 1) cout << i << endl;
  // }

  long long cnt = 0;
  for (long long x : enum_divisors(N)) {
    if (x == 1)
      continue;
    long long tmp = N;
    while (tmp % x == 0)
      tmp /= x;
    tmp %= x;
    if (tmp == 1)
      cnt++;
  }

  vector<long long> nums_mod1 = enum_divisors(N - 1);
  cnt += nums_mod1.size() - 1;

  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (int i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);

      // 重複しないならそれも入れる
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並べる
  // sort(res.begin(), res.end());
  return res;
}

bool can_be_one(long long N, long long K) {
  if (K == 1)
    return false;

  while (N % K == 0) {
    N /= K;
  }

  if (N == 1 || N % K == 1)
    return true;
  else
    return false;
}

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

  // debug
  // for(long long i=2; i<=N; ++i){
  //     long long n = N;
  //     while(n >1){
  //         if(n %i == 0){
  //             n /= i;
  //         }else{
  //             n = n -i;
  //         }
  //     }
  //     if( n == 1) cout << i << endl;
  // }

  long long cnt = 0;
  vector<long long> nums_mod0 = enum_divisors(N);
  for (long long K : nums_mod0) {
    if (can_be_one(N, K))
      cnt++;
  }
  vector<long long> nums_mod1 = enum_divisors(N - 1);
  cnt += nums_mod1.size() - 1;

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

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

int main() {
  ll n, a;
  cin >> n;
  ll ans = 1;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 1) {
      if (i * i == n - 1)
        ans++;
      else
        ans += 2;
    } else if (n % i == 0) {
      a = n;
      while (a % i == 0) {
        a /= i;
      }
      if (a % i == 1)
        ans++;
    }
  }
  if (n != 2)
    ans++;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  vector<ll> P, Q;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      P.push_back(i);
      if (i * i != n)
        P.push_back(n / i);
    } else if ((n - 1) % i == 0) {
      Q.push_back(i);
      if (i * i != (n - 1))
        Q.push_back((n - 1) / i);
    }
  }

  ll m;
  ll ans = 0;

  rep(i, Q.size()) {
    if (n % Q[i] == 1)
      ans++;
  }
  rep(i, P.size()) {
    m = n;
    while (true) {
      if (m % P[i] == 0)
        m /= P[i];
      else {
        if (m % P[i] == 1)
          ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;

typedef long long ll;

ll operation(ll x, ll K) {
  while (x > K) {
    if (x % K == 0)
      x = x / K;
    else
      x = x % K;
  }
  return x;
}

bool isGood(ll x, ll K) {
  while (x % K == 0) {
    x /= K;
  }

  return (x % K == 1);
}

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

  vector<ll> toTest;

  for (ll i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      toTest.push_back(i);
      if (i != N / i)
        toTest.push_back(N / i);
    }
  }

  ll n = N - 1;

  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      toTest.push_back(i);
      if (i != n / i)
        toTest.push_back(n / i);
    }
  }

  // toTest.push_back(N);
  // toTest.push_back(n);

  if (N == 2) {
    cout << 1;
    return 0;
  }

  ll ans = 2;
  for (auto it : toTest) {
    if (isGood(N, it))
      ans++;
  }
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;

typedef long long ll;

ll operation(ll x, ll K) {
  while (x > K) {
    if (x % K == 0)
      x = x / K;
    else
      x = x % K;
  }
  return x;
}

bool isGood(ll x, ll K) {
  while (x % K == 0) {
    x /= K;
  }

  return (x % K == 1);
}

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

  vector<ll> toTest;

  for (ll i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      toTest.push_back(i);
      if (i != N / i)
        toTest.push_back(N / i);
    }
  }

  ll n = N - 1;

  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      toTest.push_back(i);
      if (i != n / i)
        toTest.push_back(n / i);
    }
  }

  toTest.push_back(N);
  toTest.push_back(n);

  ll ans = 0;
  for (auto it : toTest) {
    if (isGood(N, it))
      ans++;
  }
  cout << ans;

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

#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

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

  return ans;
}

ll divsub(ll n, ll k) {
  ll mod = n % k;
  return mod == 0 ? divsub(n / k, k) : mod;
}

int main(int argc, const char *argv[]) {
  ll n;
  cin >> n;

  ll ans = get_divisors(n - 1).size() - 1;
  auto divisors = get_divisors(n);
  for (auto divisor : divisors) {
    if (divisor != 1 && divsub(n, divisor) == 1) {
      ans++;
    }
  }

  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

vector<ll> get_divisors(ll n) {
  vector<ll> ans;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ans.push_back(i);
      if (i * i != n) {
        ans.push_back(n / i);
      }
    }
  }

  return ans;
}

ll get_result(ll n, ll k) {
  ll mod = n % k;
  return mod == 0 ? get_result(n / k, k) : mod;
}

int main(int argc, const char *argv[]) {
  ll n;
  cin >> n;

  ll ans = get_divisors(n - 1).size() - 1;
  auto divisors = get_divisors(n);
  for (auto divisor : divisors) {
    if (divisor != 1 && get_result(n, divisor) == 1) {
      ans++;
    }
  }

  cout << ans << "\\n";
  return 0;
}