Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37161
Test instructions: gcd(x, y) = 质数, 1 <= x, y <= n
the logarithm of
Idea: Obviously (x, y) = k, 1 <= x, y <= n
the logarithm of gcd is equal to (x, y) = 1, 1 <= x, y <= n/k
the logarithm of seeking. So, enumerate each prime p
number and then ask (x, y) = 1, 1 <= x, y <= n/p
for GCD. So the crux of the matter is how to ask gcd (x, y) = 1, 1 <= x, y <= n/pi, [1, y]
and the y
number of coprime, phi(y)
if we make x < y
, then the answer is sigma(phi(y))
. Because x, y
it is equivalent, so the answer is * *, and because (1, 1)
there are only a pair, so-1. The final answer is sigma(sigma(phi(n/prime[i])) * 2 - 1)
.
Code
1#include <cstdio>2#include <cstring>3typedefLong LongLL;4 Const intMAXN =10000005;5 6 LL PHI[MAXN];7 intPRIMES[MAXN];8 BOOL is[MAXN];9 intCNT;Ten One A voidInitintN) - { -phi[1] =1L; theCNT =0; -Memset is,false,sizeof( is)); - for(inti =2; I <= N; ++i) { - if(! is[i]) { +primes[cnt++] =i; -Phi[i] = i-1; + } A for(intj =0; J < cnt && I * primes[j] <= N; ++j) { at is[i * primes[j]] =true; - if(i% primes[j] = =0) Phi[i * Primes[j]] = phi[i] *Primes[j]; - ElsePhi[i * Primes[j]] = phi[i] * (PRIMES[J)-1); - } - } - } in - intMain () to { + intN; - while(SCANF ("%d", &n)! =EOF) { the init (n); *LL ans =0; $ for(inti =2; I <= N; ++i) Phi[i] + = phi[i-1]; Panax Notoginseng for(inti =0; I < CNT; ++i) { -Ans + = phi[n/primes[i]] *2-1; the } +printf"%lld\n", ans); A } the return 0; +}
Bzoj 2818 GCD (Euler function)