Relatives+水题+欧拉函数+素数打表的基本应用+poj

来源:互联网 发布:卧龙进阶数据 编辑:程序博客网 时间:2024/05/17 08:27
Relatives
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11122 Accepted: 5383

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

7120

Sample Output

64

解决方案:水题,没什么好说的。

code:

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#define MMAX 100000using namespace std;vector<int >prime;bool vis[MMAX];void init_prime(){   memset(vis,false,sizeof(vis));   prime.clear();   prime.push_back(2);   for(int i=3;i<MMAX;i+=2){    if(!vis[i]){        prime.push_back(i);        for(int j=i+i;j<MMAX;j+=i) vis[j]=true;    }   }}int euler(int x){    long long res=x;    int len=prime.size();    for(int i=0;i<len;i++){        if(x%prime[i]==0){            res=res/prime[i]*(prime[i]-1);            while(x%prime[i]==0) x/=prime[i];        }    }    if(x>1) res=res/x*(x-1);    return res;}int main(){    long long n;    init_prime();    while(~scanf("%lld",&n)&&n){        printf("%d\n",euler(n));    }    return 0;}

0 0
原创粉丝点击