Clever use of Prime sieve 2

Source: Internet
Author: User
Tags data structures

Description
Recently, the bear started studying data structures and faced the following problem.

You are given a sequence of integers x1, x2, ..., xn of length n and M queries, each of them was characterized by S Li, RI. Let's introduce F (p) to represent the number of such indexes K, that XK was divisible by P. The answer to the query Li, ri are the sum:, where S (Li, RI) is a set of prime numbers from segment [Li, RI] (both borders is included in the segment).

Help the Bear cope with the problem.

Input
The first line contains integer n (1≤n≤106). The second line contains n integers x1, x2, ..., xn (2≤xi≤107). The numbers is not necessarily distinct.

The third line contains integer m (1≤m≤50000). Each of the following m lines contains a pair of space-separated integers, Li and ri (2≤li≤ri≤2 109)-the numbers th At characterize, the current query.

Output
Print m integers-the answers to the queries on the order the queries appear in the input.

Sample Input
Input
6
5 5 7 10 14 15
3
2 11
3 12
4 4
Output
9
7
0
Input
7
2 3 5 7 11 4 8
2
8 10
2 123
Output
0
7
Hint
Consider the first sample. Overall, the first sample has 3 queries.

The first query L = 2, R = comes. You need to count F (2) + F (3) + F (5) + F (7) + f (11) = 2 + 1 + 4 + 2 + 0 = 9.
The second query comes L = 3, R = 12. You need to count F (3) + F (5) + F (7) + f (11) = 1 + 4 + 2 + 0 = 7.
The third query comes L = 4, R = 4. As this interval have no prime numbers, then the sum equals 0.

At the beginning of the thought might use a tree-like array or something, the result is not as violent as others

 #include <cstdio> #include <iostream> using namespace std; const int max=10000010;
int n,cnt[max],vis[max],is[max],m,a,b,t;
            void Init () {for (int i=2;i<max;i++) {if (!is[i]) {cnt[i]+=vis[i];
        for (int j=i+i;j<max;j+=i) IS[J]=1,CNT[I]+=VIS[J];
}} for (int i=2;i<max;i++) cnt[i]+=cnt[i-1];
    } int main () {scanf ("%d", &n);
        for (int i=0;i<n;i++) {scanf ("%d", &t);
    vis[t]++;
    } init ();
    scanf ("%d", &m);
        while (m--) {scanf ("%d%d", &a,&b);
        if (a>max-1) a=max-1;
       if (b>max-1) b=max-1;
        cout<< "----" <<cnt[b]<< "" <<cnt[a-1]<<endl;
    printf ("%d\n", Cnt[b]-cnt[a-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.