Prime Distance

STM32实战项目-状态机函数应用 对状态机应用的整理 阅读详情

题目描述:

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).

输入:

Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.

输出:

For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.

样例输入:

2 17
14 17

样例输出:

2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

题目大意:

给出一个区间[L,R], 范围为1<=L< R<=2147483647,区间长度长度不超过1000000

求距离最近和最远的两个素数(也就是相邻的差最小和最大的素数)

code:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;
const int maxn=1e6+10;
const long long maxx=9999999999;
long long prime[maxn];
long long ans[maxn];
int visit[maxn];
long long cnt=0;
void getprime()
{
	memset(prime,0,sizeof(prime));
	memset(visit,0,sizeof(visit));
	for(int i=2;i<maxn;i++)
	{
		if(visit[i]==0)
		{
			prime[cnt++]=i;
			for(int j=i+i;j<maxn;j+=i)
			{
				visit[j]=1;
			}
		}
	}
	visit[0]=visit[1]=1;
}//因为数据量太大,先筛较小的
void findd(long long l,long long r)
{
	memset(ans,0,sizeof(ans));
	for(long long i=0;i<cnt;i++)
	{
		long long b=l/prime[i];
		while(b*prime[i]<l||b<=1)b++;
		for(long long j=b*prime[i];j<=r;j+=prime[i])
		{
			if(j>=l)ans[j-l]=1;
		}
	}
}//筛该区间的大数字
long long temp[maxn];
int main()
{
	getprime();
	long long r,l;
	while(~scanf("%lld%lld",&l,&r))
	{
		if(l==1)l=2;
		long long num=0;
		findd(l,r);
		for(long long i=0;i<=r-l;i++)
		{
			if(ans[i]==0&&i+l<=r)
			{
				temp[num++]=i+l;
			}
		}
		long long big=0,xiao=maxx;
		long long sx,sy,sa,sb;
		for(long long i=1;i<num;i++)
		{
			long long cha=temp[i]-temp[i-1];
			if(cha>big)
			{
				big=cha;
				sx=temp[i-1];
				sy=temp[i];
			}
			if(cha<xiao)
			{
				xiao=cha;
				sa=temp[i-1];
				sb=temp[i];
			}
		}
		if(xiao==maxx)printf("There are no adjacent primes.\n");
		else printf("%lld,%lld are closest, %lld,%lld are most distant.\n",sa,sb,sx,sy);
	}
	return 0;
}

【STM32】状态机(State Machine) 本文介绍了状态机(State Machine)的概念及其在嵌入式开发等领域的应用。状态机是一种通过状态、事件、动作和转移来描述系统行为的抽象模型,适用于按键识别、通信协议、UI界面等场景。文章详细讲解了状态机的基本组成元素(状态、事件、动作、转移),分类(有限状态机、Mealy/Moore状态机等),并提供了按键识别的状态图示和C语言实现模板(含状态定义、事件处理、状态转移表等)。最后强调了状态机调试方法和在复杂流程管理中的优势,适合嵌入式开发者学习参考。 阅读详情

相关推荐

POJ2689 Prime Distance题解

POJ2689 Prime Distance题解题目题目描述英文题目中文题意输入输出格式输入格式输出格式输入输出样例输入样例输出样例题解 题目 题目描述 原题 英文题目 The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured t...

菜鸡Jacky0705的优快云博客 467

STM32状态机编程----什么是状态机?

万事万物都有其状态 什么是状态 状态是人或事物表现出来的形态。是指现实(或虚拟)事物处于生成、生存、发展、消亡时期或各转化临界点时的形态或事物态势。 通过上面那句话,我们知道了状态就是一个对象在不同情况下对应的各种形态 做产品的时候,如果我们如果要对这个对象所有的形态进行描述,在一些对象复杂的逻辑状态下,比较复杂的逻辑问题,普通的流程图,或时序图对于对象和状态的解读缺乏直观的描述。 这个时候就需要状态机来对对象的各个形态进行描述,将对象的全部工作方式,分成几个场景,这些场景的工作方式不同,然后将这些场.

Z小旋 2万+

#10197. 「一本通 6.2 例 1」Prime Distance(POJ 2689,质数,因子)

注意事项: 1、素数筛选时j的范围 2、注意i+1与i-1的选用,防止越界 3、1既不是素数也不是合数 #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include..

