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 Haskell (GHC 7.10.3) (judge verdict: Wrong Answer)

module Main where

import           Control.Monad
import           Debug.Trace
import qualified Data.Vector.Unboxed           as VU
import           Data.List
import qualified Data.ByteString               as BS
import qualified Data.ByteString.Char8         as BSC8
import           Data.Char
import           Data.Maybe
import qualified Data.Text                     as T
tuplify2 (x : y : _) = (x, y)
tuplify2 _           = undefined

getVUIntFromInputLineAtBSC8 :: IO (VU.Vector Int)
getVUIntFromInputLineAtBSC8 =
    VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
getVUIntListFromInputLineAtBSC8 =
    VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine

readInt = fst . fromJust . BSC8.readInt
readIntTuple = tuplify2 . map readInt . BSC8.words
readIntList = map readInt . BSC8.words

getInt = readInt <$> BSC8.getLine
getIntList = readIntList <$> BSC8.getLine
getIntNList n = map readIntList <$> replicateM (fromIntegral n) BSC8.getLine
getIntMatrix = map readIntList . BSC8.lines <$> BS.getContents
getIntTuple = readIntTuple <$> BSC8.getLine
getIntNTuples n = map readIntTuple <$> replicateM (fromIntegral n) BSC8.getLine
getIntTuples = map readIntTuple . BSC8.lines <$> BSC8.getContents

main :: IO ()
main = do

    aN <- getInt

    print $ getK aN

getK n= length (nub((getBaiBaiK n) ++ (getkakekakeK n)))+2

getBaiBaiK n = 
        let ret = getBaiBaiK' n [2..] 
        in if ret==0
            then [] 
            else [ret]

getBaiBaiK' n (x:xs) = 
    if (mod n x == 0) 
        then 
            if getBaiBaiK'' n x 
                then x 
                else 0 
        else getBaiBaiK' n xs 

getBaiBaiK'' n x = 
    if x*x > n 
        then False
        else 
            if mod n x == 0 
                then getBaiBaiK'' (div n x) x
                else False 

getkakekakeK :: Int-> [Int]
getkakekakeK n = 
    let ret' = (getkakekakeK' (n-1) [2..] [])
        ret = delete 1( map (product) (nub(subsequences ret')))
    in --trace (show ret' ++ show ret) 
       ret
getkakekakeK' :: Int->[Int]->[Int]->[Int]
getkakekakeK' n (x:xs) ys = 
    --trace ("n:"++(show n)++"x:"++(show x)++"ys:"++(show ys))$
    if (x * 2) > n 
        then ys ++ [n]
        else
            if mod n x == 0 
                then getkakekakeK' (div n x) [2..] (ys++[x])  
                else getkakekakeK' n xs ys