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 LuaJIT (2.0.4) (judge verdict: Wrong Answer)

local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs

local function getprimes(x)
  local primes = {}
  local allnums = {}
  for i = 1, x do allnums[i] = true end
  for i = 2, x do
    if allnums[i] then
      table.insert(primes, i)
      local lim = mfl(x / i)
      for j = 2, lim do
        allnums[j * i] = false
      end
    end
  end
  return primes
end

local function getdivisorparts(x, primes)
  local prime_num = #primes
  local tmp = {}
  local lim = mce(msq(x))
  local primepos = 1
  local dv = primes[primepos]
  while primepos <= prime_num and dv <= lim do
    if x % dv == 0 then
      local t = {}
      t.p = dv
      t.cnt = 1
      x = x / dv
      while x % dv == 0 do
        x = x / dv
        t.cnt = t.cnt + 1
      end
      table.insert(tmp, t)
      lim = mce(msq(x))
    end
    if primepos == prime_num then break end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if x ~= 1 then
    local t = {}
    t.p, t.cnt = x, 1
    table.insert(tmp, t)
  end
  return tmp
end

local function getdivisorCore(divisorparts)
  local t = {}
  local pat = 1
  local len = #divisorparts
  local allpat = 1
  for i = 1, len do
    allpat = allpat * (1 + divisorparts[i].cnt)
  end
  for t_i_pat = 0, allpat - 1 do
    local div = allpat
    local i_pat = t_i_pat
    local ret = 1
    for i = 1, len do
      div = mfl(div / (divisorparts[i].cnt + 1))
      local mul = mfl(i_pat / div)
      i_pat = i_pat % div
      for j = 1, mul do
        ret = ret * divisorparts[i].p
      end
    end
    table.insert(t, ret)
  end
  -- table.sort(t)
  return t
end

local function getdivisor(x, primes)
  local dvp = getdivisorparts(x, primes)
  return getdivisorCore(dvp)
end

local n = io.read("*n")
-- n = k^a * (bk + 1)
-- n = 1000 * 1000 * 1000 * 1000
-- 2 <= a
local a = 1
local ret = 0
while true do
  a = a + 1
  if n < (2^a) then break end
  local k = 1
  while true do
    k = k + 1
    local ka = 1
    for i = 1, a do
      ka = ka * k
    end
    if n < ka then
      --print(a, k, ka)
      break
    end
    if n % ka == 0 then
      local rem = mfl(n / ka)
      if rem % k == 1 then ret = ret + 1 end
    end
  end
end
-- a == 1
-- a == 1 and 0 < b
for k = 2, 1000000 do
  if k * k <= n then
    if n % (k * k) == k then ret = ret + 1 end
    -- local rem = mfl(n / (k * k))
    -- if rem % k == 1 then ret = ret + 1 end
  end
end
-- a == 1 and b == 0
if 2 < n then ret = ret + 1 end
-- a == 0
if 2 < n then
  local primes = getprimes(1000000)
  local divisor = getdivisor(n - 1, primes)
  -- print(table.concat(divisor, " "))
  ret = ret + #divisor - 1
end
print(ret)