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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::io::*;
use std::str::FromStr;
use std::collections::HashMap;
use std::collections::HashSet;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char) 
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn trial_division(mut n :u64) -> HashMap<u64, u64>{
    let mut primes = HashMap::new();
    let mut i = 2;

    while i * i <= n {
        while n % i == 0 {
            n /= i;
            if primes.contains_key(&i) {
                let mut x = primes.get_mut(&i).unwrap();
                *x += 1;
            } else {
                primes.insert(i, 1);
            }
        }
        i+=1;
    }

    if n > 1 {
        if primes.contains_key(&n) {
            let mut x = primes.get_mut(&n).unwrap();
            *x += 1;
        } else {
            primes.insert(n, 1);
        }
    }
    primes
}

fn check(n: u64, k: u64) -> bool {
    let mut n = n;
    while n > 1 {
        // println!("{} {}", n, k);
        if n % k == 0 {
            n = n / k;
        } else {
            n = n % k;
            break;
        }
    }
    return n == 1;
}

fn main() {
    let n: u64 = read();
    let prime_map1 = trial_division(n);
    let prime_map2 = trial_division(n-1);
    // println!("{:?}", prime_map1);
    // println!("{:?}", prime_map2);

    let mut kouho: Vec<u64> = Vec::new();
    kouho.push(2);
    kouho.push(n-1);
    kouho.push(n);
    for prime_map in vec![prime_map1, prime_map2] {
        let mut addend: Vec<u64> = Vec::new();
        for (prime, cnt) in &prime_map {
            for i in 1..cnt+1 {
                let multiplier = prime * i;
                for base in addend.clone().into_iter() {
                    if base * multiplier <= n {
                        addend.push(base * multiplier);
                    }
                }
                addend.push(multiplier);
            }
        }
        kouho.extend(&addend);
        let uniq: HashSet<u64> = kouho.into_iter().collect();
        kouho = uniq.into_iter().collect();
    }
    // println!("{:?}", kouho);

    let mut ans = 0;
    for k in kouho.into_iter() {
        if check(n, k) {
            ans += 1;
        }
    }

    println!("{}", ans);
}