Primitive Roots
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3381   Accepted: 1980

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

一个数m的1次方,2次方,3次方到n-1次方 mod n 得到的数值各不相同,就说m是n的原根。

一个数是数n的原根就必然与n-1互质,所以求n的原根的数量即是求欧拉函数n-1。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; long long euler(long long n)
{
long long res = n, a = n;
for (long long i = 2; i*i <= a; i++)
{
if (a%i == 0)
{
res = res / i*(i - 1);
while (a%i == 0)a /= i;
}
}
if (a > 1)res = res / a*(a - 1);
return res;
} int main()
{
long long n;
while (cin >> n)
{
cout << euler(n-1) << endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1284:Primitive Roots 求原根的数量的更多相关文章

  1. poj 1284 Primitive Roots (原根)

    Primitive Roots http://poj.org/problem?id=1284 Time Limit: 1000MS   Memory Limit: 10000K       Descr ...

  2. poj 1284 Primitive Roots(原根+欧拉函数)

    http://poj.org/problem?id=1284 fr=aladdin">原根 题意:对于奇素数p,假设存在一个x(1<x<p),(x^i)%p两两不同(0&l ...

  3. POJ 1284 Primitive Roots 数论原根。

    Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2479   Accepted: 1385 D ...

  4. POJ 1284 Primitive Roots 原根

    题目来源:POJ 1284 Primitive Roots 题意:求奇素数的原根数 思路:一个数n是奇素数才有原根 原根数是n-1的欧拉函数 #include <cstdio> const ...

  5. POJ 1284 Primitive Roots (求原根个数)

    Primitive Roots 题目链接:id=1284">http://poj.org/problem?id=1284 利用定理:素数 P 的原根的个数为euler(p - 1) t ...

  6. POJ 1284 Primitive Roots (欧拉函数+原根)

    <题目链接> 题目大意: 满足{ ( $x^{i}$ mod p) | 1 <=$i$ <= p-1 } == { 1, …, p-1 }的x称为模p的原根.给出p,求原根个数 ...

  7. poj 1284 Primitive Roots(未完)

    Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3155   Accepted: 1817 D ...

  8. (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))

    /* * POJ_2407.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...

  9. poj 1284 Primitive Roots

    从来没有接触过完全剩余系,不会证明,知道看了别人的题解才知道要用欧拉函数: 下面是证明过程: p是奇素数,如果{xi%p | 1 <= i <= p - 1} = {1,2,...,p-1 ...

随机推荐

  1. nodejs(12)Express 中间件middleware

    中间件 客户端的请求到达服务器时,他的生命周期是:request -- 服务器端处理 -- 响应 在服务器端处理过程中,业务逻辑复杂时,为了便于开发维护,需要把处理的事情分成几步,这里每一步就是一个中 ...

  2. python类的对象使用

    class student():    def __init__(self,age,sex):        self.age = age        self.sex = sex  #self._ ...

  3. 1-6SpringBoot之事务管理@Transactional

    以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作: 用来保证一致性,即service方法里的多个dao操作,要 ...

  4. C#中的扩展类的理解

    扩展类是一种静态的一种类的调用方法,通过实例化进行调用.利用this进行指正该类,有参数的时候直接在后面追加参数. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  5. 201706 Ruby 基础 & 元编程

    yield yield self Proc yield带参数 rails中:yield 和 content_for methods.proc.lambda.block 闭包(用proc延长变量的生命周 ...

  6. 小程序包大小超过2M的解决方法

    小程序的包被限制在2M以下, 超出的时候点击预览, 发现报错: Error: 代码包大小为 3701 kb,上限为 2048 kb,请删除文件后重试 1. 优化代码, 删除掉不用的代码 2. 图片压缩 ...

  7. Combobox出现System.Data.DataRowView的原因

    这种情况多次遇到.有时候明明完全相同的代码,在不同的场景运行却是两种结果, 其中一种坏的结果就是 comboBox所有的项都显示为System.Data.DataRowView 今天仔研究了一下,应该 ...

  8. java List 去重方式及效率对比

    01 实现思路:使用两个for循环遍历集合所有元素,然后进行判断是否有相同元素,如果有,则去除.这种方式是大部分最先想到的,也是最简单的实现方式.其中,这种方式可以保证List集合原来的顺序不变. 代 ...

  9. 如何在ESXi 5.x/6.x(2005205​​)中下载和安装异步驱动程序

    本文提供了在ESXi 5.x和6.x中下载和安装异步驱动程序的步骤 https://kb.vmware.com/s/article/2005205

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-pencil

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...