【题目链接】

点击打开链接

【算法】

如果一个数是小X喜欢的数,那么有两种可能:

1.这个数是质数

2.这个数除以它的最小质因子是一个质数

所以我们可以用线性筛+前缀和的方式预处理,询问的时候O(1)计算就可以了

【代码】

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAXN = 1e7 + ;
  5.  
  6. int Q,l,r,i,j,tmp,tot;
  7. int f[MAXN],sum[MAXN],prime[MAXN];
  8. bool mark[MAXN];
  9.  
  10. template <typename T> inline void read(T &x) {
  11. int f=; x=;
  12. char c = getchar();
  13. for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
  14. for (; isdigit(c); c = getchar()) x = x * + c - '';
  15. x *= f;
  16. }
  17.  
  18. template <typename T> inline void write(T x) {
  19. if (x < ) { putchar('-'); x = -x; }
  20. if (x > ) write(x/);
  21. putchar(x%+'');
  22. }
  23.  
  24. template <typename T> inline void writeln(T x) {
  25. write(x);
  26. puts("");
  27. }
  28.  
  29. int main() {
  30.  
  31. for (i = ; i <= MAXN; i++) {
  32. if (!f[i]) prime[++tot] = i;
  33. if ((!f[i]) || (!mark[i/f[i]])) ++sum[i];
  34. sum[i] += sum[i-];
  35. for (j = ; j <= tot && prime[j] <= i; j++) {
  36. tmp = i * prime[j];
  37. if (tmp >= MAXN) break;
  38. f[tmp] = prime[j];
  39. mark[tmp] = ;
  40. }
  41. }
  42.  
  43. read(Q);
  44. while (Q--) {
  45. read(l); read(r);
  46. writeln(sum[r]-sum[l-]);
  47. }
  48.  
  49. return ;
  50.  
  51. }

【2017省中集训DAY1T1】 小X的质数的更多相关文章

  1. 使用Mac的过程中的一些小操作

    前言:使用Mac的过程中的一些小操作 查看Mac系统是32位还是64位: 方法1: 点击左上角的苹果按钮->关于本机->概览->系统报告->软件->偏好设置面板:右侧有提 ...

  2. 「中山纪中集训省选组D1T1」最大收益 贪心

    题目描述 给出\(N\)件单位时间任务,对于第\(i\)件任务,如果要完成该任务,需要占用\([S_i, T_i]\)间的某个时刻,且完成后会有\(V_i\)的收益.求最大收益. 澄清:一个时刻只能做 ...

  3. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  4. 数组中第K小的数字(Google面试题)

    http://ac.jobdu.com/problem.php?pid=1534 题目1534:数组中第K小的数字 时间限制:2 秒 内存限制:128 兆 特殊判题:否 提交:1120 解决:208 ...

  5. 九度OJ 1534 数组中第K小的数字 -- 二分查找

    题目地址:http://ac.jobdu.com/problem.php?pid=1534 题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[ ...

  6. 计算序列中第k小的数

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4046399.html 使用分治算法,首先选择随机选择轴值pivot,并使的序列中比pivot ...

  7. 九度OJ 题目1534:数组中第K小的数字(二分解)

    题目链接:点击打开链接 题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6 ...

  8. 笔试题&amp;面试题:找出一个数组中第m小的值并输出

    题目:找出一个数组中第m小的值并输出. 代码: #include <stdio.h> int findm_min(int a[], int n, int m) //n代表数组长度,m代表找 ...

  9. ACM 做题过程中的一些小技巧。

    ACM做题过程中的一些小技巧. 1.一般用C语言节约空间,要用C++库函数或STL时才用C++; cout.cin和printf.scanf最好不要混用. 2.有时候int型不够用,可以用long l ...

随机推荐

  1. codeforces Gym 101572 I 有向图最小环路径

    题目链接 http://codeforces.com/gym/101572 题意  一共n个文件  存在依赖关系 根据给出的依赖关系   判断是否存在循环依赖 ,不存在的话输出SHIP IT,存在的话 ...

  2. android 服务

    1.创建服务 Exported:是否允许除了当前程序之外的其他程序访问这个服务 Enable:是否启用这个服务 点击完成后自动生成 import android.app.Service; import ...

  3. Python使用eval强制转换字符串为字典时报错:File "<string>", line 1, in <module> NameError: name 'nan' is not defined

    文本中保存的内容为: { 'QQQ': [0.067, 0.167, 0.2, 0.033, 0.233, 0.267, 0.1, 0.133], 'TTT': [0.5, 0.375, 0.25, ...

  4. codevs——1501 二叉树最大宽度和高度

    1501 二叉树最大宽度和高度  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解       题目描述 Description 给出一个二叉树,输出它的 ...

  5. pyserial安装

    参考网址:pyserial.sourceforge.net/pyserial.html#installation Download the archive from http://pypi.pytho ...

  6. jfinal使用idea启动 访问报404 action not found

    公司一个项目,在eclipse里面启动正常,换到idea里面启动后,启动没有报错,但是访问的时候会提示404 action not found. 百度了很多种解决方法 都没有解决. 今天脑子一转,想到 ...

  7. oracle学习 第二章 限制性查询和数据的排序 ——03

    这里.我们接着上一小节2.6留下的问题:假设要查询的字符串中含有"_"或"%".又该如何处理呢? 開始今天的学习. 2.7  怎样使用转义(escape)操作符 ...

  8. java开始到熟悉60

    本次主题:多维数组 1,多维数组的初始话有三种:默认初始化.静态初始化.动态初始化. 这里只讲解静态初始化: 这里以二位数组为例,实际应用中,一维用得最多,二维次之,三维以及三维以上几乎很少使用,而且 ...

  9. 不是技术牛人,如何拿到国内IT巨头的Offer--转

    http://blog.csdn.net/lsldd/article/details/13506263 不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,N ...

  10. Intel Active Management Technology

    http://en.wikipedia.org/wiki/Intel_Active_Management_Technology Intel Active Management Technology F ...