Poj 1284 Primitive Roots
文章目錄
- Description
- 題意:
- 題解:
- 代碼:
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 79Sample Output
10 8 24題意:
給定一個p,存在一個x,使得xi%p的值的集合(i的范圍是1~p-1)是{1,2…p-1},求出x是多少?
題解:
數(shù)論題,涉及歐拉公式
先介紹一個概念:
設(shè)m是正整數(shù),a是整數(shù),若a模m的階等于φ(m),則稱a為模m的一個原根。(其中φ(m)表示m的歐拉函數(shù))
假設(shè)一個數(shù)g對于P來說是原根,那么gi mod P的結(jié)果兩兩不同,且有 1<g<P, 0<i<P,那么g可以稱為是P的一個原根,歸根到底就是g(P-1) = 1 (mod P)當(dāng)且僅當(dāng)指數(shù)為P-1的時候成立.(這里P是素?cái)?shù)).
選自百度百科
結(jié)合到本題,x就是p的一個原根,那我們只需要找到滿足xp-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; } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Poj 1284 Primitive Roots的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux系统时间\硬件时间(date、
- 下一篇: 一起开心2020暑假训练第一周