1040 最大公约数之和
题目来源: rihkddd
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注
给出一个n,求1-n这n个数,同n的最大公约数的和。比如:n = 6
1,2,3,4,5,6 同6的最大公约数分别为1,2,3,2,1,6,加在一起 = 15
Input
1个数N(N <= 10^9)
Output
公约数之和
Input示例
6
Output示例
15
设整数d,满足n%d=0;
gcd(n,k)=d —> nx + ky = d —-> n/d * x + k/d *y = 1
显然 对任意k<=n,满足gcd(n/d,k/d)=1的k的个数 就是gcd(n,k)=d的k个数
而这个k的个数就等于phi(n/d) (n的欧拉函数)
所以枚举d,将d*phi(n/d)累加就是最终答案
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
void caulate_factor(int n,vector<int>&factor){//分解n的质因子
factor.clear();
for(int i=2,end=sqrt(n+1);i<=end;++i){
if(n%i==0){
factor.push_back(i);
while(n%i==0){
n/=i;
}
}
if(i>=n)
break;
}
if(n!=1)
factor.push_back(n);
}
ll phi(ll n,vector<int>&factor){
ll ans=n;
for(int i=0;i<factor.size();++i){
ans=ans*(factor[i]-1)/factor[i];
}
return ans;
}
ll slove(int n){
ll ans=0;
vector<int>factor_fac;
for(int i=1,end=sqrt(n);i<=end;++i){//枚举d
if(n%i==0){
int factor=i;
caulate_factor(n/factor,factor_fac);
ans+=factor*phi(n/factor,factor_fac);
if(n/i>end){
factor=n/i;
caulate_factor(n/factor,factor_fac);
ans+=factor*phi(n/factor,factor_fac);
}
}
}
return ans;
}
int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
int n;
while(~scanf("%d",&n)){
printf("%lld\n",slove(n));
}
return 0;
}