CF385C.Bear and Prime Numbers

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

You are given a sequence of integers _x_1, _x_2, ..., x__n of length n and m queries, each of them is characterized by two integers l__i, r__i. Let's introduce f(p) to represent the number of such indexes k, that x__k is divisible by p. The answer to the query l__i, r__i is the sum: , where S(l__i, r__i) is a set of prime numbers from segment [l__i, r__i] (both borders are included in the segment).

Help the bear cope with the problem.

最近,小熊开始学习数据结构,并遇到了如下问题。

给定一个长度为 nn 的整数序列 x1,x2,…,xnx_1, x_2, \dots, x_n,以及 mm 个查询,每个查询由两个整数 li,ril_i, r_i 描述。定义函数 f(p)f(p) 表示满足 xkx_k 能被 pp 整除的下标 kk 的个数。查询 li,ril_i, r_i 的答案为如下求和式:
,
其中 S(li,ri)S(l_i, r_i) 是区间 [li,ri][l_i, r_i](含端点)内所有质数构成的集合。

请帮助小熊解决该问题。

输入格式

The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers _x_1, _x_2, ..., x__n (2 ≤ x__i ≤ 107). The numbers are 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, l__i and r__i (2 ≤ l__i ≤ r__i ≤ 2·109) — the numbers that characterize the current query.

第一行包含一个整数 nn(1≤n≤1061 \leq n \leq 10^6)。第二行包含 nn 个整数 x1,x2,…,xnx_1, x_2, \dots, x_n(2≤xi≤1072 \leq x_i \leq 10^7)。这些数不一定互不相同。

第三行包含一个整数 mm(1≤m≤500001 \leq m \leq 50000)。接下来的 mm 行中,每行包含一对用空格分隔的整数 lil_i 和 rir_i(2≤li≤ri≤2⋅1092 \leq l_i \leq r_i \leq 2 \cdot 10^9),表示当前查询的参数。

输出格式

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

输出 m 个整数——即按输入中查询出现的顺序给出各查询的答案。

输入输出样例

  • 输入#1

    6
    5 5 7 10 14 15
    3
    2 11
    3 12
    4 4

    输出#1

    9
    7
    0
  • 输入#2

    7
    2 3 5 7 11 4 8
    2
    8 10
    2 123

    输出#2

    0
    7

说明/提示

Consider the first sample. Overall, the first sample has 3 queries.

  1. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9.
  2. 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.
  3. The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0.

考虑第一个样例。总体而言,第一个样例包含 3 个查询。

  1. 第一个查询为 l=2l = 2,r=11r = 11。你需要计算 f(2)+f(3)+f(5)+f(7)+f(11)=2+1+4+2+0=9f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9。
  2. 第二个查询为 l=3l = 3,r=12r = 12。你需要计算 f(3)+f(5)+f(7)+f(11)=1+4+2+0=7f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7。
  3. 第三个查询为 l=4l = 4,r=4r = 4。由于该区间内不含质数,因此和为 0。

输入解题思路,AI测评打分。不知道怎么写?

首页