频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
POJ1284---Primitive Roots(求原根个数, 欧拉函数)
2015-06-06 08:59:28         来源:Alex  
收藏   我要投稿

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.2cto.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;
}
点击复制链接 与好友分享!回本站首页
相关TAG标签 欧拉 函数 个数
上一篇:sgu251:Polymania(构造)
下一篇:LightOJ1007---Mathematically Hard (欧拉函数)
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站