@[toc]
Poj 1284
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6485 Accepted: 3697
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
题意:
给定一个p,存在一个x,使得x^i^%p的值的集合(i的范围是1~p-1)是{1,2.....p-1},求出x是多少?
题解:
数论题,涉及欧拉公式
先介绍一个概念:
设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的欧拉函数)
假设一个数g对于P来说是原根,那么g^i^ mod P的结果两两不同,且有 1<g<P, 0<i<P,那么g可以称为是P的一个原根,归根到底就是g^(P-1)^ = 1 (mod P)当且仅当指数为P-1的时候成立.(这里P是素数).
选自百度百科
结合到本题,x就是p的一个原根,那我们只需要找到满足x^p-1^=1(mod P)这个式子就可以,这个式子也就是欧拉公式的φ(p-1)
欧拉公式的讲解可以看这里
代码:
#include<iostream> using namespace std; typedef long long ll; ll Euler(ll n){ ll res=n; for(ll i=2;i*i<=n;i++) { if(n%i==0){ n/=i; res=res-res/i; } while(n%i==0)n/=i; } if(n>1)res=res-res/n; return res; } int main() { int p; while(cin>>p) { cout<<Euler(p-1)<<endl; } return 0; }