码迷,mamicode.com
首页 > 其他好文 > 详细

Primes on Interval

时间:2014-07-27 23:32:49      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:style   os   io   for   代码   new   amp   ios   

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\n");

            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\n" , ans);

    }

    return 0;

}

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

Primes on Interval,布布扣,bubuko.com

Primes on Interval

标签:style   os   io   for   代码   new   amp   ios   

原文地址:http://www.cnblogs.com/tianxia2s/p/3871950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!