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\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的更多相关文章
- Primes on Interval(二分 + 素数打表)
Primes on Interval Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- HDU 5901 Count primes 论文题
Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...
- hdu 5901 Count primes (meisell-Lehmer)
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- Codeforces Round #147 (Div. 2)
A. Free Cash 判断值相同的最长长度. B. Young Table 按从上到下,从左到右排序,每个位置最多交换一次. C. Primes on Interval \(p_i\)表示位置\( ...
- 130712周赛(CF)
这次练习从第一题开始注定水了,1A的题目wa了3次,第三题走进了错误的思想,wa到死....其他三个题目看都没看...........赛后慢慢搞. A. Free Cash 巨水的一题,直接找出每个时 ...
- codeforce --- 237C
C. Primes on Interval time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 13年7月13日CF练习 Codeforces Round #147 (Div. 2)
这场div2可以说是我见过的比较水的一场吧.基本都是一眼题. 比赛地址http://acm.bnu.edu.cn/bnuoj/contest_show.php?cid=1836 题号是237A-237 ...
- Meissel Lehmer Algorithm 求前n个数中素数个数 【模板】
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找
C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- 关于linux修改max user processes limits的问题
我们都知道,ulimit -u 可以设置max user processes limits,但是往往在设置的过程中,这样直接修改,不仅只能临时生效,重启之后又无效了,而且老是会失败. 而一般来说,修改 ...
- cocos2d-3.x 创建动画
1.多文件帧序列动画 TrademarkAnimation.h #ifndef __TRADEMARK_ANIMATION_H__ #define __TRADEMARK_ANIMATION_H__ ...
- hbase checkandput
HBaseEveryDay_Atomic_compare_and_set Updated Sep 29, 2011 by lurker.h...@gmail.com 基本概念 在HBase中Inser ...
- 查看源码利器之sublime text 3 配置 Ctags 插件
最近在看源码的时候发现sublime text 3是很给力的一款软件,小巧精致,这里着重讲解一下Ctags协助编译和跟踪函数 一.安装Package Control (如果Preferences &g ...
- UVa 10305 Ordering Tasks (例题 6-15)
传送门: https://uva.onlinejudge.org/external/103/10305.pdf 拓扑排序(topological sort)简单题 自己代码的思路来自: ==> ...
- JS复习:第七章
第七章 函数表达式 一.定义函数的方式有两种:函数声明和函数表达式. 1.函数声明: function functionName(arg0 , arg1 , arg2){ //函数体... } 函数 ...
- 安装asterisk
在centos6.5上: yum -y install lynx mysql-server mysql mysql-devel php php-mysql php-mbstring tftp-serv ...
- Excel教程(8) - 财务函数
ACCRINT 用途:返回定期付息有价证券的应计利息. 语法:ACCRINT(issue,first_interest, settlement, rate,par,frequency, basis) ...
- [ An Ac a Day ^_^ ] CodeForces 468A 24 Game 构造
题意是让你用1到n的数构造24 看完题解感觉被样例骗了…… 很明显 n<4肯定不行 然后构造出来4 5的组成24的式子 把大于4(偶数)或者5(奇数)的数构造成i-(i-1)=1 之后就是无尽的 ...
- 第二次冲刺spring会议(第一次会议)
[例会时间]2014/5/4 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇 内部测试版发布时间5月11日 ...