求数论求约数和 与 互质和算法 (分解质因数与欧拉函数)
来源:互联网 发布:数据库王珊第五版pdf 编辑:程序博客网 时间:2024/05/21 19:21
Description
One day, Qz met an easy problem. But after a 5-hout-long contest in CSU, he became very tired and he wanted to call his girlfriend as soon as possible. As we all know, Qz is a genius in plenty of fields especially in programming. But he did not want to write a program to solve this problem, he just want to call his girlfriend. Now he gave this problem to you, and he want you to solve this problem without programming. Fortunately, YH heard about Qz’s bad behavior, and he thought that not everyone is as clever as Qz. Finally, YH managed to persuade Qz to ask you to write a program to solve this problem. The problem is:
Qz will give you only one number N (1<= N <= 10^9), and he wants to know the answer of A sub B, A and B are as follow:
A is sum of series of numbers X (He only cares about X which is no larger than N) .For each X, that exists an integer k satisfies that k * X = N.
B is sum of series of numbers Y (He only cares about Y which is no larger than N).For each Y satisfies that GCD(Y, N) = 1.
YH whispers to you that GCD(X, Y) means the Greatest Common Divisor of X and Y, and you should not let Qz know that YH has told you the secret.
Qz is so hurry so he had no time to give you the N one by one and he will give you multiple cases.
YH is so kind and he ask Qz that for each input file, the number of N is about 30000.
Input
Qz is so hurry to call his girlfriend so he has no time to explain the Input.
Output
Help Qz to output the answer to problems on each line.
Sample Input
1234
Sample Output
0213
题意:给出一个数n , 求n的约数和减去不大于n且与n互质的数的和,记为a-b。
当时去现场做想到的只有暴力,但10^9超时无疑。后来听出题人的讲解,说用到欧拉函数,当时也不怎么明白。直到今天看到一个性质:上述的b有一个公式,b=(n*f(n))/2。
f(n)代表n的欧拉函数。(有关证明在数论书中可以找到)。
问题转化为求n的约数和,n的欧拉函数。都需要用到分解质因数,将n分解为n=p1^a1*p2^a2*...*pn^an。n的约数和记为S(n),S(n)的公式不再赘述。
大致思路已经有了,先构造根号n的素数表,因为质因数不会超过根号n,然后就是分解质因数的过程。
该题目可以作为求约束和与不大于n且与n互质的和的模板。
#include<stdio.h>#include<math.h>typedef unsigned long long LL;#define maxn 200000LL primet[maxn];int pnum = 0;LL n,a,b,res;bool flag[maxn];void getprimtable()//筛法求出素数{ for (long i = 2; i < maxn; i++) if (!flag[i]) { primet[pnum++] = i; for (long j = i + i; j < maxn; j += i) flag[j] = true; }}LL FF(LL x ,LL y) { LL ret=1; while(y--) ret*=x; return ret;}LL eularpk(LL p, int k)//欧拉求p^k{ if (k == 0) return 1; LL ans = p - 1; while (--k) ans *= p; return ans;}LL f(LL n)//求f 因式分解{ LL sum = 1; res=1; int p, k; for (int i = 0; primet[i]*primet[i]<= n; i++) { p = primet[i]; if (n % p == 0) { k = 0; while (n % p == 0) {n /= p; k++;} sum = sum * eularpk(p, k); // p^k res*=( ( FF(p,k+1) -1 ) / (p-1)); } } if (n > 1) { // 分解到最后剩下一个素因子 , 须特别注意。 sum = sum * eularpk(n, 1); res*=( ( FF(n,2) -1 ) / (n-1)); } return sum;}int main() { getprimtable(); while(~scanf("%lld",&n)) { LL b=n*f(n)/2; if(n == 1) printf("0\n"); else printf("%lld\n",res-b); } return 0;}
0 0
- 求数论求约数和 与 互质和算法 (分解质因数与欧拉函数)
- 筛法求素数+分解质因子+欧拉函数+求约数
- HHU暑期第一弹——小小小数论(欧拉函数+埃式筛法+分解质因数+欧几里得算法+扩展欧几里得算法和模线性方程)
- 【初等数论】 求一个数有多少约数及所有约数之和、分解质因数
- Sumdiv(数论综合模板题:快速分解因式+快速幂取模+约数和公式+递归二分求等比数列和)
- 分解质因数和求因数个数
- 数据结构与算法(1)——分解质因数及各种方法求最大公约数
- 约数个数与约数和(质因素分解)
- 冒泡排序算法 递归算法,求n的阶乘 求最大公约数和最小公倍数 java分解质因数
- 数论 约数定理 约数和定理 欧拉定理
- 分解质因数 欧拉函数
- 欧拉函数的延伸:关于求小于等于n且与n互质的数的和。
- 数论之 素因子分解,素数筛选法,欧拉函数和扩展欧几里得算法 (整理)
- 最简单的问题与算法(求最大约数)
- 求1到n中与n互质的和(数论)
- 分别用连续整数检测、欧几里得和分解质因数算法求最大公约数
- poj 1845 Sumdiv(数论:欧拉函数+二分求等比数列前n项和+快速幂取模)
- 基础数论算法(六) 素数的筛法与质因数分解
- AutoLayout NSLayoutConstraint VFL
- 我与地坛
- 【iOS录音与播放】实现利用音频队列,通过缓存进行对声音的采集与播放
- 查看linux 操作系统信息的相关命令
- 40.天气渐热
- 求数论求约数和 与 互质和算法 (分解质因数与欧拉函数)
- 内存管理 - 指针数组字符串
- php zip文件内容比较类
- CSS 选择器(选择符)
- SqlMapClient对象
- 黑马程序员_第一个OC程序
- 黑马程序员--多态
- linux 安装.deb文件
- 通过 文氏图 Venn diagrams解释了SQL的Join