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 is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: , where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment).
Help the bear cope with the problem.
The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers x1, x2, ..., xn (2 ≤ xi ≤ 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, li and ri(2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query.
Print m integers — the answers to the queries on the order the queries appear in the input.
6 5 5 7 10 14 15 3 2 11 3 12 4 4
9 7 0
7 2 3 5 7 11 4 8 2 8 10 2 123
0 7
Consider the first sample. Overall, the first sample has 3 queries.
- 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.
- 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 has no prime numbers, then the sum equals 0.
题意:
给出 n (1 <= n <= 10^6),代表有 n 个数,后给出这 n 个数(X1……Xn)(2 ≤ xi ≤ 10^7)。随后给出 que (1 <= que <= 50000 ),代表有 que 个问题,每个 que 都有一个 l 和 r(2 <= l <= r <= 2 X 10^9)。统计在 l 到 r 内的每一个素数 a 能整除 Xi 的个数之和。
思路:
素数筛选。
1.注意数据范围,除数和被除数关系,一开始只注意到左右边界最大范围是 2 X 10^9 ,是用范围里面的数去除 Xi ,而 Xi 范围是 2 ≤ xi ≤ 10^7,若范围内的数大于 n ,则无论如何都不能整除的,所以暗含条 件只需要把数组开到10^7。一开始没有注意到这点,于是把问题想得复杂了;
2.于是用 num 数组统计 n 个数的数量情况,用 vis 进行素数筛选,用 sum 统计每个数能整除 n 内多少个数的情况;
3.最后注意求和的方法,用容斥定理相减求和更加省时。
AC:
#include<stdio.h> #define MAX 10000005 int num[MAX],vis[MAX],sum[MAX],n; void primer() { for(int i = 2;i < MAX;i++) { if(vis[i]) continue; for(int j = i;j < MAX;j += i) { sum[i] += num[j]; vis[j] = 1; } } } int main() { int que; scanf("%d",&n); while(n--) { int a; scanf("%d",&a); num[a]++; } primer(); for(int i = 2;i < MAX;i++) sum[i] += sum[i-1]; //容斥定理求和 scanf("%d",&que); while(que--) { __int64 l,r; scanf("%I64d%I64d",&l,&r); if(r >= MAX) r = MAX - 1; if(l >= MAX) l = MAX - 1; //注意,l 和 r 最大也只能是 MAX - 1 printf("%d\n",sum[r] - sum[l - 1]); } return 0; }
相关推荐
PrimeNumber 素数生成器 V6.0.0.3 18.4KB 可快速生成指定范围内的所有素数,并可格式化输出;还可对单个自然数快速因数分解。 HugeCalc V6.x 以上版本现已提供该程序相应导出接口,欢迎使用。 若借助算法库 ...
PrimeNumber 素数生成器 V7.0.0.0 18.7 KB 可快速生成指定范围内的所有素数,并可格式化输出;还可对单个自然数快速因数分解。 HugeCalc V6.x 以上版本现已提供该程序相应导出接口,欢迎使用。 若借助算法库 ...
使用AKS算法检测素数和生成素数. 提供了AKS的6个步骤的方法 绝对原创
超高速的素数筛选法,大数下10^6区间内筛选所有素数仅需30MS!
这是我个人对原始素数筛选法的一个优化方法,效率提高一倍以上。 (文档内的代码为C/C++代码)
这是我个人对原始素数筛选法的一个优化方法,效率提高一倍以上。
而后编制主函数,任意输入一个大于4的偶数d,找出满足d=d1+d2的所有数对,其中要求d1与d2均为素数(通过调用prime来判断素数)。如偶数18可以分解为11+7以及13+5;而偶数80可以分解为:43+37、61+19、67+13、73+7。...
编制具有如下原型的函数prime,用来判断整数n是否为素数:bool prime(int n); 而后编制主函数,任意输入一个大于4的偶数d,找出满足d=d1+d2的所有数对,其中要求d1与d2均为素数(通过调用prime来判断素数)。如偶数...
primesieve每个筛选质数使用8个字节,因此其内存使用量约为 每个线程的字节数。安装可以使用操作系统的程序包管理器来安装primesieve命令行程序。 为了使用libprimesieve进行开发,您可能需要安装libprimesieve-dev...
PYTHON 求给定范围内质数和 代码。 def get_primes(n): numbers = set(range(n, 1, -1)) primes = [] while numbers: p = numbers.pop() primes.append(p) numbers.difference_update(set(range(p*2, n+1, p))...
质数筛选器.py
这是我个人对原始素数筛选法的一个优化方法,效率提高一倍以上。
在初学c++时经常碰到关于素数的题,但是按传统做法总是会超时,先用筛选法将素数找出。
Prime Numbers - The Most Mysterious Figures in Math
prime函数判定素数.c
素数筛选法的改进及C语言实现.pdf
使用筛选法来确定100以内的素数并将其输出 使用时请在dev运行
Matlab环境下素数筛选算法的分析及比较.pdf
素数(prime number)又称质数,有无限个。除了1和它本身外,不能被其他自然数整除。换句话说就是该数除了1和它本身以外不再有其他的因数的数。 注意:最小的素数是2。 话不多说,上代码! prime=[] #用一个列表来存储...
主要为大家详细介绍了python素数筛选法的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下