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。

数论……

枚举所有n的因数的集合{k}。

令s[k]表示gcd(m,n)=k的m的个数。因为gcd(m,n)=k,显然有gcd(m/k,n/k)=1。于是s[k]=phi(n/k)

显然ans=Σk*s[k]

你问我会不会爆int?管他呢……全开long long根本不用担心

#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
LL n,m,ans;
inline LL phi(LL x)
{
	LL y=x;
	for(LL i=2;i<=m;i++)
	if(x%i==0)
	{
		y=y/i*(i-1);
		while (x%i==0) x/=i;
	}
	if (x>1) y=y/x*(x-1);
	return y;
}
int main()
{
	scanf("%lld",&n);
	m=sqrt(n);
	for (LL i=1;i<=m;i++)
	if(n%i==0)
	{
		ans+=(LL)i*phi(n/i);
		if (i*i<n) ans+=(LL)(n/i)*phi(i);
	}
	printf("%lld",ans);
}


相关文章:

  • 2022-12-23
  • 2021-12-02
  • 2021-12-16
  • 2022-02-26
猜你喜欢
  • 2021-08-20
  • 2021-12-29
  • 2021-06-28
  • 2021-09-18
  • 2021-09-09
  • 2021-07-02
相关资源
相似解决方案