POJ 2407 Relatives(欧拉函数)

题目链接

题意 : 求小于等于n中与n互质的数的个数。

思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板。

这里是欧拉函数的简介

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstdlib>

 4 #include <cstring>

 5 #include <cmath>

 6 

 7 using namespace std;

 8 

 9 int main()

10 {

11     int x;

12     while (scanf("%d", &x) && x )

13     {

14         int  res = x;

15         for (int i =2; i < (int) sqrt(x *1.0) +1; i++)

16         {

17             if (x % i ==0)

18             {

19                 res = res / i * (i -1);

20                 while (x % i ==0)

21                     x /= i; // 保证i一定是素数

22             }

23         }

24         if (x > 1)

25             res = res / x * (x -1);

26         printf("%d\n", res);

27     }

28     return 0;

29 }
View Code

 

 

你可能感兴趣的:(relative)