POJ 2407 Relatives(欧拉函数)

本文介绍了一种使用欧拉函数求解小于等于给定整数n并与n互质的数的数量的方法。通过分解质因数并利用欧拉函数的性质,文章提供了一个高效的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:

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

 

思路:

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

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 
 8 int n;
 9 
10 
11 int main()
12 {
13     //freopen("D:\\txt.txt", "r", stdin);
14     while (cin >> n && n)
15     {
16         double ans = 1;
17         int x = n;
18         if (n == 1)
19             cout << "0" << endl;
20         else
21         {
22             for (int i = 2; i <= n; i++)
23             {
24                 if (n%i == 0)
25                 {
26                     x = x - x / i;
27                     while (n%i == 0)
28                         n /= i;
29                 }
30             }
31             if (n > 1)
32                 x = x - x / n;
33         }    
34         cout << x << endl;
35     }
36 }

 

转载于:https://www.cnblogs.com/zyb993963526/p/6575859.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值