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 Rust (1.15.1) (judge verdict: Runtime Error)

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input!{
        n: usize
    }
    let mut fact = factorial(n);
    fact.sort_by_key(|x| x.1);
    let ps: Vec<usize> = fact.iter().map(|f| f.0).collect();
    let rs: Vec<usize> = fact.iter().map(|f| f.1).collect();
    let mut v: Vec<usize> = vec![0; rs.len()];
    let l = v.len();
    let mut ans = 0;
    loop {
        let d = ps.iter().zip(v.iter()).fold(1, |ac, f| ac * pow(*f.0, *f.1));
        let m = n / d - 1;
        if d < m {
            ans += m;
            v[l - 1] += 1;
        } else {
            v[l - 1] = rs[l - 1] + 1;
        }
        for i in (1..l).rev() {
            if v[i] == rs[i] + 1 {
                v[i] = 0;
                v[i - 1] += 1;
            } else {
                break;
            }
        }
        if v[0] == rs[0] + 1 {
            break;
        }
    }
    println!("{}", ans);
}

fn factorial(mut n: usize) -> Vec<(usize, usize)> {
    let mut ret: Vec<(usize, usize)> = Vec::new();
    let mut r = 0;
    while n % 2 == 0 {
        r += 1;
        n /= 2;
    }
    if r > 0 {
        ret.push((2, r));
        r = 0;
    }
    while n % 3 == 0 {
        r += 1;
        n /= 3;
    }
    if r > 0 {
        ret.push((3, r));
        r = 0;
    }
    let mut ps: Vec<usize> = vec![3];
    let mut p = 5;
    while n > 1 && p * p <= n {
        while ps.iter().any(|&x| p % x == 0) {
            p += 2;
        }
        ps.push(p);
        while n % p == 0 {
            r += 1;
            n /= p;
        }
        if r > 0 {
            ret.push((p, r));
            r = 0;
        }
    }
    if n > 1 {
        ret.push((n, 1));
    }
    ret
}

fn pow(n: usize, mut p: usize) -> usize {
    let mut ret = 1;
    let mut r = n;
    while p > 0 {
        if p % 2 == 0 {
            r *= r;
            p /= 2;
        } else {
            ret *= r;
            p -= 1;
        }
    }
    ret
}