UVA - 1404 Prime k-tuple (素数筛选)

Description

{p1,..., pk : p1 <
p2 <...< pk} is called a prime
k -tuple of distance s if
p1, p2,..., pk are consecutive prime numbers and
pk - p1 = s . For example, with
k = 4 , s = 8 ,
{11, 13, 17, 19} is a prime 4-tuple of distance 8.

Given an interval [a, b] ,
k , and s , your task is to write a program to find the number of prime
k -tuples of distance s in the interval
[a, b] .

Input

The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

For each data set, there is only one line containing 4 numbers,
a , b , k and
s(a, b < 2
* 109, k < 10, s < 40) .

Output

For each test case, write in one line the numbers of prime
k -tuples of distance s .

Sample Input

1
100 200 4 8

Sample Output

2

题意:如果有k个相邻的素数,满足pk-p1=s,称这些素数组成一个距离为s的素数k元组,输出区间[a, b]内距离为s的k元组
思路:首先筛选出素数,然后在区间[a,b]内查找满足的个数,还有我决定了以后刷选用long long 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 100005;

int prime[maxn], vis[maxn], cnt;

void init() {
	memset(vis, 0, sizeof(0));
	vis[1] = vis[0] = 1;
	cnt = 0;
	for (ll i = 2; i < maxn; i++)
		if (!vis[i]) {
			prime[cnt++] = i;
			for (ll j = i*i; j < maxn; j += i)
				vis[j] = 1;
		}
}

int check(int n) {
	if (n < maxn)
		return vis[n] == 0;
	for (int i = 0; i < cnt && prime[i]*prime[i] <= n; i++)
		if (n % prime[i] == 0)
			return 0;
	return 1;
}

int main() {
	init();
	int t, a, b, k, s;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d%d%d", &a, &b, &k, &s);
		int tmp = 0, ans = 0;
		vector<int> num;
		for (int i = a; i <= b; i++)
			if (check(i))
				num.push_back(i);
		int size = num.size();
		for (int i = 0; i+k-1 < size; i++)
			if (num[i+k-1] - num[i] == s)
				ans++;
		printf("%d\n", ans);

	}
	return 0;
}

时间: 2024-10-25 00:16:46

UVA - 1404 Prime k-tuple (素数筛选)的相关文章

UVA 1404 - Prime k-tuple(素树筛选)

UVA 1404 - Prime k-tuple 题目链接 题意:找出a-b之间有多少个素数k元组,并且最后一个元素减第一个元素为s 思路:先筛出sqrt的素数,然后对于每个区间,在用这些素数去筛出区间的素数,然后twopointer搞一下即可 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <vector> usi

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

Light OJ 1356 Prime Independence 最大独立集+素数筛选

题目来源:Light OJ 1356 Prime Independence 题意:给你n个数 选出最多的数构成一个集合使得任何2个数不是另外一个数的质数倍 x!=k*y 思路:矛盾的2个数连边 并且所有数分成质因子数为奇数和偶数两部分 以质因子奇偶不同构建二分图 同奇或者同偶的数一定不是另外一个数的质数倍 判断矛盾 首先对每个数因子分解 例如x 有a1个p1质因子 a2个p2质因子...an个pn质因子 x的质因子个数为a1+a2+...+an 记为sum 判断是否存在x/p1  x/p2 ..

POJ 2689 Prime Distance(素数筛选)

题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,147,483,647) R - L <= 1000000 思路:数据量太大不能直接筛选,要采用两次素数筛选来解决.我们先筛选出2 - 50000内的所有素数,对于上述范围内的数,如果为合数,则必定有2 - 50000内的质因子.换一句话说,也就是如果一个数没有2 - 50000内的质因子,那么这个数为素

[email&#160;protected] Sieve of Eratosthenes (素数筛选算法) &amp; Related Problem (Return two prime numbers )

Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13,

POJ 2689 Prime Distance 素数筛选法应用

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

POJ题目2689 Prime Distance(任何区间素数筛选)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13459   Accepted: 3578 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

[ACM] POJ 2689 Prime Distance (大区间素数筛选)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 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