1. /*
  2. * 二次筛素数
  3. * POJ268————Prime Distance(数论,素数筛)
  4. */
  5. #include<cstdio>
  6. #include<vector>
  7. using namespace std;
  8. const int maxn = 1000005;
  9. typedef long long LL;
  10. bool is_prime_small[maxn];
  11. bool is_prime[maxn];
  12. vector <int> res;
  13. int main() {
  14. LL l,u;
  15. while(scanf("%lld%lld",&l,&u) != EOF) {
  16. for(LL i = 0;i*i < u;i++) {
  17. is_prime_small[i] = true;
  18. }
  19. for(LL i = 0;i <= u-l;i++) {
  20. is_prime[i] = true;
  21. }
  22. is_prime_small[1] = false;
  23. if(l == 1)
  24. is_prime[0] = false;
  25. for(LL i = 2;i*i <= u;i++) {
  26. if(is_prime_small[i]) {
  27. for(LL j = 2*i;j*j <= u;j+=i) {
  28. is_prime_small[j] = false;
  29. }
  30. for(LL j = max(2LL,(l+i-1)/i) * i;j <= u;j+=i) {
  31. is_prime[j-l] = false;
  32. }
  33. }
  34. }
  35. res.clear();
  36. for(LL i = l;i <= u;i++) {
  37. if(is_prime[i - l]) {
  38. res.push_back(i);
  39. }
  40. }
  41. if(res.size() <= 1) {
  42. printf("There are no adjacent primes.\n");
  43. continue;
  44. }
  45. //避免这样写,可能会导致某些变量没有被赋值
  46. // LL maxx = res[1] - res[0],b1,b2;
  47. // LL minn = res[1] - res[0],a1,a2;
  48. LL b1 = res[0],b2 = res[1],maxx = b2 - b1;
  49. LL a1 = res[0],a2 = res[1],minn = a2 - a1;
  50. for(LL i = 2;i < res.size();i++) {
  51. if(maxx < res[i] - res[i-1]) {
  52. maxx = res[i] - res[i - 1];
  53. b2 = res[i];
  54. b1 = res[i-1];
  55. } else if(minn > res[i] - res[i-1]) {
  56. minn = res[i] - res[i-1];
  57. a2 = res[i];
  58. a1 = res[i-1];
  59. }
  60. }
  61. printf("%lld,%lld are closest, %lld,%lld are most distant.\n",a1,a2,b1,b2);
  62. }
  63. return 0;
  64. }

[POJ268] Prime Distance(素数筛)的更多相关文章

  1. Prime Path素数筛与BFS动态规划

    埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...

  2. ZOJ 1842 Prime Distance(素数筛选法2次使用)

    Prime Distance Time Limit: 2 Seconds      Memory Limit: 65536 KB The branch of mathematics called nu ...

  3. POJ - 2689 Prime Distance (区间筛)

    题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...

  4. POJ 2689 - Prime Distance - [埃筛]

    题目链接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branch o ...

  5. POJ2689 - Prime Distance(素数筛选)

    题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...

  6. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  7. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  8. POJ2689:Prime Distance(大数区间素数筛)

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  9. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

随机推荐

  1. python __all__

    它不仅在第一时间展现了模块的内容大纲,而且也更清晰的提供了外部访问接口. 若__all__的list中未定义,即便有实现也会找不到.

  2. RFID世界网

    RFID世界网 地址:http://www.rfidworld.com.cn/NFC/

  3. 与HTTP关系密切的三个协议:IP,TCP,DNS

    IP(网际协议): 位于网络层 通常易混淆的是“IP”和“IP地址”,单独讲“IP”是指一种协议名称 IP协议的作用是将各种数据包传送给对方.而要保证确实传送到对方那里,则需要满足各类条件. 其中两个 ...

  4. Asp.net 程序连接orcle如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,

    本人使用orcale11g 安装orcale 之类以及navicat配置在这里不提,之后会写一篇文章来说明. 到此已经安装和配置navicat访问数据正常,但是运行Asp.net  程序报错 问题如下 ...

  5. netframework webapi exceptionless

    1.webapi项目 添加nuget    exceptionless webapi 2.在exceptionless server端添加项目,注意key 3.修改api项目的webconfig &l ...

  6. CentOS 7 NAT模式LVS搭建

    NAT模式下 , 调度器需要有两个IP , 一个公网IP一个内网IP , web服务器只需要内网IP 调度器LB : 192.168.94.11(内网IP)  192.168.29.11 (仅主机模式 ...

  7. Tomcat增加Context配置不带项目名访问导致启动的时候项目加载两次

    eclipse发布web应用至tomcat,默认方式下访问该项目是需要带项目名称的,例http://localhost:8080/myapp/.现在需要改成这样访问http://localhost.修 ...

  8. LinQ各种方式查询、组合查询、IQueryable集合类型

    1.模糊查询(包含) Repeater1.DataSource = con.car.Where(r =>r.name.Contains(s)).ToList(); 2.开头查询 Repeater ...

  9. SpringMvc CharacterEncodingFilter 解析 encoding 参数并初始化参数

    SpringMvc CharacterEncodingFilter 解析 encoding 参数并初始化参数:

  10. fabric 更详尽的用法

    项目发布和运维的工作相当机械,频率还蛮高,导致时间浪费在敲大量重复的命令上. 修复bug什么的,测试,提交版本库(2分钟),ssh到测试环境pull部署(2分钟),rsync到线上机器A,B,C,D, ...