POJ 1284 Primitive Roots
阿新 • • 發佈:2018-07-30
inpu clu 分享 urn jpg ive prime tle mod
Write a program which given any odd prime 3 <= p < 65536 outputs the number of primitive roots modulo p.
Primitive Roots
We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (x i 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
題意:給一個奇素數n,(1,n-1)內的數i,(i^1,i^2,i^3,,,i^n-1)分別%n,正好是(1,2,3,,,n-1),可以不按順序,求滿足這個式子的i的個數
思路:首先他說取余後正好是(1,n-1),n又是奇素數,這讓我們想到其實這個就是n中所有與n互質的數,奇素數的歐拉數就是n-1,也就是組成的一個縮系,
然後我們再看那個i^1,,,,i^n-1很像求原根的條件
就是他這裏是g^0開始到n-2次方為止,題目是g^1到n-1,那麽我們可不可以進行一個轉換呢
g^0我們可以看作1,那麽我們可以由歐拉定理得知 g^a(m)=1mod
所以我們可以得知題目就是求一個原根
而求原根數有一個公式 原根數=phi(phi(m))
奇素數的歐拉數就是 奇素數-1 所以在本題中也可以改為 phi(m-1)
#include<cstdio> #include<cmath> usingnamespace std; typedef long long ll; ll phi(ll n) { ll sum=n; for(int i=2;i*i<=n;i++) { if(n%i==0) { sum=sum-sum/i; do { n/=i; }while(n%i==0); } } if(n>1) sum=sum-sum/n; return sum; } int main() { ll n; while(scanf("%lld",&n)!=EOF) { printf("%lld\n",phi(phi(n))); } }
POJ 1284 Primitive Roots