UVa 10539 (Sieve primes, two-point search) almost prime Numbers

Source: Internet
Author: User

Test instructions

Find the number of integers between L and U x satisfies the form such as X=PK, where P is prime, k>1

Analysis:

First, sift out the primes in 1e6, enumerate each prime number to find out 1e12 of all the numbers that satisfy the condition, and then sort.

For L and U, the two points find the subscript of the maximum number less than U and L, and the difference is the answer.

1#include <cstdio>2#include <cmath>3#include <algorithm>4 5typedefLong LongLL;6 7 Const intMAXN =1000000;8 Const intMAXP =80000;9 Ten BOOLVIS[MAXN +Ten]; One intPRIME[MAXP], CNTP =0; ALL A[MAXN], cnt =1; -  - voidInit () the { -     intm = +; -      for(inti =2; I <= m; ++i)if(!Vis[i]) -          for(intj = i * I; J <= Maxn; J + = i) vis[j] =true; +      for(inti =2; I <= MAXN; ++i)if(!vis[i]) prime[cntp++] =i; -  +      for(inti =0; i < CNTP; ++i) A     { atll temp = (LL) prime[i] *Prime[i]; -          while(Temp <=1000000000000LL) -         { -a[cnt++] =temp; -Temp *=Prime[i]; -         } in     } -Std::sort (A, A +CNT); to } +  - intBinary_search (LL N) the { *LL L =0, R =CNT; $      while(L <R)Panax Notoginseng     { -LL mid = ((L + R +1) >>1); the         if(A[mid] <= N) L =mid; +         ElseR = mid-1; A     } the     returnL; + } -  $ intMain () $ { - Init (); -     intT; thescanf"%d", &T); -      while(t--)Wuyi     { the LL hehe, haha; -scanf"%lld%lld", &hehe, &haha); Wu         intt = Binary_search (hehe), k =binary_search (haha); -         intAns = k-T; About         if(A[t] = = hehe) ans++; $printf"%d\n", ans); -     } -  -     return 0; A}
code June

UVa 10539 (Sieve primes, two-point search) almost prime Numbers

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.