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

#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

long long n;

bool ok(long long a) {
  long long k = n;
  while (!(k % a) && k >= a)
    k /= a;
  if (k % a == 1)
    return true;
  return false;
}

int main() {
  cin >> n;
  vector<long long> a, a1;
  for (long long i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      a.push_back(i);
      if (n / i != i)
        a.push_back(n / i);
    }
  }
  for (long long i = 1; i * i <= n - 1; ++i) {
    if ((n - 1) % i == 0) {
      a1.push_back(i);
      if ((n - 1) / i != i)
        a1.push_back((n - 1) / i);
    }
  }
  set<long long> s;
  for (long long i = 0; i < a.size(); ++i) {
    if (a[i] >= 2 && ok(a[i]))
      s.insert(a[i]);
  }
  for (int i = 0; i < a1.size(); ++i) {
    if (a1[i] >= 2 && (n % a1[i]))
      s.insert(a1[i]);
  }
  cout << s.size();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long n, c = 0;
  cin >> n;
  for (unsigned long long i = 2; i <= n; ++i) {
    long long k = n;
    while (!(k % i) && k >= i)
      k /= i;
    if (k % i == 1)
      c++;
  }
  cout << c;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 約数の列挙O(√n)
vector<ll> divisor(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i)
        res.push_back(n / i);
    }
  }
  return res;
}
int main() {
  ll n;
  cin >> n;
  vl vec = divisor(n);
  vl vec2 = divisor(n - 1);
  ll count = 0;
  rep(i, vec.size()) {
    if (vec[i] == 1)
      continue;
    ll x = n;
    while (x > 1) {
      if (x % vec[i] == 0)
        x /= vec[i];
      else
        x %= vec[i];
      if (x < vec[i])
        break;
    }
    if (x == 1)
      count++;
  }
  rep(i, vec2.size()) {
    if (vec2[i] == 1)
      continue;
    ll x = n;
    while (x > 1) {
      if (x % vec2[i] == 0)
        x /= vec2[i];
      else
        x %= vec2[i];
      if (x < vec2[i])
        break;
    }
    if (x == 1)
      count++;
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 約数の列挙O(√n)
vector<ll> divisor(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i)
        res.push_back(n / i);
    }
  }
  return res;
}
int main() {
  ll n;
  cin >> n;
  vl vec = divisor(n);
  ll count = 0;
  rep(i, vec.size()) {
    ll x = n;
    while (x % vec[i] == 0) {
      x /= vec[i];
    }
    if ((x - 1) % vec[i] == 0)
      count++;
  }
  cout << count << 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 < (n); i++)
typedef long long ll;

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a, b;
  a = divisor(n);
  b = divisor(n - 1);
  ll ans = 0;
  for (ll c : a) {
    ll x = n;
    if (c == 1) {
      continue;
    } else {
      while (x % c == 0) {
        x = x / c;
      }
      if (x % c == 1) {
        ans++;
      }
    }
  }
  ans += (b.size() - 1);
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a, b;
  a = divisor(n);
  b = divisor(n - 1);
  ll ans = 0;
  for (int c : a) {
    ll x = n;
    if (c == 1)
      continue;
    else {
      while (x % c == 0) {
        x = x / c;
      }
      if (x % c == 1) {
        ans++;
      }
    }
  }
  ans += (b.size() - 1);
  cout << ans << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

vector<long long> f(long long N) {
  vector<long long> res;
  long long i;
  for (i = 1; i * i < N; i++) {
    if (i > 1000000)
      cout << i << endl;
    if (N % i == 0) {
      res.push_back(i);
      res.push_back(N / i);
    }
  }
  if (i * i == N) {
    res.push_back(i);
  }
  return res;
}

