BZOJ 2705 [SDOI2012]Longge的问题 ——Dirichlet积
【题目分析】
狄利克雷卷积。
然后直接求出欧拉函数,计算和即可。
【代码】
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map> #include <set> #include <queue> #include <string> #include <iostream> #include <algorithm> using namespace std; #define maxn 500005 #define ll long long #define inf 0x3f3f3f3f #define F(i,j,k) for (int i=j;i<=k;++i) #define D(i,j,k) for (int i=j;i>=k;--i) void Finout() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endif } int Getint() { int x=0,f=1; char ch=getchar(); while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();} while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f; } int n; int phi(int x) { // cout<<"phi"<<x; int ret=x; F(i,2,sqrt(x)) { if (x%i==0) ret/=i,ret*=(i-1); while (x%i==0) x/=i; } if (x>1) ret=ret/x*(x-1); // cout<<" is "<<ret<<endl; return ret; } ll ans=0; int main() { Finout(); n=Getint(); F(i,1,sqrt(n)) if (n%i==0) { ans+=(ll)phi(i)*(n/i); if (i*i<n) ans+=(ll)phi(n/i)*i; } cout<<ans<<endl; }