lightoj-1098

1098 - A New Function
PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 32 MB
We all know that any integer number n is divisible by 1 and n. That is why these two numbers are not the actual divisors of any numbers. The function SOD(n) (sum of divisors) is defined as the summation of all the actual divisors of an integer number n. For example,

SOD(24) = 2+3+4+6+8+12 = 35.

The function CSOD(n) (cumulative SOD) of an integer n, is defined as below:


Given the value of n, your job is to find the value of CSOD(n).

Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case contains an integer n (0 ≤ n ≤ 2 * 109).

Output
For each case, print the case number and the result. You may assume that each output will fit into a 64 bit signed integer.

Sample Input
Output for Sample Input
3
2
100
200000000
Case 1: 0
Case 2: 3150
Case 3: 12898681201837053

解题思路:

通过一个因子,求出与此因子相对应的其他因子,求和;

例如n=20的时候,当因子为2时,对应的 2(4),3(6),4(8),5(10),6(12),7(14),8(16),9(18),10(10)  

当为3时,对应的为2(6),3(9),4(12),5(15),6(18)

此时要计算时要注意避免 2和3时之间有重复的情况。

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;

typedef long long ll;
int T;
ll sum,n,p,q,m;

int main(){
     
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        sum = 0;
        scanf("%lld",&n);
        
        m = (ll)sqrt(n);
        for(ll i=2;i<=m;i++){
            sum += i;
            // p,q 变量的增加是为了避免重复情况的产生 
            p = i+1;
            q = n/i;
            if(q<q) continue;
            sum += (q-p+1)*i;
            sum += (q-p+1)*(q+p)/2;
            
        }
        printf("Case %d: %lld
",t,sum);
        
        
    }
    
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/yuanshixingdan/p/5539756.html