题意:
给定一个整数N,求出∑gcd(i, N)(1<=i <=N)。
题解:
欧拉函数就是求比一个正整数且和它互质的正整数有几个,我不会,摘黄学长的题解:
“
题目中要求出∑gcd(i,N)(1<=i<=N)。
枚举n的约数k,令s(k)为满足gcd(m,n)=k,(1<=m<=n)m的个数,则ans=sigma(k*s(k)) (k为n的约数)
因为gcd(m,n)=k,所以gcd(m/k,n/k)=1,于是s(k)=euler(n/k)
”
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #define inc(i,j,k) for(int i=j;i<=k;i++) 6 using namespace std; 7 8 long long phi(long long x){ 9 long long a1=x,a2=(long long)sqrt(x); 10 inc(i,2,a2)if(x%i==0){ 11 a1=a1/i*(i-1); while(x%i==0)x/=i; 12 } 13 if(x!=1)a1=a1/x*(x-1); return a1; 14 } 15 int main(){ 16 long long n; scanf("%lld",&n); long long m=(long long)sqrt(n); long long ans=0; 17 inc(i,1,m)if(n%i==0){ 18 ans+=i*phi(n/i); if(i*i<=n)ans+=n/i*phi(i); 19 } 20 printf("%lld",ans); 21 }
20160408