Problem:
Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.

Solution in Go (1.6) (judge verdict: Accepted)

package main

import (
    "fmt"
    "math"
)

var num int
var nc map[int]int
var ps []int
var ns [][]int
var sum int

func main() {
    fmt.Scan(&num)

    rn := int(math.Sqrt(float64(num)))
    makePl(rn)

    nc = make(map[int]int)
    pf(num)

    //fmt.Println(nc)

    ns = make([][]int, 0)
    for k, v := range nc {
        ns = append(ns, []int{k, v})
    }

    calc(0, 1)

    fmt.Println(sum)
}

// 2^3 = 8 0,2,4,8
// 3^2 = 9 0,3,9
func calc(k, n int) {
    if k >= len(ns) {
        if n > 2 && num%(n-1) != 0 && num/n < n-1 {
            //fmt.Println(n - 1)
            sum += (n - 1)
        }
        return
    }
    //fmt.Println(ns, k)
    p, cnt := ns[k][0], ns[k][1]
    calc(k+1, n) // 0
    for i := 1; i <= cnt; i++ {
        n *= p
        calc(k+1, n)
    }
}

func makePl(n int) {
    var npm = make(map[int]bool)
    for i := 2; i <= n; i++ {
        if b, ok := npm[i]; ok && b {
            continue
        }
        for j := i * 2; j <= n; j += i {
            npm[i] = true
        }
        ps = append(ps, i)
    }
}

func pf(n int) {
    if n <= 1 {
        return
    }
    for _, p := range ps {
        for n%p == 0 {
            nc[p]++
            n /= p
        }
    }
    if n != 1 {
        nc[n]++
    }
}