Bzoj 2440 Full Square (Möbius inversion + binary search)

Source: Internet
Author: User

Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23362

Test instructions: Defines a number that contains a squared factor as a complete square number (the square number factor does not contain 1). The number of k non-complete squares is obtained.

Idea: We first find the number of non-complete squares of [1, N], and then use the binary to look for n that is exactly equal to K (note that n may be more than one, the leftmost). The key is, how to find the former, we can use the principle of tolerance, with n-[1, N] The number of complete square number, to [1, N] The number of complete square number, with the capacity to find the front coefficient is Möbius function, directly with Möbius inversion can be, the result is Sigma (mu[i]* (n/(i*i) ))。

Code

1#include <cstdio>2#include <cstring>3 using namespacestd;4 5typedefLong LongLL;6 7 ConstLL INF = 2e10 +7;8 Const intMAXN =100005;9 Ten BOOLCHECK[MAXN]; One intPRIMES[MAXN]; A intMU[MAXN]; - LL K; -  the voidMoblus () - { -memset (check,false,sizeof(check)); -mu[1] =1; +     intCNT =0; -      for(inti =2; i < MAXN; ++i) { +         if(!Check[i]) { Aprimes[cnt++] =i; atMu[i] =-1; -         } -          for(intj =0; J < CNT; ++j) { -             if(i * primes[j] > MAXN) Break; -Check[i * Primes[j]] =true; -             if(i% primes[j] = =0) { inMu[i * Primes[j]] =0; -                  Break; to}Else { +Mu[i * Primes[j]] =-Mu[i]; -             } the         } *     } $ }Panax Notoginseng  - ll Cal (ll N) the { +LL ret =N; A      for(LL i =2; I * I <= N; ++i) { theRET + = mu[i] * (N/(i *i)); +     } -     returnret; $ } $  - intMain () - { the Moblus (); -     intncase;Wuyiscanf"%d", &ncase); the      while(ncase--) { -scanf"%lld", &k); WuLL LHS =1L; -LL RHS =INF; About LL mid; $          while(LHS <RHS) { -MID = (RHS + LHS)/2; -LL tmp =Cal (mid); -             if(TMP < k) LHS = mid +1; A             ElseRHS =mid; +         } theprintf"%lld\n", LHS); -     } $     return 0; the}

Bzoj 2440 Full Square (Möbius inversion + binary search)

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.