Ultraviolet A 10539-almost prime numbers (number theory)

Source: Internet
Author: User

Link to the Q & A 10539-almost prime numbers topic

Scope: Low ~ High: the number of records in this range must be n = Pb (P is a prime number ).

Solution: first, the prime number within 1e6 is processed, and then solve (high) is used for each range )? Solve (low? 1), solve (n) is used to process the number of numbers that meet the requirements less than N. Enumerative prime numbers. You can judge them.

#include <cstdio>#include <cstring>typedef long long ll;const int maxn = 1e6;ll lower, up, prime[maxn];int cp, v[maxn];void primeTable (int n) {    cp = 0;    memset(v, 0, sizeof(v));    for (int i = 2; i <= n; i++) {        if (v[i])            continue;        prime[cp++] = i;        for (int j = 2 * i; j <= n; j += i)            v[j] = 1;    }}ll solve (ll n) {    ll ans = 0;    for (int i = 0; i < cp; i++) {        ll u = prime[i] * prime[i];        if (u > n)            break;        while (u <= n) {            u *= prime[i];            ans++;        }    }    return ans;}int main () {    primeTable(maxn);    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%lld%lld", &lower, &up);        printf("%lld\n", solve(up) - solve(lower-1));    }    return 0;}

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.