BZOJ-1705 Longge的问题 一维GCD SUM 乱搞+质因数分解+...

2705: [SDOI2012]Longge的问题
Time Limit: 3 Sec Memory Limit: 128 MB
Submit: 1871 Solved: 1172
[Submit][Status][Discuss]

Description
Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题。现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N)。

Input
一个整数,为N。

Output
一个整数,为所求的答案。

Sample Input
6

Sample Output
15

HINT
【数据范围】
对于60%的数据,0 < N<=2^16。
对于100%的数据,0 < N<=2^32。

Source
round1 day1

这里写图片描述

code:

#include <cstdio> 
#include <iostream> 
using namespace std; 
int main()
{ 
    long long n,p,a,ans; 
    while(~scanf("%lld",&n))
    { 
        ans=n; 
        for (long long i=2;i*i<=n;i++)
            { 
                if (n%i==0) 
                    { 
                        p=i;a=0; 
                        while (n%p==0) a++,n/=p; 
                        ans+=ans*a*(p-1)/p; 
                    } 
            } 
        if (n!=1) ans=ans*(n*2-1)/n;  
        printf("%lld
",ans); 
    } 
    return 0; 
}
原文地址:https://www.cnblogs.com/DaD3zZ-Beyonder/p/5346197.html