SPOJ DIVSUM - Divisor Summation

题目链接http://www.spoj.com/problems/DIVSUM/

题目大意:问N的除自己外所有因数的和。例如250 是 218(1 + 2 + 5 + 10 + 25+ 50 +125).

解题思路:采用类似埃氏筛的方法。 不知道暴力能不能过。。。

代码:

 1 int n;
 2 ll a[maxn];
 3 
 4 void solve(){
 5     memset(a, 0, sizeof(a));
 6     ll ans = 0;
 7     for(int i = 1; i * 2 <= 500000; i++){
 8         a[i] -= i;
 9         for(int k = i; k <= 500000; k += i){
10             if(k % i == 0) a[k] += i;
11         }
12     }
13 } 
14 int main(){
15     solve();
16     int t;
17     scanf("%d", &t);
18     while(t--){
19         scanf("%d", &n);
20         printf("%lld\n", a[n]);
21     }
22 }

题目:

DIVSUM - Divisor Summation

 

Given a natural number n (1 <= n <= 500000), please output 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 (equal to about 200000), and that many lines follow, each containing one integer between 1 and 500000 inclusive.

Output

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

Example

Sample Input:
3
2
10
20

Sample Output:
1
8
22

Warning: large Input/Output data, be careful with certain languages

posted @ 2017-08-24 10:10  EricJeffrey  阅读(127)  评论(0编辑  收藏  举报