[模板]原根
阿新 • • 發佈:2018-11-14
原根就是一種數論的定義,詳情請見ssy的部落格.我這裡主要講的是poj的1284.
poj這道題要求的是一個數的原根有多少個,我一開始自己瞎做把所有原根都求出來了,程式碼如下:
#include<iostream> #include<cstdio> #include<cmath> #include<ctime> #include<queue> #include<algorithm> #include<cstring> using namespace std; #define duke(i,a,n) for(register int i = a;i <= n;i++) #definelv(i,a,n) for(register int i = a;i >= n;i--) #define clean(a) memset(a,0,sizeof(a)) const int INF = 1 << 30; typedef long long ll; typedef double db; template <class T> void read(T &x) { char c; bool op = 0; while(c = getchar(), c < '0' || c > '9')if(c == '-') op = 1; x = c - '0'; while(c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0'; if(op) x = -x; } template <class T> void write(T x) { if(x < 0) putchar('-'), x = -x; if(x >= 10) write(x / 10); putchar('0' + x % 10); } intp; int used[100005],pri[50004],top = 0; int st[70000],tp = 0; void pri_table() { for(int i = 2;i <= 1000;i++) { if(used[i] == 0) { for(int j = 2;j <= 1000 / i;j++) { used[i * j] = 1; } pri[++top] = i; } } } void doit() { clean(st);tp = 0; int now = p - 1; duke(i,1,top) { if(now == 1) break; if(pri[i] > ceil(sqrt(p))) { break; } if(now != 0 && now % pri[i] == 0) st[++tp] = pri[i];//cout<<pri[i]<<" "; while(now != 0 && now % pri[i] == 0) { now /= pri[i]; } } if(now != 1 && now != 0) { st[++tp] = now; //cout<<now<<endl; } /*puts("");*/ } int qpow(int a,int b) { int tot = 1; //cout<<a<<" "<<b<<endl; while(b) { if(b % 2 == 1) { tot *= a; tot %= p; } a *= a; a %= p; b >>= 1; } //cout<<tot<<endl; return tot; } void done() { int num = 0; int now = p - 1; /*duke(i,1,tp) printf("%d ",st[i]); puts("");*/ duke(i,2,now) { int flag = 0; //cout<<i<<endl; //cout<<tp<<endl; duke(j,1,tp) { if(qpow(i,now / st[j]) == 1) { flag = 1; break; } } if(flag == 0) { num++; } } printf("%d\n",num); } int main() { pri_table(); /*duke(i,1,10) printf("%d ",pri[i]); puts("");*/ while(scanf("%d",&p) != EOF) { tp = 0; doit(); done(); } return 0; }
但是這樣會T,百度了一下,發現一個性質,就是一個數的原根個數就是phi[phi[n]],而此題n是素數,phi[n]=n - 1;
所以直接輸出phi[n - 1]就行了.
#include<iostream> #include<cstdio> #include<cmath> #include<ctime> #include<queue> #include<algorithm> #include<cstring> using namespace std; #define duke(i,a,n) for(register int i = a;i <= n;i++) #define lv(i,a,n) for(register int i = a;i >= n;i--) #define clean(a) memset(a,0,sizeof(a)) const int INF = 1 << 30; typedef long long ll; typedef double db; template <class T> void read(T &x) { char c; bool op = 0; while(c = getchar(), c < '0' || c > '9') if(c == '-') op = 1; x = c - '0'; while(c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0'; if(op) x = -x; } template <class T> void write(T x) { if(x < 0) putchar('-'), x = -x; if(x >= 10) write(x / 10); putchar('0' + x % 10); } int eul[66666]; void phi() { duke(i,1,66666) { eul[i] = i; } for(int i = 2;i <= 66666;i++) { if(eul[i] == i) { for(int j = i;j <= 66666;j += i) { eul[j] = eul[j] / i * (i - 1); } } } return; } int main() { int n; phi(); while(~scanf("%d",&n)) { printf("%d\n",eul[n - 1]); } return 0; }
妙極.