欧拉函数又称为Phi函数
欧拉函数的定义为:对于正整数n,他的欧拉函数值是不大于n的正整数中与n互质的正整数的个数(互质:除1外没有其他最大公约数)。
据此,可以得到求某个数欧拉值的代码:
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
int Phi(int x)
{
int ans = 0;
for (int i = 1; i <= x; i++)
{
if (gcd(i, x) == 1)
ans++;
}
return ans;
}
这种方法时间复杂度为O(nlogn)
p1,p2…pn是n的质因数
比如8,8只有一个质因数2,4是其因数但不是质因数12的质因数为2和3,2的倍数都不是其质因数,有1/2是2的倍数,1/2不是2的倍数;3的倍数也不是其质因数,3的倍数不是其质因数,有1/3,所以2/3是其质因数所以12的质因数有121/22/3 = 4;
1.原理:利用欧拉函数公式 1与任何整数都互质。
#include
using namespace std;
int Phi(int n){
int ans = n;
for(int i = 2; i * i <= n; i++){
if(n % i == 0){
ans = ans /i*(i - 1);
}
while(n % i == 0){
n = n /i;
}
}
if(n > 1) ans = ans /n*(n -1);
return ans;
}
int main(){
int n;
cin >> n;
cout << Phi(n);
}
有时候我们不仅仅求某一个数的欧拉函数值,而是要求欧拉函数根据欧拉函数值的定义以及欧拉函数使用筛法求出欧拉函数表
**
**
原理:挨氏筛求欧拉函数表,利用欧拉函数公式,时间复杂度为O(nlogn)只需要开一个phi数组phi数组具有两个最用,标记是不是素数,同时在程序运行过程中求出欧拉函数值
#include
using namespace std;
const int N = 1e6 + 10;
int phi[N]; //phi[i] 为0时是素数
void GetPhi(int maxn){
memset(phi,0,sizeof(phi));
phi[1] = 1;
for(int i = 2; i < maxn; i++){
if(!phi[i]){
for(int j = i; j < maxn; j = j + i){ //把i的倍数全处理了
if(!phi[j]) phi[j] = j;
phi[j] = phi[j]/i * (i - 1);
}
}
}
return;
}
int main(){
int x;
cin >> x;
GetPhi(100);
cout << phi[x];
}
线性筛求欧拉函数表的原理是利用欧拉函数递推式,时间复杂度为O(n)
#include
using namespace std;
const int N = 1e6 + 10;
bool isprime[N];
int primes[N],pn;
int phi[N];
void FastPhi(int maxn){
memset(isprime,true,sizeof(isprime));
isprime[0] = isprime[1] = false;
phi[1] = 1;
pn = 0;
for(int i = 2; i < maxn; i ++){
if(isprime[i]){
primes[pn++] = i;
phi[i] = i - 1;
}
for(int j = 0; j < pn && i * primes[j] < maxn; j++){
isprime[i * primes[j]] = false;
if(i % primes[j] == 0){
phi[i * primes[j]] = phi[i] * primes[j];break;
}
phi[i * primes[j]] = phi[i] * (primes[j] - 1);
}
}
return;
}
int main(){
}
例题
Farey Sequence (POJ 2478) Description
The Farey Sequence Fn for any integer n with n >= 2 is the set of
irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1
arranged in increasing order. The first few are F2 = {1/2} F3 = {1/3,
1/2, 2/3} F4 = {1/4, 1/3, 1/2, 2/3, 3/4} F5 = {1/5, 1/4, 1/3, 2/5,
1/2, 3/5, 2/3, 3/4, 4/5}You task is to calculate the number of terms in the Farey sequence Fn.
Input There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 106). There are
no blank lines between cases. A line with a single 0 terminates the
input.
Output For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn.
Sample Input 2 3 4 5 0 Sample Output 1 3 5 9
思路
求解小于N的互质组数,相当于求欧拉函数值的和在(2<=i<=n),直接用打表法算出欧拉函数表。
#include
#include
#include
#include
#include
#include
using namespace std;
const int N = 1e6 + 100;
int phi[N]; //phi[i] 为0时是素数
void getPhi(int maxn){
memset(phi,0,sizeof(phi));
phi[1] = 1;
for(int i = 2; i < maxn; ++i){
if(!phi[i]) //满足该条件为素数
{
for(int j = i ; j < maxn; j = j + i){
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] /i * (i - 1);
}
}
}
return;
}
int main(){
getPhi(N);
int n;
while(cin >> n){
if(n == 0) break;
long long sum = 0;
for(int i = 2; i <=n; i++){
sum += phi[i];
}
cout << sum <<endl;
}
}
注意sum的取值范围,用int的话可能导致结果不正确,需要使用int,或者是—__int64 sum = 0;在提交POJ时不能使用万能头文件