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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;

namespace AtCoder.Contests.ABC161
{
    /// <summary>
    /// 
    /// </summary>
    static class F
    {
        static void Main(string[] args)
        {
            var n = NextLong();

            //n = k^i * (j*k+1) とかける k の数を探す

            //i=0
            var count = ModOneCount(n) * 2 + 1;

            //i>0
            var pf = PrimeFactorization(n);
            foreach (var kv in pf)
            {
                var p = kv.Key;
                var o = kv.Value;

                var num = n / (long)Math.Pow(p, o);
                if (num % p == 1) count++;
            }

            Console.WriteLine(count + 1);
        }
        /// <summary>
        /// n = a * b + 1 とかける aとb の自明でない組み合わせの数を返す
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        static int ModOneCount(long n)
        {
            var f = Math.Sqrt(n);
            var count = 0;
            for (int i = 2; i <= f; i++)
            {
                if (n % i == 1) count++;
            }

            return count;
        }

        static Dictionary<long, int> PrimeFactorization(long n)
        {
            var num = n;
            var f = Math.Sqrt(n);
            var d = new Dictionary<long, int>();
            for (int i = 2; i <= Math.Sqrt(num); i++)
            {
                if (num % i != 0) continue;
                var a = 0;
                while (num % i == 0)
                {
                    num /= i;
                    a++;
                }
                d[i] = a;
            }

            if(num > 1)
            {
                d[num] = 1;
            }

            return d;
        }

        #region Console
        static int _index = 0;
        static string[] _buffer;

        static string Next()
        {
            if (_buffer == null || _index >= _buffer.Length)
            {
                _buffer = Console.ReadLine().Split();
                _index = 0;
            }

            return _buffer[_index++];
        }
        static int NextInt()
        {
            return int.Parse(Next());
        }
        static long NextLong()
        {
            return long.Parse(Next());
        }
        static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }
        static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }
        static int[] ReadIntArray()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }
        static int[] ReadIntArray(int n)
        {
            var a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadInt();
            }
            return a;
        }
        static long[] ReadLongArray()
        {
            return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        }
        static long[] ReadLongArray(int n)
        {
            var a = new long[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadLong();
            }
            return a;
        }
        static List<T> ReadObjects<T>(int n, Func<int[], T> creator)
        {
            var l = new List<T>(n);

            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                l.Add(creator(a));
            }

            return l;
        }

        static int[,] ReadMatrix(int n, int m)
        {
            var r = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                for (int j = 0; j < m; j++)
                {
                    r[i, j] = a[j];
                }
            }
            return r;
        }
        #endregion
    }
}