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)