Bzoj 2818 GCD (Euler function)

Source: Internet
Author: User

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)

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.