POJ 2407 Relatives (欧拉函数)

Relatives
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12847   Accepted: 6337

Description

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.

Sample Input

7
12
0

Sample Output

6

4


题意:小于n且与n互素的数的个数


思路:欧拉函数模板


ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 100010
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
ll eular(ll n)  
{    
    ll i,res=n;    
    for(i=2;i*i<=n;i++)    
        if(n%i==0)  
        {    
            res=res/i*(i-1);    
            while(n%i==0)  
            n/=i;    
        }    
    if(n>1)   
    res=res/n*(n-1);    
    return res;    
}  
int main()
{
	ll n; 
	while(scanf("%I64d",&n)!=EOF,n)
	{
		printf("%d\n",eular(n));
	}
	return 0;
}


你可能感兴趣的:(POJ 2407 Relatives (欧拉函数))