FZU 1012 Relatives

Relatives


Time Limit:1s Memory limit:32M
Accepted Submit:387 Total Submit:1304

Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.

There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.

For each test case there should be single line of output answering the question posed above.

Sample Input

7
12
0

Sample Output

6
4

解题:

       题目要1..n-1中与n互质的数的个数,运用欧拉函数求解。
假设要求x的欧拉函数值,则可以按照下面求解。
FZU 1012 Relatives_第1张图片

举例:φ(72)=φ(2^3×3^2)=(2-1)2^(3-1)×(3-1)3^(2-1)=24
代码实现的时候,遇到是素数p1的话,先乘以(p1-1),再计算是p1的几次幂,然后乘起来;继续计算下个素数。
欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。

#include using namespace std; int eular(int n) { int ret=1,i; for (i=2;i*i<=n;i++) if (n%i==0) { n/=i,ret*=i-1; while (n%i==0) n/=i,ret*=i; } if (n>1) ret*=n-1; return ret; } int main() { int n; while (cin>>n && n!=0) { cout<

你可能感兴趣的:(1.2,ACM/ICPC)