zoukankan      html  css  js  c++  java
  • 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;

    }

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

    我要坚持一年,一年后的成功才是我想要的。
  • 相关阅读:
    easyui学习笔记1—增删改操作
    sql点滴37—mysql中的错误Data too long for column '' at row 1
    javascript获取当前url
    escape()、encodeURI()、encodeURIComponent()区别详解
    js文本框提示和自动完成
    javascript js string.Format()收集
    超链接标签为什么会造成页面颤抖
    asp.net mvc 4.0常见的几个问题
    如何使用Flashfxp上传下载文件
    点击按钮显示谷歌地图
  • 原文地址:https://www.cnblogs.com/tianxia2s/p/3871950.html
Copyright © 2011-2022 走看看