1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #define ll long long 5 ll n,m,ans; 6 ll phi(ll a1) 7 { 8 ll sum=a1,m=sqrt(a1); 9 for(ll i=2;i<=m;i++) 10 if(a1%i==0&&a1) 11 { 12 sum=sum/i*(i-1); 13 for(;a1%i==0;a1/=i); 14 } 15 if(a1>1) 16 sum=sum/a1*(a1-1); 17 return sum; 18 } 19 int main() 20 { 21 scanf("%lld",&n); 22 m=sqrt(n); 23 for(ll i=1;i<=m;i++) 24 if(n%i==0) 25 { 26 ans+=i*phi(n/i); 27 if(n/i!=i) 28 ans+=n/i*phi(i); 29 } 30 printf("%lld\n",ans); 31 return 0; 32 }
题目中要求出∑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)
phi可以在根号的时间内求出