您的位置:首页 > 其它

根据要求求除数的数 与 互素和算法 (的品质因数和欧拉函数分解)

2015-12-09 16:23 375 查看

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

0
2
1
3


题意:给出一个数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互质的和的模板。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: