Divisor Summation_

Divisor Summation

Problem Description

Give a natural number n (1 <= n <= 500000), please tell the summation of all its proper divisors. Definition: A proper divisor of a natural number is the divisor that is strictly less than the number.

e.g. number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.

Input

An integer stating the number of test cases, and that many lines follow each containing one integer between 1 and 500000.

Output

One integer each line: the divisor summation of the integer given respectively.

Sample Input

3
2
10
20

###Sample Output

1
8
22

参考代码

int sum[500001];
int main()
{
int n, a;

sum[1] = 0;
for (int k = 2; k <= 500000; k++)
    sum[k] = 1;
for (int i = 2; i <=250000; i++)
{
    for (int j = 2; j <= 500000/i; j++)
        sum[j*i] += i;
}
//freopen("d:\\out.txt", "w", stdout);
while (scanf("%d", &n) != EOF)
{
    for (int i = 0; i < n;i++)//坑啊 改了两次竟然没发现少写了个循环 一定要细心
    {
        scanf("%d", &a);

        printf("%d\n", sum[a]);
    }
    
}
//system("pause");
}

如果用一般的暴力解法的话会超时,用打表法反而更快

你可能感兴趣的:(Divisor Summation_)