POJ 2407 Relatives(欧拉函数)

时间:2023-03-09 19:01:32
POJ 2407 Relatives(欧拉函数)

http://poj.org/problem?id=2407

题意:

给出一个n,求小于等于的n的数中与n互质的数有几个。

思路:

欧拉函数的作用就是用来求这个的。

POJ 2407 Relatives(欧拉函数)

POJ 2407 Relatives(欧拉函数)

 #include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
using namespace std; int n; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (cin >> n && n)
{
double ans = ;
int x = n;
if (n == )
cout << "" << endl;
else
{
for (int i = ; i <= n; i++)
{
if (n%i == )
{
x = x - x / i;
while (n%i == )
n /= i;
}
}
if (n > )
x = x - x / n;
}
cout << x << endl;
}
}