zoukankan      html  css  js  c++  java
  • 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 1655】Balancing Act
    【POJ 2631】 Roads in the North
    【洛谷 1002】 过河卒
    【洛谷 3178】树上操作
    【洛谷 1060】开心的金明
    【洛谷 2709】小B的询问
    【洛谷 1972】HH的项链
    JavaSpark-sparkSQL
    java8下spark-streaming结合kafka编程(spark 2.3 kafka 0.10)
    Kafka 使用Java实现数据的生产和消费demo
  • 原文地址:https://www.cnblogs.com/dybala21/p/9747397.html
Copyright © 2011-2022 走看看