Hdu4992 primitive roots (all original roots)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4992

Question: Give N and output all the original roots of N.

Idea: obtain an original root X of N. For the I, I <PHI (n) and (I, Phi (N) = 1, x ^ I % N is the original root of N.

 

int Euler(int n){    int i,ans=n;    for(i=2;i*i<=n;i++) if(n%i==0)    {        ans=ans/i*(i-1);        while(n%i==0) n/=i;    }    if(n>1) ans=ans/n*(n-1);    return ans;}int prime[N],tag[N],cnt;void init(){int i,j;for(i=2;i<N;i++) if(!tag[i]){prime[cnt++]=i;for(j=i+i;j<N;j+=i) tag[j]=1;}tag[1]=1;}int get(int x){if(x%2==0) return 0;int i;for(i=0;i<cnt&&prime[i]*prime[i]<=x;i++) if(x%prime[i]==0){while(x%prime[i]==0) x/=prime[i];if(x!=1) return 0;return prime[i];}return x;}int n;int myPow(i64 x,i64 y){i64 ans=1;while(y){if(y&1) ans=ans*x%n;x=x*x%n;y>>=1;}return (int)ans;}int ee;vector<int> V;int ok(int t){if(myPow(t,ee)!=1) return 0;int i;FOR0(i,SZ(V)) if(myPow(t,V[i])==1) return 0;return 1;}int Gcd(int x,int y){if(y==0) return x;return Gcd(y,x%y);}int main(){init();while(scanf("%d",&n)!=-1){if(n==2){puts("1");continue;}if(n==4){puts("3");continue;}int t=n%2==0?get(n/2):get(n);if(!t){puts("-1");continue;}ee=Euler(n);V.clear();int i;for(i=2;i*i<=ee;i++) if(ee%i==0){V.pb(i);if(i*i!=ee) V.pb(ee/i);}for(i=2;i<n;i++) if(ok(i)) break;int Min=i;vector<int> ans;for(i=1;i<ee;i++) {if(Gcd(i,ee)!=1) continue;ans.pb(myPow(Min,i));}sort(all(ans));for(i=0;i<SZ(ans);i++){if(i) putchar(‘ ‘);printf("%d",ans[i]);}puts("");}}

 

Hdu4992 primitive roots (all original roots)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.