测试数学公式
题目翻译
【题目描述】
你决定用素数定理来做一个调查. 众所周知, 素数又被称为质数,其含义就是除了数字一和本身之外不能被其他任何的数字除尽.
现在给定一个正整数序列
找到并输出符合要求的最小值
【输入格式】
输入一行, 包含三个用空格隔开的整数
【输出格式】
输出一行, 为符合要求的最小值
题目描述
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
Find and print the required minimum -1
.
输入格式
A single line contains three space-separated integers
输出格式
In a single line print a single integer — the required minimum -1
.
样例 #1
样例输入 #1
2 4 2
样例输出 #1
3
样例 #2
样例输入 #2
6 13 1
样例输出 #2
4
样例 #3
样例输入 #3
1 4 3
样例输出 #3
-1
题目简化
求一个区间内,任意长度为
题目思路
初始化一个数组存储从 true
,表示对应的数是素数。
使用埃拉托斯特尼筛法(Sieve of Eratosthenes)来找出所有小于
在主循环中,读取三个输入:
接下来,进行一系列操作来找出在区间
如果
AC代码
#include <bits/stdc++.h>
using namespace std;
#define li long long int
#define rep(i,to) for(li i=0;i<((li)(to));++i)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bit(n) (1ll<<(li)(n))
#define all(vec) (vec).begin(),(vec).end()
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define MP make_pair
#define F first
#define S second
#define MAX 1000500
li is_prime[MAX];
int main()
{
rep(i, MAX)if(2 <= i) is_prime[i] = true;
for(li i = 2; i * i < MAX; i++){
if(!is_prime[i]) continue;
for(li j = i * i; j < MAX; j += i) is_prime[j] = false;
}
li a, b, k;
cin >> a >> b >> k;
queue<li> q;
li res = -1;
q.push(a - 1);
for(li pos = a; pos <= b; pos++){
if(is_prime[pos]) q.push(pos);
while(k < sz(q)) q.pop();
if(sz(q) == k) res = max(res, pos - q.front() + 1);
}
if(q.front() == a - 1) cout << -1 << endl;
else cout << res << endl;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
2018-10-22 上周热点回顾(10.15-10.21)
2012-10-22 找到问题的真正原因:20121021服务器故障处理经历
2012-10-22 上周热点回顾(10.15-10.21)
2009-10-22 博客园上海俱乐部Windows 7社区发布活动的奖品