UVA 10140 - Prime Distance(数论)

10140 - Prime Distance

题目链接

题意:求[l,r]区间内最近和最远的素数对。

思路:素数打表,打到sqrt(Max)即可,然后利用大的表去筛素数,由于[l, r]最多100W,所以可以去遍历一遍,找出答案。注意1的情况,一开始没判断1,结果WA了

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define INF 0x3f3f3f3f3f
const int N = 100005;
long long l, r, vis[N * 10], prime[N], pn = 0;

int main() {
	for (long long i = 2; i < N; i++) {
		if (vis[i]) continue;
		prime[pn++] = i;
		for (long long j = i; j < N; j += i)
			vis[j] = 1;
 	}
 	while (~scanf("%lld%lld", &l, &r)) {
 		memset(vis, 0, sizeof(vis));
 		for (long long i = 0; i < pn; i++) {
  			for (long long j = (l / prime[i] + (l % prime[i] != 0)) * prime[i]; j <= r; j += prime[i]) {
  				if (j / prime[i] != 1)
  					vis[j - l] = 1;
     		}
		}
		long long pre = -1;
		long long Max = 0, Min = INF;
		long long maxl, maxr, minl, minr, flag = 1;
		for (long long i = l; i <= r; i++) {
			if (vis[i - l] || i == 1) continue;
			if (pre != -1) {
				if (i - pre > Max) {
			 		Max = i - pre;
			 		maxl = pre; maxr = i;
    			}
    			if (i - pre < Min) {
    				Min = i - pre;
    				minl = pre; minr = i;
       			}
				flag = 0;
   			}
   			pre = i;
  		}
  		if (flag) printf("There are no adjacent primes.\n");
  		else printf("%lld,%lld are closest, %lld,%lld are most distant.\n", minl, minr, maxl, maxr);
  	}
	return 0;
}

UVA 10140 - Prime Distance(数论),布布扣,bubuko.com

时间: 2024-10-13 01:02:01

UVA 10140 - Prime Distance(数论)的相关文章

uva 1404 - Prime k-tuple(数论)

题目链接:uva 1404 1404 - Prime k-tuple 题目大意:如果k个相邻的素数p1,p2,-,pk,满足pk?p1=s,称这些素数组成一个距离为s的素数k元组,给定区间a,b,求有多少个距离s的k元组. 解题思路:筛选素数法,先预处理出[1, sqrt(inf)]的素数表,然后对给定区间[a,b]根据预处理出的素数表筛选出素数即可. #include <cstdio> #include <cstring> #include <cmath> #incl

uva 10539 - Almost Prime Numbers(数论)

题目链接:uva 10539 - Almost Prime Numbers 题目大意:给出范围low~high,问说在这个范围内有多少个数满足n=pb,(p为素数). 解题思路:首先处理出1e6以内的素数,然后对于每个范围,用solve(high)?solve(low?1),solve(n)用来处理小于n的满足要求的数的个数.枚举素数,判断即可. #include <cstdio> #include <cstring> typedef long long ll; const int

poj 2689 Prime Distance 【数论】【筛法求素数】

题目链接:传送门 题目大意: 给你L和R两组数,L和R的范围是2^32,其间隔(即R-L最大为1,000,000.) .让你求出L和R之间素数的最大间隔和最小的间隔. 比如 2 17.之间的最小素数间隔是2 3,最大的素数间隔是11 17. 要是直接进行一个2^32次方筛法然后在判断是会T的. 我们这样来想,筛法求素数的原理是什么: /**vis数组标记为0则说明是素数*/ int vis[10005]; void getPrimevis(int n) { int m=sqrt(n+0.5);

数论 - 素数的运用 --- poj 2689 : Prime Distance

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

POJ2689 Prime Distance(数论:素数筛选)

题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24073 Accepted: 6306 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of

uva 11105 - Semi-prime H-numbers(数论)

题目链接:uva 11105 - Semi-prime H-numbers 题目大意:H-number为4?k+1(k为非负数),H-composites为因子中含有H-number(不包括自己本身)的数,反之久是H-prime,给定n,求有多少H-composites. 解题思路:首先用筛选法求出范围内的H-prime,然后枚举两个判断乘积是否在范围内. #include <cstdio> #include <cstring> const int maxn = 1e6+5; ty

UVA 10139 Factovisors(数论)

Factovisors The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n > 0) We say that a divides b if there exists an integer k such that k*a = b The input to your program consists of several lines, each conta

POJ-2689 Prime Distance (两重筛素数,区间平移)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the