bzoj 2705 [SDOI2012]Longge的问题——欧拉函数大水题

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705

撕逼题。不就是枚举gcd==d,求和phi[ n/d ]么。

然后预处理sqrt (n)的阶乘,RE得不行。发现用到了大于sqrt (n)的阶乘。

然后翻看TJ。

发现phi可以现求!就用那个式子。我竟然都忘了!

注意最后剩下的一个大于sqrt (i)的质因数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const int N=1e5+5;
ll ans,n;
ll phi(ll a)
{
    ll ret=a;
    for(ll i=2;i*i<=a;i++)
        if(a%i==0)
        {
            ret=ret/i*(i-1);
            while(a%i==0)a/=i;
        }
    if(a>1)ret=ret/a*(a-1);
    return ret;
}
int main()
{
    scanf("%lld",&n);
    for(ll i=1;i*i<=n;i++)
        if(n%i==0)
            if(i!=n/i)ans+=i*phi(n/i)+(n/i)*phi(i);
            else ans+=i*phi(n/i);
    printf("%lld\n",ans);
    return 0;
}

 

posted on 2018-08-01 10:24  Narh  阅读(154)  评论(0编辑  收藏  举报

导航