TOJ 1395.Relatives

来源:互联网 发布:香港域名注册商 编辑:程序博客网 时间:2024/04/30 03:40

题目链接 : http://acm.tju.edu.cn/toj/showp1395.html

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.
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.

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

Sample Input

7
12
0
Sample Output

6
4

知道欧拉函数的话很容易就做出来了,说白了就是求欧拉函数的值。直接上代码,不知道欧拉函数可以去看这篇:http://blog.csdn.net/muyujinxi/article/details/52164510

#include <stdio.h>using namespace std;int euler(int n){    int sum=n;    for(int i=2;i*i<=n;i++){        if(n%i==0){            sum=sum-sum/i;            while(n%i==0)                n/=i;        }    }    if(n>1)        sum=sum-sum/n;    return sum;}int main(){    int n;    while(scanf("%d",&n) && n)        printf("%d\n",euler(n));} 
0 0