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 0Sample 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; }