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 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.

Input

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 andri(2 ≤ li ≤ ri ≤ 2·109) — the numbers that 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.

  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.

查找一个区间内的,一系列数中,包含素数的个数。基本思路是:把这一系列数字中,到最大的数字Max之前是数字包含是素数的个数统计出来,然后用统计的个数区间的右端值减掉左端值减一,sum[right]-sum[left-1].因为求解的素数是包括最左端的这一个的,所以左端值应该减掉一。

比如说 对这样一个序列:5 6 7 ,则sum[1]=0;sum[2]=1;sum[3]=1;sum[4]=1;sum[5]=1;sum[6]=3;sum[7]=4; 若le=2,ri=7;ans=sum[7]-sum[1]=4;

codeforces 385 c的更多相关文章

  1. Codeforces 385 C Bear and Prime Numbers

    题目链接~~> 做题感悟:这题属于想法题,比赛时直接做的 D 题.可是处理坐标处理的头晕眼花的结果到最后也没AC. 解题思路: 由于查询的时候仅仅考虑素数,so~我们仅仅考虑素数就能够,这就须要 ...

  2. CodeForces 385 D.Bear and Floodlight 状压DP

    枚举灯的所有可能状态(亮或者不亮)(1<<20)最多可能的情况有1048576种 dp[i]表示 i 状态时灯所能照射到的最远距离(i 的二进制中如果第j位为0,则表示第j个灯不亮,否则就 ...

  3. Codeforces 385 D Bear and Floodlight

    主题链接~~> 做题情绪:时候最后有点蛋疼了,处理点的坐标处理晕了.so~比赛完清醒了一下就AC了. 解题思路: 状态压缩DP ,仅仅有 20 个点.假设安排灯的时候仅仅有顺序不同的问题.全然能 ...

  4. Codeforces Round #385 (Div. 2) B - Hongcow Solves A Puzzle 暴力

    B - Hongcow Solves A Puzzle 题目连接: http://codeforces.com/contest/745/problem/B Description Hongcow li ...

  5. Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift 水题

    A. Hongcow Learns the Cyclic Shift 题目连接: http://codeforces.com/contest/745/problem/A Description Hon ...

  6. Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards

    地址:http://codeforces.com/problemset/problem/744/C 题目: C. Hongcow Buys a Deck of Cards time limit per ...

  7. Codeforces Round #385 (Div. 2) Hongcow Builds A Nation —— 图论计数

    题目链接:http://codeforces.com/contest/745/problem/C C. Hongcow Builds A Nation time limit per test 2 se ...

  8. Codeforces Round #385 (Div. 2) C - Hongcow Builds A Nation

    题目链接:http://codeforces.com/contest/745/problem/C 题意:给出n个点m条边,还有k个不能连通的点,问最多能添加几条边. 要知道如果有n个点最多的边是n*( ...

  9. Codeforces Round #385(div 2)

    A =w= B QwQ C 题意:n个点m条边的无向图,其中有k个特殊点,你在这张图上尽可能多的连边,要求k个特殊点两两不连通,问最多能连多少边 分析:并查集 对原图做一次并查集,找出特殊点所在集合中 ...

随机推荐

  1. seajs+spm之再研究

    好久没有用seajs了,之前对spm也只是一知半解,这些天再次拿起来研究.谈谈我的认识与理解. 声明:本文不适合对seajs完全不了解的同学阅读.对于想知道seajs来龙去脉以及spm相关的同学&qu ...

  2. ThinkPHP访问不存在的模块跳到404页面

    在ACTION中新建一个文件EmptyAction.class.php,文件中的代码如下: <?php class EmptyAction extends Action{     functio ...

  3. HDU 1878 欧拉回路 图论

    解题报告:题目大意,给出一个无向图,判断图中是否存在欧拉回路. 判断一个无向图中是否有欧拉回路有一个充要条件,就是这个图中不存在奇度定点,然后还要判断的就是连通分支数是否为1,即这个图是不是连通的,这 ...

  4. [Effective JavaScript 笔记]第37条:认识到this变量的隐式绑定问题

    CSVReader示例 需求 CSV(逗号分隔型取值)文件格式是一种表格数据的简单文本表示 张三,1982,北京,中国 小森,1982,东京,日本 吉姆,1958,纽约,美国 现需要编写一个简单的.可 ...

  5. Minimum Depth of Binary Tree

    二叉树的最小深度 采用递归的方式求左右结点的高度,注意判断一个结点是否是叶子结点(左右子树都不存大). int minDepth(TreeNode *root) { return minDepth(r ...

  6. ZeroMQ安装

    一.ZeroMQ介绍 ZeroMQ是一个开源的消息队列系统,按照官方的定义,它是一个消息通信库,帮助开发者设计分布式和并行的应用程序. 首先,我们需要明白,ZeroMQ不是传统的消息队列系统(比如Ac ...

  7. Nmap备忘单:从探索到漏洞利用(Part 2)

    这是我们的第二期NMAP备忘单(第一期在此).基本上,我们将讨论一些高级NMAP扫描的技术,我们将进行一个中间人攻击(MITM).现在,游戏开始了. TCP SYN扫描 SYN扫描是默认的且最流行的扫 ...

  8. iOS 和 Android 触摸事件传递

    先看文章,写得很好 ios 触摸事件传递 http://www.cnblogs.com/Quains/p/3369132.html 另外一篇 http://blog.csdn.net/yongyinm ...

  9. codeforces A. K-Periodic Array 解题报告

    题目链接:http://codeforces.com/problemset/problem/371/A 题目意思:给出n和k和一个只有1或者2组成的序列,需要求出最少的改变次数,使得 n/k 组里面的 ...

  10. July 30th, Week 31st Saturday, 2016

    No matter how far you may fly, never forget where you come from. 无论你能飞多远,都别忘了你来自何方. No matter how fa ...