BZOJ2705 [SDOI2012]Longge的问题 【欧拉函数】

来源:互联网 发布:深入浅出数据分析 pdf 编辑:程序博客网 时间:2024/06/05 18:32

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2705

题解:
首先感谢SDOI出题组出了POJ2480的原题= =b
—-参考hzwer的题解—–
设函数s(k) 为 gcd(m, n)=k中的m的个数,那么gcd(m/k, n/k)一定为1,即互质,我们可以使用欧拉函数来解决。

代码:

// by DenyTianly#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;const int inf = 1 << 26;typedef long long LL;LL n;LL phi(LL p) {    // To do something here    LL ans = p;    for ( LL i = 2; i*i <= n; i ++ ) {        if(p%i == 0) {            ans = ans/i*(i-1);            while(p%i == 0) p /= i;        }    }    if(p > 1) ans = ans/p*(p-1);    return ans;}int main(){    while( scanf("%lld", &n) != EOF ){        LL ans = 0;        for ( LL k = 1; k*k <= n; k ++ ) {            if(n%k == 0) {                ans += (LL)k*phi(n/k);                if(k*k != n) ans += (LL)(n/k)*phi(k);            }        }        printf("%lld\n", ans);    }    return 0;}
0 0
原创粉丝点击