Prime Distance
Description
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).
Input
Output
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
题意:给定一个区间,找去这个区间里相邻的最近的和最远的两组素数。
思路:根据题目中的数据范围,可以看出要用到埃式筛法,直接暴力肯定是不行的,然后直接无脑写就行了,好吧这就是我调了两天才AC的原因,我用的是挑战程序设计竞赛的模板。。。。。。阿西吧。
一直过不了,不废话了,具体看代码中的注释吧。
1 #include <map>
2 #include <set>
3 #include <list>
4 #include <stack>
5 #include <queue>
6 #include <deque>
7 #include <cmath>
8 #include <ctime>
9 #include <string>
10 #include <limits>
11 #include <cstdio>
12 #include <vector>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <cstring>
16 #include <istream>
17 #include <iostream>
18 #include <algorithm>
19 #define ci cin
20 #define co cout
21 #define el endl
22 #define Scc(c) scanf("%c",&c)
23 #define Scs(s) scanf("%s",s)
24 #define Sci(x) scanf("%d",&x)
25 #define Sci2(x, y) scanf("%d%d",&x,&y)
26 #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
27 #define Scl(x) scanf("%I64d",&x)
28 #define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
29 #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
30 #define Pri(x) printf("%d\n",x)
31 #define Prl(x) printf("%I64d\n",x)
32 #define Prc(c) printf("%c\n",c)
33 #define Prs(s) printf("%s\n",s)
34 #define For(i,x,y) for(int i=x;i<y;i++)
35 #define For_(i,x,y) for(int i=x;i<=y;i++)
36 #define FFor(i,x,y) for(int i=x;i>y;i--)
37 #define FFor_(i,x,y) for(int i=x;i>=y;i--)
38 #define Mem(f, x) memset(f,x,sizeof(f))
39 #define LL long long
40 #define ULL unsigned long long
41 #define MAXSIZE 1000005
42 #define INF 0x3f3f3f3f
43
44 const int mod=1e9+7;
45 const double PI = acos(-1.0);
46
47 using namespace std;
48
49 bool is_prime[MAXSIZE];
50 bool is_prime_small[MAXSIZE];
51 //is_prime[i-a]=true--->i是素数
52 void solve(LL a,LL b)
53 {
54 for(int i=0; (LL)i*i<=b; i++)
55 is_prime_small[i]=true;
56 //is_prime_small[0]=is_prime_small[1]=false;
57 for(int i=0; i<=b-a; i++)
58 is_prime[i]=true;
59 if(a==1)
60 is_prime[0]=0;//就是这个特判,我调了一天才搞出来 。。。。。。
61 for(int i=2; (LL)i*i<=b; i++)
62 if(is_prime_small[i])
63 {
64 for(int j=2*i; (LL)j*j<=b; j+=i)
65 is_prime_small[j]=false;//筛2~根号b
66 for(LL j=max(2LL,(a+i-1)/i)*i; j<=b; j+=i)
67 is_prime[j-a]=false;//筛a~b
68 }
69 }
70 //j = (ll)(a-1+i)/i*i
71 //(a-1+i)/i*i是对a/i向上取整,此计算的作用是求得第一个>=a的i的倍数。
72 //for(LL j=max(2LL,(a+i-1)/i)*i; j<=b; j+=i)这个循环的初始条件还是不懂
73
74 int main()
75 {
76 LL a,b;
77 LL c1,c2,d1,d2;
78 while(~Scl2(a,b))
79 {
80 solve(a,b);
81 int tmp=-1;
82 int minn=INF,maxx=-1;
83 queue<int>q1,q2;
84 For_(i,a,b)
85 {
86 if(is_prime[i-a])
87 {
88 if(tmp!=-1)
89 {
90 if(i-tmp<minn)
91 {
92 minn=i-tmp;
93 c1=tmp;
94 c2=i;
95 }
96 if(i-tmp>maxx)
97 {
98 maxx=i-tmp;
99 d1=tmp;
100 d2=i;
101 }
102 }
103 tmp=i;
104 }
105 }
106 if(minn!=INF||maxx!=-1)
107 cout<<c1<<","<<c2<<" are closest, "<<d1<<","<<d2<<" are most distant."<<endl;
108 else
109 Prs("There are no adjacent primes.");
110 }
111 return 0;
112 }
Prime Distance的更多相关文章
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
		Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ... 
