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,s,cnt;
 8 bool vis[500000];
 9 int f[500000];
10 int p[10000000];
11 void getprime()
12 {
13     cnt=0;
14     memset(vis,0,sizeof(vis));
15     vis[1]=true;
16     vis[0]=true;
17     for(int i=2; i<=50000; i++)
18     {
19         if(!vis[i])
20         {
21             f[cnt++]=i;
22             for(int j=i*2; j<=50000; j+=i)
23             {
24                 vis[j]=true;
25             }
26         }
27     }
28 }
29
30 int main()
31 {
32     getprime();
33     scanf("%d\n",&t);
34     while(t--)
35     {
36         scanf("%d%d%d%d",&a,&b,&k,&s);
37         memset(p,0,sizeof(p));
38         int ans=0;
39         int t1=0;
40         for(int i=a; i<=b; i++)
41         {
42             if(i<=50000)
43             {
44                 if(!vis[i]) p[t1++]=i;
45             }
46             else
47             {
48                 bool flag=true;
49                for(int j=0; j<cnt&&f[j]*f[j]<=i; j++)
50                {
51                     if(i%f[j]==0)
52                     {
53                         flag=false;
54                         break;
55                     }
56                }
57                if(flag)
58                {
59                    p[t1++]=i;
60                }
61             }
62         }
63         for(int i=0; i<t1; i++)
64         {
65             if(p[i+k-1]-p[i]==s)
66             {
67                 ans++;
68             }
69         }
70         printf("%d\n",ans);
71     }
72     return 0;
73 }

时间: 2025-01-01 08:08:03

LA 3998 Prime k-tuple的相关文章

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,

(算法竞赛入门经典 优先队列)LA 3135(前K条指令)

A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usage logs and telephone call records. Likewise, queries o

(算法入门经典大赛 优先级队列)LA 3135(之前K说明)

A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usage logs and telephone call records. Likewise, queries o

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