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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章