『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找
1 second
256 megabytes
standard input
standard output
You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors.
Consider positive integers a, a + 1, ..., b (a ≤ b).
You want to find the minimum integer l (1 ≤ l ≤ b - a + 1) such
that for any integer x(a ≤ x ≤ b - l + 1) among l integers x, x + 1, ..., x + l - 1 there
are at least k prime numbers.
Find and print the required minimum l. If no value l meets
the described limitations, print -1.
A single line contains three space-separated integers a, b, k (1 ≤ a, b, k ≤ 106; a ≤ b).
In a single line print a single integer — the required minimum l. If there's no solution, print -1.
2 4 2
3
6 13 1
4
1 4 3
-1
题目意思看了老半天,冷静分析终于摸清了题意,就是求a,b范围内任意一段包含k个素数的最小长度L;用常规方法的话也可以,但是可能会很麻烦;这里可以用一种二分查找,但都是要打表的;
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1000000+10;
int a,b,k,s[N],ss[N];
void dabiao()
{
memset(s,0,sizeof(s));
memset(ss,-1,sizeof(ss));
ss[0]=ss[1]=0;
for(int i=2; i<=N; i++)
{
s[i]=s[i-1];
if(ss[i])
s[i]+=1;//打表;
if(ss[i])
{
if(i>N/i) continue;
for(int j=i*i; j<=N; j+=i)
ss[j]=0;//筛法;
}
}
}
int cha(int x)
{
for(int i=a; i<=b-x+1; i++)
if(s[i+x-1]-s[i-1]<k)
return 0;
return 1;
}
int main()
{
dabiao();
while(~scanf("%d%d%d",&a,&b,&k))
{
if(s[b]-s[a-1]<k)
printf("-1\n");整个区间内的素数个数;
else
{
int x=0;
int l=1,r=b-a+1;
while(l<=r)
{
int mid=(l+r)/2;
if(cha(mid))
{
x=mid;
r=mid-1;
}
else
l=mid+1;
}
printf("%d\n",x);
}
}
return 0;
}
其实理清了思路就不会很难;由此可见思路的重要性;
『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找的更多相关文章
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便
A. Boy or Girl time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]CF-236B. Easy Number Challenge
B. Easy Number Challenge time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]-最小内积(第八届北京师范大学程序设计竞赛决赛)
H. 最小内积 Time Limit: 1000ms Memory ...
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]- Nearly Lucky Number(Codeforces Beta Round #84 (Div. 2 Only)A. Nearly)
A. Nearly Lucky Number time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]--Codeforces -35D. Animals
D. Animals time limit per test 2 seconds memory limit per test 64 megabytes input input.txt output o ...
- K Simple question (第十届山东理工大学ACM网络编程擂台赛 正式赛)
题解:素数筛+唯一分解定理 可以把素数筛那部分放到while之外,减小时间复杂度. #include <stdio.h> #include <stdlib.h> #includ ...
- F 阎小罗的Minimax (第十届山东理工大学ACM网络编程擂台赛 正式赛 )
题解:by Mercury_Lc 阎小罗的矩阵给的n和m都不超过300,枚举一下所有情况就可以了,用前缀和来储存.数组a[x][y]代表前x行前y列的和是多少,那么枚举每一种切割的方式就可以.注意一下 ...
- 第八届山东省ACM大学生程序设计竞赛个人总结
因为省赛,从开学紧张到5月7号.心思也几乎全放在ACM的训练上.因为我还是校台球协会的会长,所以台协还有一些事情需要忙,但是我都给延迟了.老会长一直在催我办校赛,但我一直说 等等吧,因为校赛只能在周六 ...
- 『计算机视觉』Mask-RCNN_训练网络其三:训练Model
Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...
随机推荐
- Steps to Resolve the Database JAVAVM Component if it Becomes INVALID After Applying an OJVM Patch
11.2.0.2升级到11.2.0.4 memory_target 设置过小,只有800M. 导致jserver jave virtual machine 组件无法安装, 建议升级之前至少memory ...
- Plugging an Unplugged Pluggable Database issue 2
因为原库和目标库版本不一制,出现各种问题,强烈建议保持2个版本一致 http://www.cndba.cn/dave/article/220 Log 提示查看PDB_PLUG_IN_VIOLATION ...
- ref 和 React.js 中的 DOM 操作
在 React.js 当中你基本不需要和 DOM 直接打交道.React.js 提供了一系列的 on*方法帮助我们进行事件监听,所以 React.js 当中不需要直接调用 addEventListen ...
- 【学习笔记】深入理解js原型和闭包(11)——执行上下文栈
继续上文的内容. 执行全局代码时,会产生一个执行上下文环境,每次调用函数都又会产生执行上下文环境.当函数调用完成时,这个上下文环境以及其中的数据都会被消除,再重新回到全局上下文环境.处于活动状态的执行 ...
- Web前端开发学习误区,你掉进去了没?
从接触网站开发以来到现在,已经有五个年头了吧,今天偶然整理电脑资料看到当时为参加系里面一个比赛而做的第一个网站时,勾起了在这网站开发道路上的一串串回忆,成功与喜悦.烦恼与纠结都历历在目,感慨颇多. 先 ...
- About the iOS File System
两个维度: 1)是否给用户使用: 2)是否持久存储. During installation of a new app, the installer creates a number of conta ...
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)
A(By talker): 题意分析:以a(int) op b(int)形式给出两个整数和操作符, 求两个整数是否存在操作符所给定的关系 ,有则输出true,无则输出false: 思路:由于无时间复杂 ...
- Python100天打卡-Day10-图形用户界面和游戏开发
基于tkinter模块的GUIPython默认的GUI开发模块是tkinter(在Python 3以前的版本中名为Tkinter)使用tkinter来开发GUI应用需要以下5个步骤: 导入tkinte ...
- 什么是python 中的顶层代码?
在python语言中我们经常会听到顶层代码的说法,但是什么是顶层代码? 在python中,我们是以缩进来区分代码层次的,所以顶层代码指的是缩进为0个空格的代码. 看如下例子: PP = 3.14 de ...
- dns2tcp使用教程
在2010年6月的更新(也是迄今为止最新的更新)后,其源代码支持编译为Windows平台的可执行程序.而且此工具使用C语言开发编写,不需要TUN/TAP,所以大大加强了它的可用性. 下载 当前最新的0 ...