int g(long long p, long long N) {
  while (N % p == 0) {
    N /= p;
  }
  if (N % p == 1) {
    return 1;
  } else {
    return 0;
  }
}

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

  auto v = f(N - 1);
  auto w = f(N);
  long long ans = v.size() - 1;
  // for(auto p:w){cout<<p<<" ";}cout<<endl;
  for (auto p : w) {
    if (p != 1) {
      ans += g(p, N);
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

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

int g(long long p, long long N) {
  while (N % p == 0) {
    N /= p;
  }
  if (N % p == 1) {
    return 1;
  } else {
    return 0;
  }
}

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

  auto v = f(N - 1);
  auto w = f(N);
  long long ans = v.size() - 1;
  for (auto p : w) {
    if (p != 1) {
      ans += g(p, N);
    }
  }
  cout << ans << endl;

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

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

bool is_prime(ll n) {
  if (n == 1)
    return false;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

vector<ll> divisor(ll n)

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

map<ll, int> prime_factor(ll n) {
  map<ll, int> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n]++;
  return res;
}
// 左边long long存因数，右边int存因数的指数

vector<ll> prime_enum(int n) {
  vector<bool> is_prime(n + 1, true);
  is_prime[0] = is_prime[1] = false;
  vector<ll> res;
  for (int i = 2; i <= n; i++) {
    if (!is_prime[i])
      continue;
    res.push_back(i);
    for (int j = 2 * i; j <= n; j += i) {
      is_prime[j] = false;
    }
  }
  return res;
}

set<long long> se;

void solve(ll N, ll n) {
  auto v = divisor(N);
  for (ll k : v) {
    if (k == 1)
      continue;
    ll d = n;
    while (d >= k) {
      if (d % k == 0)
        d /= k;
      else
        break;
    }
    d %= k;
    if (d == 1) {
      se.insert(k);
    }
  }
}

int main() {
  ll n;
  cin >> n;
  solve(n, n);
  solve(n - 1, n);
  cout << se.size() << endl;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 1e6 + 19, Mod = 1e9 + 7;
long long n;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  ll tot = 0;
  ll m = n - 1;
  for (long long i = 1; i * i <= m; i++)
    if (m % i == 0) {
      if (i != 1)
        tot++;
      if (m / i != i)
        tot++;
    }
  vector<long long> A;
  for (long long i = 1; i * i <= n; i++)
    if (n % i == 0) {
      A.push_back(i);
      if (n / i != i)
        A.push_back(n / i);
    }
  for (int i = 0; i <= A.size(); i++)
  // for(long long a:A)
  // for each element in A

  // for (int j=0;j<A.size();j++)
  //{ ...A[j]...
  //	... }
  {
    ll a = A[i];
    if (a == 1)
      continue;
    long long tmp = n;
    long long c = 0;
    while (tmp % a == 0)
      tmp /= a, c = 1;
    if (c == 0)
      continue;
    if (tmp % a == 1)
      tot++;
  }
  printf("%lld\\n", tot);
  return 0;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
int main() {
  ll ans = 0;
  long long int N;
  cin >> N;
  for (long long int i = 1; i * i <= N; i++) {
    if (N % i == 0 && i != 1) {
      ll n = N, ni = N, I = N / i;
      while (n % i == 0) {
        n /= i;
      }
      if (n % i == 1) {
        ans++;
      }
      if (i != I) {
        while (ni % I == 0) {
          ni /= I;
        }
        if (ni % I == 1) {
          ans++;
        }
      }

    } else if (i == 1) {
      ans++;
    }
  }
  for (long long int i = 1; i * i <= N; i++) {
    if ((N - 1) % i == 0 && N > 2) {
      if (i * i == (N - 1) || i == 1) {
        ans++;
      } else {
        ans += 2;
      }
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
int main() {
  int ans = 0;
  long long int N;
  cin >> N;
  for (long long int i = 2; i <= N; i++) {
    if (N % i == 0) {
      long long int n = N;
      while (n % i == 0) {
        n = n / i;
      }
      if (n == 1 || (n - 1) % i == 0) {
        ans++;
      }
    } else if ((N - 1) % i == 0) {
      ans++;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define MAXN 1000100
#define ll long long
ll n, m, a[MAXN], h, t, cnt, cnt2[MAXN], d[MAXN], cnt3, ans;
bool check(ll u) {
  t = n;
  while (t % u == 0)
    t /= u;
  if (t % u == 1)
    return true;
  else
    return false;
}
int main() {
  scanf("%lld", &n);
  t = n;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      a[++cnt] = i;
      if (i != n / i)
        a[++cnt] = n / i;
    }
  }
  a[++cnt] = n;
  //	if(t != 1) a[++cnt] = t;
  t = n - 1;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (t % i == 0) {
      d[++cnt3] = i;
      while (t % i == 0) {
        t /= i;
        cnt2[cnt3]++;
      }
    }
  }
  if (t != 1)
    cnt2[++cnt3] = 1, d[cnt3] = t;
  ans = 1;
  for (ll i = 1; i <= cnt3; i++)
    ans *= (cnt2[i] + 1); //, cout<<d[i]<<" "<<cnt2[i]<<endl;;
  ans--;
  // cout<<ans<<endl;
  for (ll i = 1; i <= cnt; i++) {
    if (check(a[i]) == true)
      ans++; // cout<<a[i]<<endl;;
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define MAXN 1000100
#define ll long long
ll n, m, a[MAXN], h, t, cnt, cnt2[MAXN], d[MAXN], cnt3, ans;
bool check(int u) {
  t = n;
  while (t % u == 0)
    t /= u;
  if (t % u == 1)
    return true;
  else
    return false;
}
int main() {
  scanf("%lld", &n);
  t = n;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      a[++cnt] = i;
      if (i != n / i)
        a[++cnt] = n / i;
      while (t % i == 0) {
        t /= i;
      }
    }
  }
  a[++cnt] = n;
  //	if(t != 1) a[++cnt] = t;
  t = n - 1;
  for (int i = 2; i <= sqrt(n - 1); i++) {
    if (t % i == 0) {
      d[++cnt3] = i;
      while (t % i == 0) {
        t /= i;
        cnt2[cnt3]++;
      }
    }
  }
  if (t != 1)
    cnt2[++cnt3] = 1;
  ans = 1;
  for (int i = 1; i <= cnt3; i++)
    ans *= (cnt2[i] + 1);
  ans--;
  // cout<<ans<<endl;
  for (int i = 1; i <= cnt; i++) {
    if (check(a[i]) == true)
      ans++;
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
#define slowanddetermined                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define debug(x)                                                               \\
  ;                                                                            \\
  cout << #x << "=" << x << endl;
#define debug2(x, y)                                                           \\
  ;                                                                            \\
  cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define int long long
using namespace std;

int fun(int n, int x) {
  if (x == 1)
    return 0;
  while (n % x == 0) {
    n /= x;
  }
  n = n % x;
  return n == 1;
}

int32_t main() {

  int n;
  cin >> n;
  set<int> st;
  for (int i = 2; i * i <= (n - 1); i++) {
    if ((n - 1) % i == 0) {
      st.insert(i);
      st.insert((n - 1) / i);
    }
  }
  st.insert(n - 1);

  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (fun(n, i))
        st.insert(i);
      if (fun(n, n / i))
        st.insert(n / i);
    }
  }
  // c++;
  st.erase(1);
  cout << st.size();

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define slowanddetermined                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define debug(x)                                                               \\
  ;                                                                            \\
  cout << #x << "=" << x << endl;
#define debug2(x, y)                                                           \\
  ;                                                                            \\
  cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define int long long
using namespace std;

void p_p_m() {
  slowanddetermined
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int fun(int n, int x) {
  if (x == 1)
    return 0;
  while (n % x == 0) {
    n /= x;
  }
  n = n % x;
  return n == 1;
}

int32_t main() {
  p_p_m();

  int n;
  cin >> n;
  set<int> st;
  for (int i = 2; i * i <= (n - 1); i++) {
    if ((n - 1) % i == 0) {
      st.insert(i);
      st.insert((n - 1) / i);
    }
  }
  st.insert(n - 1);

  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (fun(n, i))
        st.insert(i);
      if (fun(n / i, i))
        st.insert(n / i);
    }
  }
  // c++;
  st.erase(1);
  cout << st.size();

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 998244353;
const int Inv2 = (MOD + 1) / 2;
stack<int> que;
signed main() {
  int n, m, ans = 0, j, j0;
  cin >> n;

  for (int i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    que.push(i);
    while ((n / que.top()) % que.top() == 0) {
      m = que.top();
      que.push(m * m);
    }
    m = n;
    while (!que.empty()) {
      if (m % que.top() == 0) {

        m /= que.top();
      }
      que.pop();
    }
    if (n != m && (m - 1) % i == 0)
      ans++;
    if (i % (n / i) == 1)
      ans++;
  }

  n--;
  int ans2 = 1, l = 2, p, n0 = n;
  for (int i = 2; i * i <= n0; i++) {
    if (n % i)
      continue;
    p = 0;
    que.push(i);
    while ((n / que.top()) % que.top() == 0) {
      m = que.top();
      que.push(m * m);
    }

    while (!que.empty()) {
      p <<= 1;
      if (n % que.top() == 0) {
        n /= que.top();

        p++;
      }
      que.pop();
    }
    ans2 *= (p + 1);
  }
  if (n != 1)
    ans2 *= 2;
  cout << ans + ans2 << endl;
  // cerr<<ans<<' '<<ans2;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 998244353;
const int Inv2 = (MOD + 1) / 2;
stack<int> que;
signed main() {
  int n, m, ans = 0, j, j0;
  cin >> n;

  for (int i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    que.push(i);
    while (n % (que.top() * que.top()) == 0) {
      m = que.top();
      que.push(m * m);
    }
    m = n;
    while (!que.empty()) {
      if (m % que.top() == 0) {

        m /= que.top();
      }
      que.pop();
    }
    if (n != m && (m - 1) % i == 0)
      ans++;
    if (i % (n / i) == 1)
      ans++;
  }

  n--;
  int ans2 = 1, l = 2, p, n0 = n;
  for (int i = 2; i * i <= n0; i++) {
    if (n % i)
      continue;
    p = 0;
    que.push(i);
    while (n % (que.top() * que.top()) == 0) {
      m = que.top();
      que.push(m * m);
    }

    while (!que.empty()) {
      p <<= 1;
      if (n % que.top() == 0) {
        n /= que.top();

        p++;
      }
      que.pop();
    }
    ans2 *= (p + 1);
  }
  if (n != 1)
    ans2 *= 2;
  cout << ans + ans2 << endl;
  // cerr<<ans<<' '<<ans2;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstring>
#include <functional>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;

int main() {
  // freopen("1.txt", "r", stdin);
  LL N;
  cin >> N; // N and N - 1 are relatively prime

  int res = 0;
  for (LL i = 2; i * i <= N - 1; ++i) {
    if ((N - 1) % i == 0) {
      ++res;
      if (i * i != N - 1)
        ++res;
    }
  }
  if (N != 2)
    ++res; // N - 1;

  for (LL i = 2; i * i <= N; ++i) {
    if (N % i == 0) {
      LL n0 = N;
      while (n0 % i == 0)
        n0 /= i;
      if ((n0 - 1) % i == 0)
        ++res;
    }
  }
  ++res;
  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstring>
#include <functional>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;

int main() {
  // freopen("1.txt", "r", stdin);
  int N;
  cin >> N; // N and N - 1 are relatively prime

  int res = 0;
  for (int i = 2; i * i <= N - 1; ++i) {
    if ((N - 1) % i == 0) {
      ++res;
      if (i * i != N - 1)
        ++res;
    }
  }
  if (N != 2)
    ++res; // N - 1;

  for (int i = 2; i * i <= N; ++i) {
    if (N % i == 0) {
      LL n0 = N;
      while (n0 % i == 0)
        n0 /= i;
      if ((n0 - 1) % i == 0)
        ++res;
    }
  }
  ++res;
  cout << res << endl;

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

#include <cmath>
#include <iostream>
using namespace std;
long long f(long long n) {
  long long count = 0;
  for (long long i = 1; i <= (double)sqrt((double)n); i++) {
    if (n % i == 0) {
      count += 2;
      if (i == n / i)
        count--;
    }
  }
  return count;
}
int main() {
  long long n;
  cin >> n;
  if (n == 2) {
    cout << 1 << endl;
    exit(0);
  }
  long long count = 0;
  count += f(n - 1) - 1;
  for (long long i = 2; i <= (double)sqrt((double)n); i++) {
    if (n % i != 0)
      continue;
    long long m = n;
    while (m % i == 0)
      m /= i;
    if (m % i == 1)
      count++;
    m = n;
    if (i == n / i)
      continue;
    while (m % (n / i) == 0)
      m /= (n / i);
    if (m % (n / i) == 1)
      count++;
  }
  count++;
  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
long long f(long long n) {
  long long ans = 1;
  for (long long i = 2; i <= n; i++) {
    long long a = 0;
    while (n % i == 0) {
      n /= i;
      a++;
    }
    ans *= (a + 1);
  }
  return ans;
}
int main() {
  long long n;
  cin >> n;
  long long count = 0;
  count += f(n - 1) - 1;
  for (long long i = 2; i <= n; i++) {
    if (n % i != 0)
      continue;
    long long m = n;
    while (m % i == 0)
      m /= i;
    if (m % i == 1)
      count++;
  }
  cout << count << endl;
  return 0;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

// #pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e6 + 5;
const int mod = 1e9 + 7;
ll n, s, val;
set<ll> st;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      st.insert(i);
      st.insert(n / i);
    }
    if ((n - 1) % i == 0) {
      st.insert(i);
      st.insert((n - 1) / i);
    }
  }
  for (ll v : st) {
    if (v == 1)
      continue;
    val = n;
    while (val % v == 0)
      val = val / v;
    if (val % v == 1)
      s++;
  }
  cout << s << endl;
  return 0;
}


Error: TLE

Fixed submission:

// #pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e6 + 5;
const int mod = 1e9 + 7;
ll n, s, val;
set<ll> st;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      st.insert(i);
      st.insert(n / i);
    }
    if ((n - 1) % i == 0) {
      st.insert(i);
      st.insert((n - 1) / i);
    }
  }
  st.insert(n - 1);
  st.insert(n);
  for (ll v : st) {
    val = n;
    while (val % v == 0)
      val = val / v;
    if (val % v == 1)
      s++;
  }
  cout << s << endl;
  return 0;
}