当前位置:网站首页>均是素数——天梯训练赛
均是素数——天梯训练赛
2022-04-21 14:23:00 【MITBlick】
L1-8 均是素数 (20 分)
在给定的区间 [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
解题方法:如果这题采用线性筛对每个数进行逐一判断,就会出现超时现象,因此这题采用更高级一点的方法 欧拉筛。
Code:
#include <iostream>
#include <stdio.h>
using namespace std;
const int N = 1000010;
int n, m, p, q, r, ans;
bool f[N];
void prime() //欧拉筛主体部分
{
for(int i = 2; i <= 1000; i ++ )
{
if(!f[i])
{
for(int j = 2; i * j <= 1000010; j ++ )
{
if(!f[i * j]) f[i * j] = true;
}
}
}
}
signed main()
{
f[1] = true;
prime();
cin >> n >> m;
for(int i = n; i <= m; i ++ )
{
for(int j = i + 1; j <= m; j ++ )
{
for(int k = j + 1; k <= m; k ++ )
{
if(!f[i] && !f[j] && !f[k] && !f[i * j + k] && !f[i * k + j] && !f[j * k + i]) ans ++;
}
}
}
cout << ans << endl;
}
版权声明
本文为[MITBlick]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_62343171/article/details/124271456
边栏推荐
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
- ROS2学习笔记(六)-- 自定义消息和服务实现控制指令优化以及在线换图
- 无人驾驶虚拟仿真(十三)--图像处理之交通标志牌识别1
- Three methods for beginners to exchange the values of two variables
- SQL Server 批处理变量定义和赋值
- 数据仓库架构演变和建设思路
- Using library function qsort () to sort, implementation and principle analysis
- 堆排序--TOP-K问题解决及复杂度分析
- Implementation of broadcast and monitoring at source level
- Sequential list -- linked list implementation
猜你喜欢

【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )

暴力破解美团最新JVM面试题:无限执行

腾讯二面:MySQL的半同步是什么?

Heap sorting -- Top-k problem solving and complexity analysis

Ros2 learning notes (8) -- road recognition and debugging based on the application of ros2 parameters

SQL Server 批处理变量定义和赋值

软件测试入职2个月想辞职了

Script operation es

翻译《Mastering ABP Framework》

程序员想在深圳扎根,除了去腾讯,还可以考虑一下这些公司
随机推荐
Redisjson: a redis that can store JSON
Personal summary of three simple sorting for beginners
从技术原理、主流平台、市场展望快速入门NFT
虫子 归并 计数
ROS2学习笔记(十一)-- ROS2 bag数据记录与回放
Ros2 learning notes (IX) -- Summary of common instructions for ros2 command line operation (II)
Worm ring list
虫子 文件操作
PCL测试程序出现LNK2001-无法解析的外部符号
Go's relevant operation and arrangement of documents (continuous update)
A lifetime of needs, team collaboration can play this way on cloud nailing applet
ROS2学习笔记(三)-- 采集虚拟仿真环境图像并发布
53.最大子数组和
Insect merging count
代码重构之内联函数
ROS2学习笔记(六)-- 自定义消息和服务实现控制指令优化以及在线换图
Several implementation methods and optimization of quick sorting
Dynatrace抓取系统中的任何方法Method的参数值
WordPress博客搭建指南
文本处理——sed