poj 1284——Primitive Roots

来源:互联网 发布:打电话自动录音软件 编辑:程序博客网 时间:2024/05/17 03:52

题意:求65536以内的素数的原根个数

思路:素数的原根个数就是phi(p-1)。

错误:没有注意到题目中说了是素数,就从1到p一个一个试原根,导致超时

代码如下:

#include<cstdio>#include<iostream>#include<cstring>#include<vector>using namespace std;const int maxn=70000;int minDiv[maxn],phi[maxn],sum[maxn];void genPhi(){    for(int i=1;i<maxn;++i)    {        minDiv[i]=i;    }    for(int i=2;i*i<maxn;++i)    {        if(minDiv[i]==i){            for(int j=i*i;j<maxn;j+=i)            {                minDiv[j]=i;            }        }    }    phi[1]=1;    for(int i=2;i<maxn;++i){        phi[i]=phi[i/minDiv[i]];        if((i/minDiv[i])%minDiv[i]==0)        {            phi[i]*=minDiv[i];        }        else{            phi[i]*=(minDiv[i]-1);        }    }}int main(){ //   freopen("data.txt","r",stdin);    genPhi();    int n;    while(scanf("%d",&n)!=EOF)    {        printf("%d\n",phi[n-1]);    }    return 0;}


0 0
原创粉丝点击