- UVA 10140 - Prime Distance(数论)
		10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ... 
- poj 2689 Prime Distance(大区间素数)
		题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ... 
- [POJ268] Prime Distance(素数筛)
		/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ... 
- 一本通1619【例 1】Prime Distance
		1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ... 
- POJ2689 Prime Distance(数论:素数筛选模板)
		题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ... 
- POJ-2689  Prime Distance (两重筛素数,区间平移)
		Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ... 
- UVA10140 Prime Distance
		UVA10140 Prime Distance 给定两个整数L,R(1<=L<=R<=2^{31},R-L<=10^6)L,R(1<=L<=R<=231,R− ... 
- ZOJ 1842	Prime Distance(素数筛选法2次使用)
		Prime Distance Time Limit: 2 Seconds Memory Limit: 65536 KB The branch of mathematics called nu ... 
- 解题报告:poj2689 Prime Distance
		2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ... 
随机推荐
- 第一章:seaborn图形美学
			一.seaborn模板 1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 def sinplot(flip=1): 5 x = np ... 
- day30-JQuery03
			JQuery03 4.jQuery选择器03 4.4表单选择器 应用实例 <!DOCTYPE html> <html lang="en"> <head ... 
- 【PostgreSQL/PGSQL】创建分区表与临时表
			一.分区表 1.链接 https://blog.csdn.net/zhangyupeng0528/article/details/119423234 2.分类 列(值)分区表:partition by ... 
- 一个小时,200行代码,手写Spring的IOC、DI、MVC
			一.概述 配置阶段:主要是完成application.xml配置和Annotation配置. 初始化阶段:主要是加载并解析配置信息,然后,初始化IOC容器,完成容器的DI操作,已经完成HandlerM ... 
- mac连接mysql出现Access denied for user ‘root‘@‘localhost‘
			处理方法:1.关闭mysql的服务,点击最左上的苹果图标在系统偏好设置中,找到mysql,点击,stop 确认关闭后进入终端 输入(cd /usr/local/mysql/bin/)回车 输入(sud ... 
- STM32按键控制LED亮灭的代码
			led.c #include "led.h" void LED_Config(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2P ... 
- JavaScript:操作符:算术运算符(加减乘除模幂)及其隐式转换数据类型
			加法+ 减法- 乘法* 除法/ 模运算% 幂运算**,即a ** b求的是a的b次方 执行上述运算时,当两个操作数有非数字时,JS会隐式转换为数字,再进行运算: 一些特殊的非数字,会进行如下转换: t ... 
- [OpenCV实战]4 OpenCV中的颜色空间
			目录 1 不同的色彩空间 1.1RGB颜色空间 1.2 Lab色彩空间 1.3 YCrCb颜色空间 1.4 HSV颜色空间 2 如何使用这些颜色空间进行分割 2.1 获取特定颜色的颜色值 2.2 应 ... 
- Python Excel 追加数据
			xlutils 库的安装 你好,我是悦创. 前面我分享了 Excel 的读写:Python 实现 Excel 的读写操作:https://bornforthis.cn/column/pyauto/au ... 
- 深入探究Java中的对象类型变量声明操作——在声明对象时,系统究竟做了什么?
			深入探究Java中的对象类型变量声明操作--在声明对象时,系统究竟做了什么? 摘要:本文主要对Java中的对象类型变量的声明的底层原理做了探究. 目录 深入探究Java中的对象类型变量声明操作--在声 ... 
