51NOD 1135 Original Root

Source: Internet
Author: User

/*Violent practices: enumeration starting from 2, then the violent Judgment g^ (P-1) = 1 (mod P) if and only if the exponent is P-1, set up 25 points 0.0*/#include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>using namespacestd;Long LongMi (intAintMintp) {    if(m==0)return 1; Long LongX=mi (a,m/2, p)%p; X=x*x%p; if(m%2==1) x=x*a%p; returnx;}intGet_root (intp) {     for(intg=2; g;g++)      {          intX=mi (g,p-1, p), falg=0; if(x!=1)Continue;  for(intI=2; i<=p-2; i++)            if(Mi (g,i,p) = =1) {Falg=1;  Break; }        if(falg==0)returnG; }}intMain () {intp; scanf ("%d",&p); printf ("%d\n", Get_root (p)); return 0;}
/*find out all the different x-1 of the p1,p2...pm, for any 2<=a<=x-1, determine whether a is the root of x, only need to test a^ ((x-1)/p1), a^ ((x-1)/p2),... a^ ((x-1)/pm) in the number of M , whether there is a number mod x is 1, if present, a is not the original root of x, otherwise is the original root of X. O (M) *log (P-1) 100 min*/#include<iostream>#include<cstdio>#include<cstring>#defineMAXN 100010using namespacestd;intN,prime[maxn],l,f[maxn],ans[maxn],num;voidGet_prime () { for(intI=2; i<=maxn;i++)      {          if(!f[i]) prime[++l]=i;  for(intj=1; j<=l;j++)            {                if(I*PRIME[J]&GT;MAXN) Break; F[i*prime[j]]=1; if(i%prime[j]==0) Break; }      }}voidGet_num () {intk=n-1;  for(intI=1; i<=l;i++)      {          intfalg=0;  while(k%prime[i]==0) {k=k/Prime[i]; Falg=1; }        if(FALG) ans[++num]=Prime[i]; if(k==1) Break; }}Long LongMi (intAintMintp) {    if(m==0)return 1; Long LongX=mi (a,m/2, p)%p; X=x*x%p; if(m%2==1) x=x*a%p; returnx;}intJudgeintx) {     for(intI=1; i<=num;i++)      if(Mi (x, (n1)/ans[i],n) = =1)        return 0; return 1;}intMain () {scanf ("%d",&N);    Get_prime ();    Get_num ();  for(intI=2; i;i++)      if(judge (i)) {printf ("%d\n", i); return 0; }}

51NOD 1135 Original Root

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.