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: Accepted)

use std::collections::HashMap;

fn dfs(pos: usize, value: i64, n: i64, cnts: &[(i64, i32)]) -> i64 {
    if pos >= cnts.len() {
        let mut m = n;

        if value > 1 {
            while m % value == 0 {
                m /= value;
            }
        }
        let res = if m % value == 1 { 1 } else { 0 };

        return res;
    }
    let (v, cnt) = cnts[pos];
    let mut m = n;
    let mut result = 0;
    let mut next_value = value;

    for i in 0..cnt + 1 {
        result += dfs(pos + 1, next_value, m, cnts);
        if i < cnt {
            m /= v;
            next_value *= v;
        }
    }

    result
}
fn solve() {
    let stdin = std::io::stdin();
    let mut scn = Scanner {
        stdin: stdin.lock(),
    };
    let n: i64 = scn.read();
    if n == 2 {
        println!("{}", 1);
        return;
    }
    // for x in 2..n+1 {
    //     let mut m = n;
    //     while m > 0 {
    //         if m % x == 0 {
    //             m /= x;
    //         } else {
    //             m -= x;
    //         }
    //         if m == 1 { break;}
    //     }
    //     if m == 1 {
    //         eprintln!("{} {}", n, x);
    //     }
    // }
    let cnts1: Vec<(i64, i32)> = {
        let mut cnts = HashMap::new();
        let mut m = n;
        let mut div = 2;
        while m > 0 {
            if div * div > m {
                break;
            }
            while m % div == 0 {
                m /= div;
                *cnts.entry(div).or_insert(0) += 1;
            }
            div += 1;
        }
        if m > 1 {
            *cnts.entry(m).or_insert(0) += 1;
        }
        cnts.iter().map(|x| (*x.0, *x.1)).collect()
    };

    let cnts = {
        let mut cnts = HashMap::new();
        let mut m = n - 1;
        let mut div = 2;
        while m > 0 {
            if div * div > m {
                break;
            }
            while m % div == 0 {
                m /= div;
                *cnts.entry(div).or_insert(0) += 1;
            }
            div += 1;
        }
        if m > 1 {
            *cnts.entry(m).or_insert(0) += 1;
        }
        cnts
    };
    //3
    //    let mut value = 1;
    //  eprintln!("{:?}", cnts1);
    let result1 = dfs(0, 1, n, &cnts1);

    let mut result: i64 = 1;
    for x in cnts {
        result *= (x.1 + 1);
    }
    result -= 1;
    //    eprintln!("{} {}", result1, result);

    println!("{}", result + result1);
}

fn main() {
    std::thread::Builder::new()
        .stack_size(64 * 1024 * 1024) // 64MB
        .spawn(|| solve())
        .unwrap()
        .join()
        .unwrap();
}
pub mod utils {
    const DYX: [(isize, isize); 8] = [
        (0, 1),   //right
        (1, 0),   //down
        (0, -1),  //left
        (-1, 0),  //top
        (1, 1),   //down right
        (-1, 1),  //top right
        (1, -1),  //down left
        (-1, -1), //top left
    ];

    pub fn try_adj(y: usize, x: usize, dir: usize, h: usize, w: usize) -> Option<(usize, usize)> {
        let ny = y as isize + DYX[dir].0;
        let nx = x as isize + DYX[dir].1;
        if ny >= 0 && nx >= 0 {
            let ny = ny as usize;
            let nx = nx as usize;
            if ny < h && nx < w {
                Some((ny, nx))
            } else {
                None
            }
        } else {
            None
        }
    }
}

//snippet from kenkoooo
pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn read_line(&mut self) -> String {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b'\n' || b == b'\r')
            .take_while(|&b| b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}