码迷,mamicode.com
首页 > 其他好文 > 详细

POJ_2407 Relatives 【欧拉函数裸题】

时间:2018-10-06 16:45:27      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:answer   poj   mem   题目   integer   answering   style   case   last   

一、题目

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 【欧拉函数裸题】

标签:answer   poj   mem   题目   integer   answering   style   case   last   

原文地址:https://www.cnblogs.com/dybala21/p/9747397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!