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;
    }
    

      

  • 相关阅读:
    修复 Visual Studio Error “No exports were found that match the constraint”
    RabbitMQ Config
    Entity Framework Extended Library
    Navisworks API 简单二次开发 (自定义工具条)
    NavisWorks Api 简单使用与Gantt
    SQL SERVER 竖表变成横表
    SQL SERVER 多数据导入
    Devexpress GridControl.Export
    mongo DB for C#
    Devexress XPO xpPageSelector 使用
  • 原文地址:https://www.cnblogs.com/dybala21/p/9747397.html
Copyright © 2011-2022 走看看