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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // uint64_t
#include <math.h>

#define BUF_SIZE 20

// size: specify sizeof(str)
int get_str(char *str, int size) {
    if(!fgets(str, size, stdin)) return -1;
    return 0;
}

uint64_t get_int(void) {
  uint64_t num;
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return 0;
  sscanf(line, "%llu", &num);
#else
#error
#endif
  return num;
}
#define PRIME_MAX 1000000

uint64_t get_divisors(uint64_t num, int *prime, int size, int *divisors, int *idx) {
    int pidx = 0;
    int div_idx = 0;
    while(num>1) {
        if(pidx >= size) {
            goto finish;
        }
        int prim = prime[pidx];
        int div = num%prim;
        if(div) { pidx++; continue; }
#ifdef DEBUG
        printf("defactor: %d\n", prim);
#endif
        divisors[div_idx++] = prim;
        num /= prim;
    }
finish:
    *idx = div_idx;
    return num;
}

int main(void) {
    uint64_t num = get_int();
    //int num_2 = sqrt(0.5 + num);
    static char prime_map[PRIME_MAX+1];
    memset(prime_map, 0x01, PRIME_MAX+1);
    static int prime[PRIME_MAX];
    int pidx = 0;
    int i, j;
    // sieve of eratosthenes
    for(i = 2; i <= PRIME_MAX; i++) {
        if(!prime_map[i]) continue;
        prime[pidx++] = i;
        int ulimit = PRIME_MAX/i;
        for(j = 2; j <= ulimit; j++) {
            prime_map[i*j] = 0;
        }
    }
#ifdef DEBUG
    printf("%d\n", pidx);
#endif

    int divisors1[100] = {0};
    int divisors[100] = {0};
    int idx = 0;
    // get_cnt
    uint64_t big_prime1 = get_divisors(num-1, prime, pidx, divisors1, &idx);
    uint64_t ans = (big_prime1 > 1) ? 2 : 1;
    int prev = divisors1[0];
    int cnt = 1;
    for(i = 1; i < idx+1; i++) {
        int cur = divisors1[i];
        if(cur != prev) { ans *= (cnt+1); cnt = 1; continue; }
        cnt++;
    }
    ans--;
#ifdef DEBUG
    printf("ans: %llu\n", ans);
#endif

    idx = 0;
    uint64_t big_prime = get_divisors(num, prime, pidx, divisors, &idx);
    uint64_t divs[100]; int didx = 0;

    prev = divisors[0];
    cnt = 1;
    uint64_t n;
    for(i = 1; i < idx+1; i++) {
        int cur = divisors[i];
        if(prev == cur) { cnt++; continue; }
        n = prev;
        while(cnt) {
            divs[didx++] = n;
            n *= n;
            cnt /= 2;
        }
        cnt = 1;
        prev = cur;
    }
    if(big_prime>1) divs[didx++] = big_prime;

#ifdef DEBUG
    printf("idx: %d\n", didx);
    for(i = 0; i < didx; i++) {
        printf("%llu ", divs[i]);
    }
#endif
    uint64_t pat;
    int bit;
    for(pat = 1; pat < (1<<didx); pat++) {
        uint64_t k = 1;
        for(bit = 0; bit < didx; bit++) {
            if(pat & (1<<bit)) k *= divs[bit];
        }
        if(k > num) continue;
#ifdef DEBUG
        printf("k: %llu\n", k);
#endif
        uint64_t n = num;
        while(1) {
            uint64_t rem = (n%k);
            if(rem) {
                ans += (rem==1);
                break;
            }
            n /= k;
        }
    }
    printf("%llu\n", ans);
    return 0;
}