POJ_2407 Relatives 欧拉函数裸题
Posted dybala21
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ_2407 Relatives 欧拉函数裸题相关的知识,希望对你有一定的参考价值。
一、题目
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 <iostream> #include <cmath> #include <cstring> using namespace std; const int MAXN = 1e5+3; int Prime[MAXN], nPrime; bool isPrime[MAXN]; void getPrime() { memset(isPrime, 1, sizeof(isPrime)); isPrime[0] = isPrime[1] = 0; nPrime = 0; for(int i = 2; i < MAXN; i++) { if(isPrime[i]) Prime[nPrime++] = i; for(int j = 0; j < nPrime && (long long)Prime[j]*i < MAXN; j++) { isPrime[Prime[j]*i] = 0; if(i%Prime[j] == 0) break; } } } int Euler(int n) { int ans = n; for(int i = 0; Prime[i]*Prime[i] <= n; i++) { if(n%Prime[i] == 0) { ans = ans - ans/Prime[i]; do { n/=Prime[i]; } while(n%Prime[i] == 0); } } if(n > 1) ans = ans - ans/n; return ans; } int main() { int N; getPrime(); while(cin >> N && N) { cout << Euler(N) << endl; } return 0; }
以上是关于POJ_2407 Relatives 欧拉函数裸题的主要内容,如果未能解决你的问题,请参考以下文章