l1037746466的博客 434

poj 2689 Prime Distance 素数

题意:输入区间[l,u],其中l和u为int范围的整数,区间最大为1000000。求出[l,u]中,相邻素数只差最大和最小的素数对。当存在多个时,输出较小的素数对。 题解:l,u范围太大,不能直接求int范围的素数。而区间间隔比较小,只有1e6,而且对于int范围内的合数来说,最小质因子必定小于2^16。所以可以求出[l,u]中合数,转而求出素数,然后暴力枚举所有素数对即可。 如何求区间[l,

冷月之殇 5598

2018.09.01 poj2689 Prime Distance(埃式筛法)

传送门 一道挺有趣的。 第一眼以为每个数都用miller_rabin判一次,但感觉会被卡时间啊。 继续分析发现可以晒出sqrt(r)中的所有素数,然后用类似埃式筛法的方法晒出[l,r][l,r][l,r]之间的素数。 然后就没了。 代码: #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; ...

苟为蒟蒻又何妨 311

STM32单片机状态机设计与实现

本文还有配套的精品资源,点击获取 简介:STM32单片机是嵌入式系统的核心,状态机设计模式适用于此环境下以简化任务处理。通过C/C++语言实现,以事件驱动型状态机提高效率和响应性,涵盖状态定义、结构体、事件枚举、状态转移、初始化、中断处理等多个关键点。 1. STM32单片机概述 1.1 STM32单片机的简介 STM32单片机是由意法半导体(STMi...

weixin_35797963的博客 4508

Prime Distance(素数区间筛)

Prime Distance(素数区间筛) The branch of mathematics called number theory is about properties of numbers. One of the areas that has captu...

VUno)UKGzseI 141

/proc/sys/vm/max_map_count耗尽时,调用glibc 2.11.3 free()导致程序crash,问题追踪和解决

1. 问题 在客户环境下,当我们的程序消耗较大内存时,会偶尔crash。 通过gdb分析,程序core dump在malloc/free函数内。 当把/proc/sys/vm/max_map_count 从65530增加到131070,程序再也不crash。 相关命令 echo 131073 > /proc/sys/vm/max_map_count 2.  core

万大胖博客,苦并快乐着 4430

信息学奥赛一本通 1619:【例 1】Prime Distance

题目要求求出区间[L, R]中相邻质数的最大差值和最小差值。由于R-L≤10^6,采用筛法先预处理[1,50000]中的质数,再用这些质数筛去[L,R]中的合数。剩下的质数存入数组后遍历求相邻差值即可。关键点包括: 使用线性筛预处理小质数 通过区间偏移映射大数到数组 处理边界情况如L=1和质数数量不足的情况 使用数学公式计算质数倍数的范围以提高效率 最终通过遍历筛选后的质数数组,即可得到所需的最大最小相邻质数对。

君义NOIP的博客 1056

Cadence OrCAD常见引脚错误排查指南:从原理图到封装的完整解决方案

本文详细解析Cadence OrCAD设计中常见的引脚错误问题,提供从原理图到封装的系统化解决方案。涵盖错误诊断、元件库修复、封装管理及自动化处理技巧,帮助工程师高效解决ORCAP-36022等典型错误,确保设计流程顺畅。

weixin_29294975的博客 255

STM32H750xx【QSPI】轮询方式读写GD25Q64E

本文详细介绍了GD25Q64E其特性、引脚定义及功能说明,重点阐述了在STM32H750平台上通过CubeMX配置QuadSPI接口的步骤,包括时钟设置、参数优化等。文章提供了完整的BSP驱动开发流程,涵盖写使能、四线模式使能、ID读取、解除保护、扇区擦除、整片擦除、页编程和数据读取等关键功能实现,并通过读写测试验证了DummyCycles参数对数据准确性的影响。

honmy18的博客 1422

POJ2689 Prime Distance

POJ2689 Prime Distance 时间限制:1 Sec内存限制:512 MB提交:104解决:16[提交] [状态] [命题人:admin] 题目描述 The branch of mathematics called number theory is about properties of numbers. One ...

Albert_Liuzw的博客 191

智能车赛道识别:八邻域算法的实战解析与优化

本文深入解析了智能车赛道识别中八邻域算法的核心原理与实战优化。作为兼顾性能与稳定性的经典方案,该算法通过模拟探险家环顾八方的策略,能精准追踪弯道与直道边界。文章详细拆解了代码逻辑,并提供了边界平滑、异常处理及应对十字路口、环岛等复杂场景的关键优化策略,助力开发者提升智能车视觉系统的鲁棒性与实时性。

weixin_29228781的博客 261

【POJ 2689】Prime Distance(素数筛法)

题目链接 【POJ 2689】Prime Distance 题解 题目的关键在于如何求出lll到rrr之间所有的素数。 性质:如果一个数nnn是合数,则它一定有一个≤n‾√≤n\le \sqrt n的素因子。 思路:先筛出222到r√r\sqrt r的所有素数pipip_i,在一一标记lll到rrr中所有pipip_i的倍数。 时间复杂度 O(q∑素数p≤r√r−lp)O(q∑素...

Galaxy Coder 的博客 574

第33次CCF-CSP认证考试 第一题 词频统计 满分题解

本人小白一枚,抱着学习的态度想到网上找一些csp的参考答案,但由于实力有限,大佬的代码大都看不太懂,于是萌生了想用一些简单,基础的语句完成csp前两题的想法并公开,以便与像我这样实力不强的同学交流学习,如有更好的建议,还请斧正,感谢!

qq_75242106的博客 1869

拜耳阵列(Bayer Pattern)以及常见彩色滤波矩阵(CFA)

本文介绍了拜耳阵列的工作原理(Bayer Pattern)以及常用的彩色滤波矩阵(CFA)

风一样的男子(quinta)的博客 7120

poj 2689 Prime Distance 筛法+区间筛素数

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper fac

klc的博客 554

Flutter:如何在Android中实现串口通信调试

本文介绍如何通过flutter_libserialport插件在Flutter中实现串口通信调试。

sg_knight的专栏 6098

POJ-2689-Prime Distance

ACM模版题目链接POJ 2689 Prime Distance题解素数排查问题,问题的关键在于如何找出给定区间中的所有素数,涉及到一个预处理。代码#include <iostream>using namespace std;const int MAXN = 100010; int prime[MAXN + 1];// 预处理 void getPrime() { memset(prime,

逐梦者 939

利用大模型和聚类算法找出 Excel 文件中重复或相似度高的数据,并使用 FastAPI 进行封装的详细方案

tokenizer将输入的文本列表texts转换为 PyTorch 张量,同时进行填充和截断操作,以确保所有输入具有相同的长度。对输入进行推理,得到模型的输出。对模型的最后一层隐藏状态求平均,得到每个文本的向量表示。.numpy()将 PyTorch 张量转换为 NumPy 数组。使用DBSCAN算法对文本向量进行聚类。eps是邻域半径,是形成核心点所需的最小样本数。fit方法对输入的向量进行聚类,并返回每个样本所属的簇标签。try:定义了一个 POST 请求的 API 路由,路径为。

AI的内部世界的博客 1069

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

POJ 2689 Prime Distance (经典素数筛选)

转载请注明出处,谢谢 http://blog.youkuaiyun.com/ACM_cxlove?viewmode=contents           by---cxlove POJ 2689 Prime Distance  http://poj.org/problem?id=2689 区间范围很大,2^31左右,不可能筛选出所有素数,时间和空间都不允许。 但是可以发现询问的区间不是很大

窝不是爱酱,喵~~~~ 1666

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

POJ 2689 Prime Distance 素数筛选法应用

题目来源:POJ 2689 Prime Distance 题意:给出一个区间L R 区间内的距离最远和最近的2个素数 并且是相邻的 R-L 思路:一般素数筛选法是拿一个素数 然后它的2倍3倍4倍...都不是 然后这题可以直接从2的L/2倍开始它的L/2+1倍L/2+2倍...都不是素数 首先筛选出一些素数 然后在以这些素数为基础 在L-R上在筛一次因为 R-L #include #i

嗯。 980

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

GDUT寒假训练场4-Prime Distance

题目描述 The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no pro

千反田的博客 334
上一篇: Happy 2006
下一篇: Goldbach`s Conjecture
大橘为重eleven
博客等级 码龄8年 35粉丝 103原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值