Description
Determinate Prime
Input: standard input
Output: standard output
Time Limit: 1 second
If three or more consecutive primes are uni-distance they are called Determinate Primes. Your task is to print all the Determinate Prime sets between two integers (inclusive).
Input
The input is consist of several test cases. Each test case consists of two non negative integers x and y. None of the input will be grater than 32000. Input will be terminated with two zeroes.
Output
For each test case you have to print all the Determinate Primes between x and y. Each set must be in a different line. For clarity check out the sample input and output.
NB: No subset of a series is allowed. For example, a series of five uni-distant primes having even four of them in the interval is not allowed, all the five primes should be in the interval.
The first two lines and the third line of the sample output are the outputs for the first and second sample inputs respectively.
Sample Input
1 100
2 8
0 0
Sample Output
3 5 7
47 53 59
3 5 7
//题意:题意很明了也不难。本题考的不是解题而是读题。并且也有重大的坑,首先给定的区间[x,y],x可能大于y。 其次,题目最后对输出要求说对于满足题意的素数序列不能输出它的子集。为了更好理解这句话,【假设9也是素数】,那么在区间[5,,20],满足题意的素数序列为5,7,9,11,13。大多数人对“不能输出它的子集”这句话理解为要输出最长的满足在区间内的素数序列,即要把5,7,9,11,13同时输出而不能仅输出5,7,9,11这个子集。 但这种理解对于题目要求是不完全的。
//序列5 7 9 11 13 是3 , 5 , 7, 9, 11, 13的子集。 那么就不能输出子集5 7 9 11 13而必须输出3 5 7 9 11 13. 但是3不在区间[5,20]范围内,所以整个序列都不能输出了。即如果在在区间[x,y]内满足题意的素数序列A如果是B序列的子集的话,而B序列里某些元素又不在区间[x,y]之内,则我们不能输出A序列,这就是题目要求的“不能输出它的子集”。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;
#define maxn 33000
bool x[maxn];
int prime[maxn],cnt=0;
void Init()
{
int i,tmp;
memset(x,false,sizeof(x));
x[0]=x[1]=true;
for(i=2;i<maxn;i++)
if(!x[i])
{
prime[++cnt]=i;
tmp=2*i;
while(tmp<maxn)
{
x[tmp]=true;
tmp+=i;
}
}
}
int main()
{
Init();
int n,m,i,j,k;
while(scanf("%d%d",&n,&m),n|m)
{
//if(n>m) swap(n,m);
int num=1,lef=1,t=-1;
for(i=1;i<cnt;i++)
{
if(prime[i+1]-prime[i]!=t)
{
if(prime[lef]<n||prime[lef]>m||prime[i]<n||prime[i]>m) //判整个素数序列是否都在[x,y]内,不能输出其子集
{
lef=i,num=1,t=prime[i+1]-prime[i];;
continue;
}
if(num>=2)
{
for(j=lef;j<i;j++)
printf("%d ",prime[j]);
printf("%d\n",prime[j]);
}
lef=i,num=1,t=prime[i+1]-prime[i];
if(prime[i]>m||prime[i+1]>m)
break;
}
else num++;
}
}
return 0;
}