大体思路见过,但是已经模糊了,复习一下
要求sigma(gcd(n,i)),1<=i<=n
枚举因子x,gcd(i,n)==x,要知道ii的个数
即是gcd(i/x,n/x)==1的个数,这可以用phi(n/x)求出有多少个i/x
#include<bits/stdc++.h> #include<stdio.h> #include<algorithm> #include<queue> #include<string.h> #include<iostream> #include<math.h> #include<set> #include<map> #include<vector> #include<iomanip> using namespace std; #define ll long long #define ull unsigned long long #define pb push_back #define FOR(a) for(int i=1;i<=a;i++) const int inf=0x3f3f3f3f; const ll Linf=9e18; const int maxn=1e5+7; const ll mod=100003; const double eps=1e-6; ll n,ans; int m; ll eular(ll n){ ll ans=n; for(int i=2;i<=m;i++){ if(n%i==0){ ans-=ans/i; while(n%i==0)n/=i; } } if(n>1)ans-=ans/n; return ans; } int main(){ scanf("%lld",&n); m=sqrt(n); for(int i=1;i<=m;i++){ if(n%i==0){ if(n/i==i){ ans+=i * eular(n/i); }else{ ans+=i * eular(n/i) + n/i * eular(i); } } } printf("%lld ",ans); }