UVa11424 Gcd-extreme (I)

Source: Internet
Author: User

Direct double loop O (n^2) Calculate gcd ... It's too time consuming.

Enumeration factor A and a multiples of n, consider the number of I (i<=n) ==a of gcd (I,n)

Since gcd (i,n) ==a is equivalent to GCD (i/a,n/a) ==1, the number of gcd (i,n) ==a is satisfied

Play Euler function table, enumeration factor, calculate each n f[n]=gcd (1,n) +gcd (2,n) +gcd (3,n) +...+gcd (n-1,n)

Then ask F[n] for the prefix and, answer the question.

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 Const intmxn=200010;9 intRead () {Ten     intx=0, f=1;CharCh=GetChar (); One      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A      while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnx*F; - } the intPri[mxn],cnt=0; - Long LongPHI[MXN],F[MXN]; - voidPHI () { -      for(intI=2; i<mxn;i++){ +         if(!Phi[i]) { -phi[i]=i-1; +pri[++cnt]=i; A         } at          for(intj=1; j<=cnt && (Long Long) i*pri[j]<mxn;j++){ -             if(i%pri[j]==0){ -phi[i*pri[j]]=phi[i]*Pri[j]; -                  Break; -             } -             Elsephi[i*pri[j]]=phi[i]* (pri[j]-1); in         } -     } to     return; + } - intMain () { the PHI (); *     inti,j; $      for(i=1; i<mxn;i++) {//Enumeration FactorPanax Notoginseng          for(j=i*2; j<mxn;j+=i) { -f[j]+=i*phi[j/i]; the         } +     } A      for(i=3; i<mxn;i++) f[i]+=f[i-1]; the      while(1){ +I=read (); -         if(!i) Break; $printf"%lld\n", F[i]); $     } -     return 0; -}

UVa11424 Gcd-extreme (I)

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.