[模板]原根
原根就是一种数论的定义,详情请见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++) #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 p; 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; }
妙极.
只想找一个不会伤害我的人