Problem:
Score : 600 points

Problem Statement

Given is a positive integer N.

We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.

Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K.

In how many choices of K will N become 1 in the end?

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of choices of K in which N becomes 1 in the end.

Sample Input 1

6

Sample Output 1

3

There are three choices of K in which N becomes 1 in the end: 2, 5, and 6.

In each of these choices, N will change as follows:

When K=2: 6 \to 3 \to 1

When K=5: 6 \to 1

When K=6: 6 \to 1

Sample Input 2

3141

Sample Output 2

13

Sample Input 3

314159265358

Sample Output 3

9

Solution in C++14 (GCC 5.4.1) (judge 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 (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;
}