3998 - Prime k-tuple

{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

思路:区间筛素数;

数据就是很水,没给定【a,b】区间的大小,然后筛法水过。然后统计的时候,维护两个端点就行了。
 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<queue>
 6 #include<math.h>
 7 using namespace std;
 8 typedef long long LL;
 9 bool prime[100005];
10 bool prime_max[20*1000000];
11 int ans[100005];
12 LL ac[1000007];
13 int  main(void)
14 {
15         memset(prime,0,sizeof(prime));
16         int i,j;
17         int cn = 0;
18         for(i = 2; i < 1000; i++)
19                 if(!prime[i])
20                         for(j = i; (i*j) < 100005; i++)
21                                 prime[i*j] = true;
22         for(i = 2; i < 100005; i++)
23         {
24                 if(!prime[i])
25                         ans[cn++] = i;
26         }
27         int n;
28         scanf("%d",&n);
29         LL a,b,k,t;
30         while(n--)
31         {
32                 scanf("%lld %lld %lld %lld",&a,&b,&k,&t);
33                 memset(prime_max,0,sizeof(prime_max));
34                 LL s ;
35                 for(i = 0; i < cn; i++)
36                 {
37                         for(s = max(2LL,a/ans[i])*ans[i]; s <= b; s += ans[i])
38                         {
39                                 if(s >= a)
40                                         prime_max[s-a] = true;
41                         }
42                 }
43                 int v = 0;
44                 for(s = a; s <= b; s++)
45                 {
46                         if(!prime_max[s-a])
47                                 ac[v++] = s;
48                 }
49                 int l = 0;
50                 int r = k-1;
51                 LL ask = 0;
52                 while(true)
53                 {
54                         if(r >= v)
55                                 break;
56                         if(ac[r] - ac[l] == t)
57                         {
58                                 ask++;
59                         }
60                         l++;
61                         r++;
62                 }
63                 printf("%lld\n",ask);
64         }
65         return 0;
66 }

 
时间: 2024-12-20 11:57:15

3998 - Prime k-tuple的相关文章

LA 3998 Prime k-tuple

题意:如果K个相邻素数p1,p2,p3.....pk满足pk-p1=s,称这些素数组成一个距离为s的素数K元组,输入a,b,k,s,输出区间[a,b]内距离为s的素数k元组的个数. 思路:先打到50000素数表,然后暴力求出a,b区间的素数,然后判断. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int t; 7 int a,b,k,

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 i

[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

[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,

Project Euler:Problem 87 Prime power triples

The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way: 28 = 22 + 23 + 24 33 = 32 + 23 + 24 49 = 52 + 23 + 24 47

杭电 HDU ACM 1016 Prime Ring Problem

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 31900    Accepted Submission(s): 14108 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

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

九度OJ 1040 Prime Number (筛素数,试除法)

题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k-th prime number. 样例输入: 3 7 样例输出: 5 17 这道题,好久以前使用试除法做的,原理是维护一个素数表,根据输入的num,确定是否之前算过,算过了,就直接输出,没算过,就现在开始算,并且把中间的素数全保存下来: #include<stdio.h> int k[10001]; int main(int argc, char *argv[]) { k[1]=

POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20020   Accepted: 10966 Description Some positive integers can be represented by a sum of one or more consecutive