poj2407(欧拉函数-套模板)

来源:互联网 发布:剑三萝莉脸型数据 2016 编辑:程序博客网 时间:2024/05/17 03:13

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

Relatives
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 9323Accepted: 4398

Description

Given n, a positiveinteger, how many positive integers less than n are relativelyprime to n? Two integers a and b are relatively prime if there areno integers x > 1, y > 0, z > 0 such that a = xy and b =xz.

Input

There are severaltest cases. For each test case, standard input contains a line withn <= 1,000,000,000. A line containing 0 follows the lastcase.

Output

For each test casethere should be single line of output answering the question posedabove.

Sample Input

7120

Sample Output

64

Source

Waterloo local 2002.07.01
题意:就是求小于n且与n互质的数的个数
明显的欧拉函数直接套的模版。。(ps:自己专用模版)
#include
#include
#include
using namespace std;
int n;
int gcd(int a,int b)
{
    returnb?gcd(b,a%b):a;
}
inline int lcm(int a,int b)
{
    returna/gcd(a,b)*b;
}
int eular(int n)
{
    intret=1,i;
   for(i=2;i*i<=n;i++)
    {
       if(n%i==0)
       {
           n/=i;
           ret*=i-1;
           while(n%i==0)
           {
               n/=i;
               ret*=i;
           }
       }
    }
   if(n>1)
    {
       ret*=n-1;
    }
    returnret;
}
int main()
{
   while(scanf("%d",&n)!=EOF)
    {
       if(n==0)
       {
           break;
       }
       printf("%d\n",eular(n));
    }
    return0;
}
原创粉丝点击