欧拉函数 模板题

1.题目引入:

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.

Input

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.

Output

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

2.样例输出: 

Sample Input

7
12
0

Sample Output

6
4

题目大意:求解欧拉函数,欧拉函数即:小于 n 且与 n 互质的数的数量 ,具体操作如下:

如对欧拉函数不了解的可看这里 https://blog.csdn.net/Qiuker_jl/article/details/109587507

3.代码如下: 

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
ull dfs_get(ull n)
{
    ull ans=n;         // 统计 n 以内的互质函数的数量  
    for(ull i=2;i*i<=n;i++)  
    // 如果存在一个数与 n不互质那么这个数的平方一定小于等于 n 
    {
        if(n%i==0) 
         /*如果存在质因子那么这个质因子的整数倍就一定不是欧拉函数
           反之就是我们要求的欧拉函数 
        */ 
        {
            //n-(n/p1)-...(n.p2)=n/p*(p-1)  
            ans=ans/i*(i-1);    
            while(n%i==0)  n/=i; 
            //12=2*2*3; 即将它整倍个数全部除掉与上面相互对应 
        }
    }
    if(n>1) ans=ans/n*(n-1);  //这一步一定别忘哈 
    return ans;
}
int main()
{
    ull n;
    ios::sync_with_stdio(0);  
    cin.tie(0); cout.tie(0); 
    while(cin>>n&&n)
    {
        cout<<dfs_get(n)<<endl;
    }
    return 0;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风遥~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值