当前位置:网站首页>2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
2022-04-23 03:22:00 【再敲一行就去睡】
在给定的区间 [m,n] 内,是否存在素数 p、q、r(p<q<r),使得 pq+r、qr+p、rp+q 均是素数?
输入格式:
输入给出区间的两个端点 0<m<n≤1000,其间以空格分隔。
输出格式:
在一行中输出满足条件的素数三元组的个数。
输入样例:
1 35
输出样例:
10
样例解读
满足条件的 10 组解为:
2, 3, 5
2, 3, 7
2, 3, 13
2, 3, 17
2, 5, 7
2, 5, 13
2, 5, 19
2, 5, 31
2, 7, 23
2, 13, 17
好题!模拟赛的时候没注意到关键剪枝,但是也过了。详细说一下思路
首先1000以内最大的两个素数是991和997,差不多是10^6的量级,因此,需要对素数提前打表。
为了提高查询速度,再额外附加一个数组作为快表使用。笔者使用了质数筛+平方根判别。
再之后就是遍历了,此处有一个关键性的剪枝:如果是三个奇数参与题中所给的三元操作,其结果为偶数,必不为素数。由于2是唯一的素数,因此,先判别范围是否包含2,在存在2的前提下,进行二重循环就可以了。
#include <bits/stdc++.h>
using namespace std;
int v[1000000]={0}; //素数快表
int main(void){
int m,n,sum=0;
vector<int> p; //素数表
vector<int> d; //检查序列
p.push_back(2);
cin>>m>>n;
for(int i=3;i<1000000;i+=2){
int flg=1;
for(int j=0;j<p.size();j++){
if(i<p[j]*p[j])break;
if(!(i%p[j])){
flg=0;
break;
}
}
if(flg){
v[i]=1;
p.push_back(i);
}
}
for(int i=0;i<p.size();i++){
if(p[i]>n)break;
if(p[i]>=m)d.push_back(p[i]);
}
if(d[0]==2) //剪枝
for(int i=1;i<d.size()-1;i++){
for(int j=i+1;j<d.size();j++){
int a=d[i],b=d[j];
if(v[a*b+2]&&v[a*2+b]&&v[b*2+a])sum++;
}
}
cout<<sum;
return 0;
}
版权声明
本文为[再敲一行就去睡]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46212625/article/details/124348671
边栏推荐
- JSON related
- QT dynamic translation of Chinese and English languages
- 2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
- Top 9 task management system in 2022
- 为什么BI对企业这么重要?
- 一文了解全面静态代码分析
- Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
- 2022a special equipment related management (elevator) work license question bank and simulation examination
- Xamarin effect Chapter 22 recording effect
- Mysql database design specification
猜你喜欢
It can receive multiple data type parameters - variable parameters
When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
QT learning summary
IDEA查看历史记录【文件历史和项目历史】
New ORM framework -- Introduction to beetlsql
Visual programming - Experiment 1
Iotos IOT middle platform is connected to the access control system of isecure center
Build websocket server in. Net5 webapi
关于idea调试模式下启动特别慢的优化
Supersocket is Use in net5 - startup
随机推荐
Optimization of especially slow startup in idea debugging mode
你真的懂hashCode和equals吗???
. net 5 Web custom middleware implementation returns the default picture
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
Supersocket is Use in net5 - concept
JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
"Visual programming" test paper
幂等性实践操作,基于业务讲解幂等性
Query stored procedures in PostgreSQL
可以接收多种数据类型参数——可变参数
A comprehensive understanding of static code analysis
MySQL installation pit
二进制文件版本控制工具选择难?看完这篇你会找到答案
Problem C: Hanoi Tower III
批量下载文件----压缩后再下载
Use of ADB command [1]
全新的ORM框架——BeetlSQL介绍
MySQL keyword group_ Concat, combined connection query
Experiment 5 components and event handling