E - A New Function LightOJ - 1098

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

3

2

100

200000000

Sample Output

Case 1: 0

Case 2: 3150

Case 3: 12898681201837053

题解:根号n循环,求得前半部分得同时求出后半部分。

后半部分就是从sqrt(n)+1到n/i的累加和,这里给出的是化简之后的公式,自己可自己推导一下

(从p到q的累加和:q*(q+1)/2-(p-1)*p/2)

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<stdio.h>
using namespace std;
typedef long long ll;
int main()
{
    int T;
    int k=1;
    cin>>T;
    while(T--){
        ll n;
        cin>>n;
        int x=sqrt(n);
        int le=x+1;
        ll sum=0;
        for(int i=2;i<=x;i++){
            sum+=(n/i-1)*i;//前半部分
            int re=n/i;
            sum+=(le+re)*(re-le+1)/2;//后半部分,从le到re的加和,运用求和公式推导即得
        }
        cout<<"Case "<<k++<<": "<<sum<<endl;
    }  
    return 0;
}

2:求[a,b]区间所有数的因子和:

int main()
{
    ll a,b;
    cin>>a>>b;
    ll sum=0;
    for(int i=1;i<=sqrt(b);i++){
        ll l=a/i;
        ll r=b/i;
        if(a%i)l++;
        sum+=i*(r-l+1);//分解式左边部分
        if(i!=sqrt(b)){ 
            ll lx=max(l,(ll)sqrt(b)+1);
            ll rx=r;
            if(rx>=lx){
                sum+=(rx+lx)*(rx-lx+1)/2;//分解式右边部分(区间和)
            }
        }
    }
    cout<<sum<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值