POJ 2480 Longge's problem(欧拉函数,积性函数)
来源:互联网 发布:超级数据恢复注册机 编辑:程序博客网 时间:2024/05/17 06:54
题目: LINK
题意: 给定n,∑gcd(i,n),1<=i<=n 。
如果求 满足 gcd(i, n) == x的和,先要求出有多少个这样的i满足条件,结果是phi(n/x) 个,因为gcd(i, n) == x, 则i/x 和n/x应该互质,n/x的欧拉函数的值就是i/x的个数,也就是i的个数。
比较暴力的办法就是,枚举n的因子x ,x*phi(n/x)就是满足gcd(i,n) == x的和,对于每个因子的结果求和就可以.
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<cmath>#include<queue>#include<map>#include<set>using namespace std;#define INF 1000000000typedef __int64 LL;#define N 50001LL n;LL phi(LL n){LL ret=1;for(LL i=2;i*i<=n;i++) {if(n%i==0) {n/=i;ret*=(i-1); // p^k - p^(k-1)while(n%i==0) {n/=i; ret*=i;}}}if(n>1) ret*=n-1;return ret;}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE while(scanf("%I64d", &n) != EOF) { LL ans = 0; LL i ; for(i = 1; i*i < n; i++ ) { if(n % i == 0) { ans += i * phi(n/i); ans += (n/i) *phi(n/(n/i)); } } if(i * i == n) ans += i*phi(n/i); printf("%I64d\n", ans); }return 0;}
还有就是利用积性函数.
对于欧拉函数,如果p是质数 phi(p^k) = p^k - p^(k-1) .
gcd是积性函数, gcd(x*y, n) = gcd(x, n) * gcd(y, n), (gcd(x, y) == 1) .
积性函数的和也是积性函数, 所以F(N) = ∑gcd(i,n) 是积性函数
如果p是质数
F(p^r) = p^0*phi(p^(r-0)) + p^1*phi(p^(r-1)) + .... + p^(r-1)*phi(p^1) + p^r*phi(p^0) //利用上面所说的欧拉函数的性质
= r * (p^r - p^(r-1)) + p^r;
最终要求的结果 F(N) = F(p1^k1*p2^k2……pi^ki) = II (ki * (pi^ki - pi^(ki-1)) + pi^ki).
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<cmath>#include<queue>#include<map>#include<set>using namespace std;#define INF 1000000000typedef __int64 LL;LL n;int main(){#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE while(scanf("%I64d", &n) != EOF) { LL ans = 1; for(LL i = 2; i*i <= n; i++) { if(n%i) continue; LL cou = 0, p = 1; while(n%i == 0) { cou ++ ; n /= i; p *= i; } ans *=(cou * ( p - p / i) + p); } if(n != 1) ans *= (n - 1 + n); printf("%I64d\n", ans); }return 0;}
0 0
- POJ 2480 Longge's problem 解题报告(欧拉函数 + 积性函数)
- POJ 2480 Longge's problem(积性函数之欧拉函数与gcd)
- poj 2480 Longge's problem(欧拉函数或积性函数)
- POJ 2480 Longge's problem 欧拉函数的应用 积性函数
- POJ 2480 Longge's problem(欧拉函数,积性函数)
- poj 2480 Longge's problem 积性函数性质+欧拉函数
- poj 2480 Longge's problem(积性函数 & 欧拉函数)
- POJ 2480 Longge's problem 欧拉函数
- POJ 2480 Longge's problem(神奇欧拉函数)
- POJ 2480 Longge's problem (欧拉函数)
- POJ 2480 Longge's problem (欧拉函数)
- (poj 2480 Longge's problem)<欧拉函数>
- 【poj】 2480 Longge's problem (欧拉函数)
- poj 2480 Longge's problem 关于欧拉函数和积形函数推导
- POJ 2480 Longge's problem (欧拉函数+乘性函数)
- POJ 2048 Longge's problem (欧拉函数 积性函数)
- POJ 2480 Longge's problem 积性函数
- POJ-2480(欧拉函数的运用)(Longge's problem )
- 001-练习02:入门学习Linux常用必会60个命令实例详解
- DiskLruCache
- 金庸武侠小说之我的见解
- XP 与 敏捷开发的关系
- 我的算法学习之路
- POJ 2480 Longge's problem(欧拉函数,积性函数)
- 全面认识JUnit4.0
- HEVC学习与研究
- linux环境应用程序性能测试——google preftools
- 实例学习gcc+gdb+make
- Executing Selenium TestNG tests using ANT
- 一次关于游戏配置的实践心得
- 单例设计模式
- 改变世界的17个等式