bzoj2705: [SDOI2012]Longge的问题 欧拉定理
题意:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N)。
题解:考虑n的所有因子,假设有因子k,那么对答案的贡献gcd(i,n)k的个数即gcd(i/k,n/k)1的个数即n/k的欧拉函数,答案就是∑(k|n)k*φ(n/k)
枚举n的因子复杂度O(sqrt(n)),单次求欧拉函数复杂度O(sqrt(n)),复杂度O(n),但是实际跑起来比O(n)小很多
/**************************************************************
Problem: 2705
User: walfy
Language: C++
Result: Accepted
Time:56 ms
Memory:1288 kb
****************************************************************/
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
const double g=10.0,eps=1e-12;
const int N=100000+10,maxn=1000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
ll eu(ll n)
{
ll ans=n;
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
ans=ans/i*(i-1);
while(n%i==0)n/=i;
}
}
if(n!=1)ans=ans/n*(n-1);
return ans;
}
int main()
{
ll n;scanf("%lld",&n);
ll ans=0;
for(int i=1;i*i<=n;i++)
{
if(n%i==0)
{
ans+=i*eu(n/i);
if(i*i!=n)ans+=n/i*eu(i);
}
}
printf("%lld\n",ans);
return 0;
}
/***********************
***********************/