Problem:
Score : 400 points

Problem Statement

For a positive integer X, let f(X) be the number of positive divisors of X.

Given a positive integer N, find \sum_{K=1}^N K\times f(K).

Constraints

1 \leq N \leq 10^7

Input

Input is given from Standard Input in the following format:

N

Output

Print the value \sum_{K=1}^N K\times f(K).

Sample Input 1

4

Sample Output 1

23

We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23.

Sample Input 2

100

Sample Output 2

26879

Sample Input 3

10000000

Sample Output 3

838627288460105

Watch out for overflows.

Solution in Rust (1.42.0) (judge verdict: Time Limit Exceeded)

#[allow(unused_imports)]
use std::cmp::{max, min};
use std::collections::*;
use std::io::{stdin, Read};

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: tt),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, u1) => {
        $it.next().unwrap().parse::<usize>().unwrap() -1
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}

fn add(a: &mut HashMap<usize, usize>, b: &HashMap<usize, usize>) {
    for (k, v) in b {
        *a.entry(*k).or_default() += v;
    }
}

fn count(a: &HashMap<usize, usize>) -> usize {
    let mut ret = 1;
    for (_, v) in a {
        ret *= v + 1
    }
    ret
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, n: usize);
    let mut memo = vec![HashMap::new(); n + 1];

    let mut d = 2;
    while d <= n {
        for i in (d..=n).step_by(d) {
            let mut count = 0;
            let mut cur = i;
            loop {
                if cur % d != 0 {
                    break;
                } else {
                    count += 1;
                    cur /= d;
                }
            }
            memo[i].insert(d, count);
        }
        let mut flag = false;
        for i in (d + 1)..=n {
            if memo[i].len() == 0 {
                d = i;
                flag = true;
                break;
            }
        }
        if !flag {
            break;
        }
    }
    let mut ret = 1;
    for i in 2..=n {
        ret += i * count(&memo[i]);
    }

    println!("{}", ret);
}

fn primes(mut n: usize) -> usize {
    let mut ret = std::collections::HashMap::new();
    let n0 = n;
    let mut cur = 2;
    while cur * cur <= n0 {
        if n % cur != 0 {
            cur += 1;
            continue;
        }
        let mut count = 0;
        while n % cur == 0 {
            n /= cur;
            count += 1;
        }
        ret.insert(cur, count);
        cur += 1;
    }
    count(&ret)
}

fn get_primes(n: usize, memo: &mut Vec<HashMap<usize, usize>>) {
    if memo[n].len() != 0 {
        return;
    }

    let mut ret = HashMap::new();
    let n0 = n;
    let mut cur = 2;
    while cur * cur <= n0 {
        if n % cur != 0 {
            cur += 1;
            continue;
        }
        get_primes(cur, memo);
        get_primes(n / cur, memo);
        ret = memo[cur].clone();
        let b = &memo[n / cur];
        add(&mut ret, b);
        break;
    }
    if ret.len() == 0 {
        ret.insert(n, 1);
    }
    memo[n] = ret;
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}