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

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい
// for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define big 1000000007
#define all(a) sort((a).begin(), (a).end()) // ソートのマクロ
#define Re(a) reverse((a).begin(), (a).end())
#define YN(a)                                                                  \\
  if (a) {                                                                     \\
    cout << "Yes" << endl;                                                     \\
  } else                                                                       \\
    cout << "No" << endl; // 条件によってYes、Noを出力する

int main() {
  int64_t n;
  cin >> n;
  int64_t count = 0;
  set<int64_t> s;
  s.insert(n);
  if (n >= 3) {
    s.insert(n - 1);
  }
  for (int64_t i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      s.insert(i);
      s.insert(n / i);
    }
  }
  for (int64_t i = 2; i * i <= n; i++) {
    if ((n - 1) % i == 0) {
      s.insert(i);
      s.insert((n - 1) / i);
    }
  }
  for (auto c : s) {
    // cout<<c<<endl;
    int64_t k = c;
    int64_t N = n;
    if (N % k == 0) {
      while (N % k == 0) {
        N = N / k;
      }
      if (N % k == 1) {
        count++;
      }
    } else if (N % k == 1) {
      count++;
    }
  }
  // cout<<count<<endl;
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい
// for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define big 1000000007
#define all(a) sort((a).begin(), (a).end()) // ソートのマクロ
#define Re(a) reverse((a).begin(), (a).end())
#define YN(a)                                                                  \\
  if (a) {                                                                     \\
    cout << "Yes" << endl;                                                     \\
  } else                                                                       \\
    cout << "No" << endl; // 条件によってYes、Noを出力する

int main() {
  int64_t n;
  cin >> n;
  int64_t count = 0;
  set<int64_t> s;
  s.insert(n);
  s.insert(n - 1);
  for (int64_t i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      s.insert(i);
      s.insert(n / i);
    }
  }
  for (int64_t i = 2; i * i <= n; i++) {
    if ((n - 1) % i == 0) {
      s.insert(i);
      s.insert((n - 1) / i);
    }
  }
  for (auto c : s) {
    // cout<<c<<endl;
    int64_t k = c;
    int64_t N = n;
    if (N % k == 0) {
      while (N % k == 0) {
        N = N / k;
      }
      if (N % k == 1) {
        count++;
      }
    } else if (N % k == 1) {
      count++;
    }
  }
  // cout<<count<<endl;
  cout << count << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())

using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ld eps = 1e-9;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll << 50;

template <typename T> void printv(const vector<T> &s) {
  for (int i = 0; i < (int)(s.size()); ++i) {
    cout << s[i];
    if (i == (int)(s.size()) - 1)
      cout << endl;
    else
      cout << " ";
  }
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

int calc(ll n, ll i) {
  if (n < i)
    return n;
  else if (n % i == 0)
    return calc(n / i, i);
  else
    return n % i;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  if (n == 2) {
    cout << 1 << endl;
    return 0;
  }

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

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

  ll cnt = 0;
  for (auto &e : st) {
    cnt += calc(n, e) == 1;
  }
  cout << cnt + 2 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())

using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ld eps = 1e-9;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll << 50;

template <typename T> void printv(const vector<T> &s) {
  for (int i = 0; i < (int)(s.size()); ++i) {
    cout << s[i];
    if (i == (int)(s.size()) - 1)
      cout << endl;
    else
      cout << " ";
  }
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

int calc(ll n, ll i) {
  if (n < i)
    return n;
  else if (n % i == 0)
    return calc(n / i, i);
  else
    return n % i;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;

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

  ll cnt = 0;
  for (auto &e : st) {
    cnt += calc(n, e) == 1;
  }
  cout << cnt << endl;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long

ll n;
set<ll> st;

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 (n / i != i)
        res.push_back(n / i);
    }
  }
  return res;
}

void solve(ll n, ll m) {
  vector<ll> v = divisor(n);
  for (ll i = 0; i < v.size(); i++) {
    ll k = v[i];
    if (k == 1)
      continue;
    ll d = m;
    while (d >= k) {
      if (d % k == 0)
        d /= k;
      else
        break;
    }
    d %= k;
    if (d == 1)
      st.insert(k);
  }
}

