程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ1284---Primitive Roots(求原根個數, 歐拉函數)

POJ1284---Primitive Roots(求原根個數, 歐拉函數)

編輯:C++入門知識

POJ1284---Primitive Roots(求原根個數, 歐拉函數)


Description
We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (xi mod p) | 1 <= i <= p-1 } is equal to { 1, …, p-1 }. For example, the consecutive powers of 3 modulo 7 are 3, 2, 6, 4, 5, 1, and thus 3 is a primitive root modulo 7.
Write a program which given any odd prime 3 <= p < 65536 outputs the number of primitive roots modulo p.

Input
Each line of the input contains an odd prime numbers p. Input is terminated by the end-of-file seperator.

Output
For each p, print a single number that gives the number of primitive roots in a single line.

Sample Input

23
31
79

Sample Output

10
8
24

Source

求模素數p的原根個數
關於原根請看這裡:
原根

/*************************************************************************
    > File Name: POJ1284.cpp
    > Author: ALex
    > Mail: www.Bkjia.com
    > Created Time: 2015年06月04日 星期四 16時04分32秒
 ************************************************************************/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair  PLL;


int phi[100000];
int minDiv[100000];

void geteuler() {
    for (int i = 1; i <= 70000; ++i) {
        minDiv[i] = i;
    }
    for (int i = 2; i <= 70000; ++i) {
        if (minDiv[i] == i) {
            if (70000 / i < i) {
                break;
            }
            for (int j = i * i; j <= 70000; j += i) {
                minDiv[j] = i;
            }
        }
    }
    phi[1] = 1; 
    for (int i = 2; i <= 70000; ++i) {
        phi[i] = phi[i / minDiv[i]];
        if ((i / minDiv[i]) % minDiv[i]) {
            phi[i] *= (minDiv[i] - 1);
        }
        else {
            phi[i] *= minDiv[i];
        }
    }
}

int main() {
    geteuler();
    int n;
    while (cin >> n) {
        cout << phi[n - 1] << endl;
    }
    return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved