lightoj-1098 - A New Function(因子和)

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\n",t,sum);
        
        
    }
    
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/yuanshixingdan/p/5539756.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值