int main() {
  cin >> n;
  solve(n, n);
  solve(n - 1, n);
  cout << st.size() << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long
#define ins insert
#define pb push_back
#define sz size()

ll n;
set<ll> st;

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

void solve(ll n, ll m) {
  vector<ll> v = divisor(n);
  for (int i = 0; i < v.sz; i++) {
    ll k = v[i];
    if (k == 1)
      continue;
    ll d = m;
    while (d >= k) {
      if (d % k == 0)
        d /= k;
      else
        break;
    }
    d %= k;
    if (d == 1)
      st.ins(k);
  }
}

int main() {
  cin >> n;
  solve(n, n);
  solve(n - 1, n);
  cout << st.sz << endl;
  return 0;
}
Problem p02722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

llint n;
vector<llint> vec, avec;

void get(llint n) {
  vec.clear();
  for (llint i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      vec.push_back(i);
      if (i * i != n)
        vec.push_back(n / i);
    }
  }
  vec.push_back(n);
}

bool calc(llint k) {
  llint t = n;
  while (k <= t) {
    if (t % k == 0)
      t /= k;
    else
      t %= k;
  }
  return t == 1;
}

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

  cin >> n;

  llint ans = 0;
  get(n);
  for (int i = 0; i < vec.size(); i++) {
    if (calc(vec[i]))
      ans++;
  }

  if (n > 2) {
    get(n - 1);
    for (int i = 0; i < vec.size(); i++) {
      if (calc(vec[i]))
        ans++;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

llint n;
vector<llint> vec, avec;

void get(llint n) {
  vec.clear();
  for (llint i = 2; i * i <= n; i++) {
    if (n % i == 0)
      vec.push_back(i), vec.push_back(n / i);
  }
  vec.push_back(n);
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
}

bool calc(llint k) {
  llint t = n;
  while (k <= t) {
    if (t % k == 0)
      t /= k;
    else
      t %= k;
  }
  return t == 1;
}

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

  cin >> n;

  llint ans = 0;
  get(n);
  for (int i = 0; i < vec.size(); i++) {
    if (calc(vec[i]))
      ans++;
  }

  get(n - 1);
  for (int i = 0; i < vec.size(); i++) {
    if (calc(vec[i]))
      ans++;
  }

  cout << ans << endl;

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

#include <algorithm>
#include <boost/range/adaptors.hpp>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <tuple>
#include <unordered_map>
#include <vector>

std::map<int64_t, int> prime_factor(int64_t n) {
  std::map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

std::vector<int64_t> divisor(int64_t n) {
  std::vector<int64_t> ret;
  for (int64_t 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() {
  long N;
  std::cin >> N;

  auto result = prime_factor(N - 1);
  long long count = 1;
  for (auto p : result) {
    count *= (p.second + 1);
    // std::cout << p.first << ": " << p.second << std::endl;
    // while(p.second--) std::cout << " " << p.first;
  }
  --count;

  auto result_ = divisor(N);
  for (int i = 1; i < result_.size() - 1; ++i) {
    // if (result_[i] * result_[i] > N) {
    //     break;
    // }
    // std::cout << result_[i] << std::endl;
    // std::cout << result_[i] * result_[i] << std::endl;
    // std::cout << N << std::endl;
    auto value = N;
    while (value % result_[i] == 0)
      value = value / result_[i];
    if (value % result_[i] == 1)
      ++count;
  }
  ++count;
  std::cout << count << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <boost/range/adaptors.hpp>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <tuple>
#include <unordered_map>
#include <vector>

std::map<int64_t, int> prime_factor(int64_t n) {
  std::map<int64_t, int> ret;
  for (int64_t i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

std::vector<int64_t> divisor(int64_t n) {
  std::vector<int64_t> ret;
  for (int64_t 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() {
  long N;
  std::cin >> N;

  auto result = prime_factor(N - 1);
  long count = 1;
  for (auto p : result) {
    count *= (p.second + 1);
    // std::cout << p.first << ": " << p.second << std::endl;
    // while(p.second--) std::cout << " " << p.first;
  }
  --count;

  auto result_ = divisor(N);
  for (int i = 1;; ++i) {
    if (result_[i] * result_[i] > N) {
      break;
    }
    // std::cout << result_[i] << std::endl;
    // std::cout << result_[i] * result_[i] << std::endl;
    // std::cout << N << std::endl;
    auto value = N;
    while (value % result_[i] == 0)
      value = value / result_[i];
    if (value % result_[i] == 1)
      ++count;
  }
  ++count;
  std::cout << count << std::endl;

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

#include <bits/stdc++.h>

using namespace std;

// kai
int main(void) {
  long long N;
  long long counter = 0;
  long long tmp, g;

  cin >> N;
  // 割り算が一度もない場合N%K=1であればいい
  // これを全探索するとO(N)だが、
  // N-1の約数のうち1でないものを探せばよく、これはO(sqrtN)
  for (long long i = 1; i * i <= N - 1; i++) {
    if ((N - 1) % i == 0) {
      if (i != 1) {
        counter++;
      }
      if (i != (N - 1) / i) {
        counter++;
      }
    }
  }
  // for(long long i=1;i<=sqrt(N-1);i++){
  //   if(i==1){
  //     counter++; //N-1の分
  //   }else if((N-1) == i*i){
  //     counter++;
  //     // cout << "first: " << i << endl;
  //   }else if((N-1)%i == 0){
  //     counter+=2;
  //     // cout << "first: " << i << endl;
  //   }
  // }

  // 割り算を一度以上する場合
  // そのようなKはNの約数 - O(sqrtN)
  // その後確約数ごとに割りまくって余りを出す
  vector<long long> yakusu;
  for (long long i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      if (i != 1) {
        yakusu.push_back(i);
      }
      if (i != N / i) {
        yakusu.push_back(N / i);
      }
    }
  }
  // for(long long i=1;i<=sqrt(N);i++){
  //   if(N == i*i) yakusu.push_back(i);
  //   else if(N%i == 0){
  //     yakusu.push_back(i);
  //     yakusu.push_back(N/i);
  //   }
  // }
  for (auto itr = yakusu.begin(); itr != yakusu.end(); itr++) {
    g = *itr;
    tmp = N / g;
    while (tmp % g == 0) {
      tmp /= g;
    }
    if (tmp % g == 1) {
      counter++;
    }
  }

  cout << counter << endl;

  return 0;
}

// TLE
//  int main(void){
//    long long N,tmp;
//    long long counter=0;

//   cin >> N;

//   for(long long k=N;k>1;k--){
//     tmp = N;
//     while(tmp>=k){
//       // cout << "tmp%k:" << tmp%k << endl;
//       if(tmp%k==0){
//         tmp /= k;
//         // cout << "tmp:" << tmp << " k:" << k << endl;
//       }
//       else{
//         tmp %= k;
//         break;
//       }
//       // cout << "tmp:" << tmp << " k:" << k << endl;
//     }
//     if(tmp==1) counter++;
//   }

//   cout << counter << endl;

//   return 0;
// }

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  long long N, tmp;
  long long counter = 0;

  cin >> N;

  for (long long k = N; k > 1; k--) {
    tmp = N;
    while (tmp >= k) {
      // cout << "tmp%k:" << tmp%k << endl;
      if (tmp % k == 0) {
        tmp /= k;
        // cout << "tmp:" << tmp << " k:" << k << endl;
      } else {
        tmp %= k;
        break;
      }
      // cout << "tmp:" << tmp << " k:" << k << endl;
    }
    if (tmp == 1)
      counter++;
  }

  cout << counter << endl;

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;

ll N;

map<ll, int> insu;
vector<ll> crt, nxt;

ll mpow(ll a, int b) {
  ll base = 1, kakeru = a;
  while (b > 0) {
    if (b & 1)
      base *= kakeru;
    kakeru *= kakeru;
    b >>= 1;
  }
  return base;
}

void soinsu(ll x) {
  for (ll i = 2; i * i <= x; i++) {
    int cnt = 0;
    while (x % i == 0)
      x /= i, cnt++;
    if (cnt != 0)
      insu[i] = cnt;
  }
  if (x != 1) {
    insu[x]++;
  }

  crt.push_back(1);
  for (auto it = insu.begin(); it != insu.end(); it++) {
    for (int i = 0; i < crt.size(); i++) {
      ll times = 1;
      for (int j = 0; j <= (*it).second; j++) {
        nxt.push_back(crt[i] * times);
        times *= (*it).first;
      }
    }
    swap(crt, nxt);
    nxt.clear();
  }
}

int main() {
  cin >> N;
  if (N == 2) {
    cout << 1 << endl;
    return 0;
  }
  soinsu(N - 1);
  sort(crt.begin(), crt.end());

  ll cnt = 0;
  for (ll i = 2; i * i <= N; i++) {
    ll cN = N;
    while (cN % i == 0)
      cN /= i;
    if (cN % i == 1)
      cnt++;
  }

  cout << cnt + crt.size() -
              (lower_bound(crt.begin(), crt.end(), sqrt(N)) - crt.begin()) + 1
       << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;

ll N;

map<ll, int> insu;
vector<ll> crt, nxt;

ll mpow(ll a, int b) {
  ll base = 1, kakeru = a;
  while (b > 0) {
    if (b & 1)
      base *= kakeru;
    kakeru *= kakeru;
    b >>= 1;
  }
  return base;
}

void soinsu(ll x) {
  for (ll i = 2; i * i <= x; i++) {
    int cnt = 0;
    while (x % i == 0)
      x /= i, cnt++;
    if (cnt != 0)
      insu[i] = cnt;
  }
  if (x != 1) {
    insu[x]++;
  }

  crt.push_back(1);
  for (auto it = insu.begin(); it != insu.end(); it++) {
    for (int i = 0; i < crt.size(); i++) {
      for (int j = 0; j <= (*it).second; j++) {
        nxt.push_back(crt[i] * mpow((*it).first, j));
      }
    }
    swap(crt, nxt);
    nxt.clear();
  }
}

int main() {
  cin >> N;
  if (N == 2) {
    cout << 1 << endl;
    return 0;
  }
  soinsu(N - 1);
  sort(crt.begin(), crt.end());

  ll cnt = 0;
  for (int i = 2; i * i <= N; i++) {
    ll cN = N;
    while (cN % i == 0)
      cN /= i;
    if (cN % i == 1)
      cnt++;
  }

  cout << cnt + crt.size() -
              (lower_bound(crt.begin(), crt.end(), sqrt(N)) - crt.begin()) + 1
       << endl;

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

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

vector<long long> div(long long n) {
  vector<long long> a;

  for (long long i = 1; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    a.push_back(i);
    if (i * i != n)
      a.push_back(n / i);
  }

  return a;
}

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

  long long ans = 0;
  for (long long x : div(n)) {
    if (x == 1)
      continue;
    long long k = n;
    while (k % x == 0)
      k /= x;
    if (k % x == 1)
      ans++;
  }
  ans += div(n - 1).size() - 1;

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

vector<long long> div(long long n) {
  vector<long long> a;

  for (int i = 1; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    a.push_back(i);
    if (i * i != n)
      a.push_back(n / i);
  }

  return a;
}

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

  long long ans = 0;
  for (long long x : div(n)) {
    if (x == 1)
      continue;
    long long k = n;
    while (k % x == 0)
      k /= x;
    if (k % x == 1)
      ans++;
  }
  ans += div(n - 1).size() - 1;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
#define ALL(x) (x).begin(), (x).end()

ll N;
inline ll Pow(ll a) {
  ll res = a;
  while (N >= res) {
    res *= a;
    if (N % res != 0) {
      res /= a;
      break;
    }
  }
  if (N % res != 0)
    res /= a;
  return res;
}

int main() {
  cin >> N;
  if (N == 2) {
    cout << 1 << endl;
    return 0;
  }
  ll ans = 2;
  for (ll k = 2; k * k <= N; k++) {
    if (N % k == 0)
      continue;
    if ((N - 1) % k == 0) {
      ans++;
      ll tmp = (N - 1) / k;
      if (tmp == k)
        continue;
      if (N % tmp != 0) {
        ans++;
      }
    }
  }
  for (ll k = 2; k * k <= N; k++) {
    if (N % k == 0) {
      ll kk = Pow(k);
      if ((N / kk) % k == 1) {
        ans++;
      }
      ll nk = N / k;
      if (N % nk == 1) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
#define ALL(x) (x).begin(), (x).end()

ll N;
inline ll Pow(ll a) {
  ll res = a;
  while (N >= res) {
    res *= a;
    if (N % res != 0) {
      res /= a;
      break;
    }
  }
  if (N % res != 0)
    res /= a;
  return res;
}

int main() {
  cin >> N;
  if (N == 2) {
    cout << 1 << endl;
    return 0;
  }
  ll ans = 2;
  for (ll k = 2; k * k <= N; k++) {
    if (k * k == N and (N - 1) % k == 0) {
      ans++;
    } else if ((N - 1) % k == 0)
      ans += 2;
  }
  for (ll k = 2; k * k <= N; k++) {
    if (N % k == 0) {
      ll kk = Pow(k);
      if ((N / kk) % k == 1) {
        ans++;
      }
      ll nk = N / k;
      kk = Pow(nk);
      if ((N / kk) % 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 < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n;
  cin >> n;
  vector<ll> g;

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

  int ans = 0;
  for (ll x : g) {
    ll dn = n;
    while (dn >= x) {
      if (dn % x == 0)
        dn /= x;
      else
        dn %= x;
    }
    if (dn == 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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n;
  cin >> n;
  vector<ll> g;

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

  int ans = 0;
  for (ll x : g) {
    ll dn = n;
    while (dn >= x) {
      if (dn % x == 0)
        dn /= x;
      else
        dn -= x;
    }
    if (dn == 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}