• Primes on Interval


    AC代码:

    #include <cstdio>

    #include <cstring>

    #include <iostream>

    #include <algorithm>

    using namespace std;

    const int maxn = 1001000;

    #define  inf (1<<29)

    //上面的位运算还真心没有看懂

    // p[i] is i-th prime's position

    bool pp[maxn]; //里面保存的是一个素数的信息

    int p[maxn] , cnt = 0; //将素数保留在数组中间

    int ss[maxn] , tt[maxn];//在这里申请了这么多的数组我就是没有看懂了是到底为啥

    void init() {

        cnt = 0;

        pp[0] = pp[1] = 1;//前两个数字都是不予考虑的

        tt[0] = tt[1] = -1;

        for(int i=2;i<maxn;i++) {

            if(!pp[i]) {

                p[cnt++] = i; //这个是将素数保留在表格中间吗?

                for(int j=i+i;j<maxn;j+=i) {

                    pp[j] = true; //这个是素数达标

                }

            }

            tt[i] = cnt - 1;

        }

        for(int i=0;i<maxn;i++) {

            if(!pp[i]) ss[i] = tt[i];

            else ss[i] = tt[i] + 1;

        }

    }

    int main() {

        init();

        int a , b , k;

        while(~scanf("%d%d%d" , &a,&b,&k)) {

            int s = ss[a] , t = tt[b];

            int num = t - s + 1;

         

            if(num < k) {//先判断在这个区间之中里面的素数量是否达到了题目的要求,否则直//接退出

                printf("-1 ");

                continue;

            }

            int ans = 0;

            int tmp;

            tmp = b - p[t-k+1] + 1;

            if(tmp > ans) ans = tmp;

            tmp = p[s+k-1] - a + 1;

            if(tmp > ans) ans = tmp;

            for(int i=s;i+k<=t;i++) {

                tmp = p[i+k] - p[i];

                if(tmp > ans) ans = tmp;

            }

            printf("%d " , ans);

        }

        return 0;

    }

    //本题的主要思路是通过打表,成功后就可以比较简单的得到结果

    我要坚持一年,一年后的成功才是我想要的。
  • 相关阅读:
    WEB前端第四十四课——jQuery框架(二)常用方法
    WEB前端第四十三课——jQuery框架(一)$()函数、添加事件监听
    WEB前端第四十二课——应用案例-瀑布流、递归、代理
    WEB前端第四十一课——简单算法与解析indexof、hash、冒泡排序、递归、JSON
    WEB前端第四十课——正则表达式-RegExp、高级
    WEB前端第三十九课——HTML正则表达式-基础、修饰符、检索模式
    SITE STRUCTURE
    CSS GRID ESSENTIALS
    CSS TYPOGRAPHY
    CSS COLOR
  • 原文地址:https://www.cnblogs.com/tianxia2s/p/3871950.html
Copyright © 2020-2023  润新知