![](http://static.blog.csdn.net/images/category_icon.jpg)
版权声明:本文为博主原创文章,未经博主允许不得转载。
欧拉函数的定义可以自己百度 phi(n)=n*(1-1/p1)*(1-1/p2)*...(1-1/pn)
主要是涉及到求很多个欧拉函数时的求法,这种题一般都是先初始化,
其中,在求素因子的时候处理的很巧妙,当phi[i]==i时,i就是素因子(可以拿起笔在草稿上画一下就明白了),这个时候j+=i, 则j有素因子i,认真思考!
之前做过一道题,是求一个数的所以因子之和,也是类似的做法:
Problem Description
The hardest problem may not be the real hardest problem.
Given a natural number n (1 <= n <= 500000), please output the summation of all its proper divisors. Definition: A proper divisor of a natural number is the divisor that is strictly less than the number. e.g. number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.
Given a natural number n (1 <= n <= 500000), please output the summation of all its proper divisors. Definition: A proper divisor of a natural number is the divisor that is strictly less than the number. e.g. number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.
Input
An integer stating the number of test cases (equal to about 200000), and that many lines follow, each containing one integer between 1 and 500000 inclusive.
Output
One integer each line: the divisor summation of the integer given respectively.
Sample Input
3 2 10 20
Sample Output
1 8 22
关键代码如下:
欧拉定理早就看到了防止忘记先码上
这个题首先我们要会欧拉定理
a b % p = a (b % phi(p)) % p
但是这个有一个前提,a和p必须互质,如果不互质的话是不对的。
但是有一个大神告诉我,不互质的情况下也有一个类似的定理。。
如果a和p不互质,且b大于等于phi(p)
a b % p = a (b % phi(p) + phi(p)) % p
hdu2824本题的代码如下: