您的位置:首页 > 其它

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

2014-07-26 18:18 316 查看
Relatives

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 11122Accepted: 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
7
12
0

Sample Output
6
4

解决方案:水题,没什么好说的。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define MMAX 100000
using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