Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
POJ 2689
Description
The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Input
Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.
Output
For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
现在写这个得简洁点了qwq废话太多。。。但是最重要的不是方法还是想到这个方法的过程。。。。
大意:给出区间[l,r]求这个区间里 相邻素数差值最小和最大的两组素数。
朴素想法就是枚举区间每一个数,判断其是否为素数,判断的时候是挨着去除之前筛好了的1~2^16的素数(因为一个合数肯定能由多个素数相乘得到)。复杂度应该是 10^6*6000左右吧。
然后就需要考虑怎么优化的问题。
优化的关键点应该放在素数的判断上。原想法是让每一个数挨着挨着找它的素数因子,但是这样会除很多次,这就很像我们最开始对找素数法(枚举)进行优化一样。用同样的思维,窝们可以也用筛的方法。因为l~r的区间很小,用已经有的素数去筛这个区间里的数。这样每个数只会被访问一次,不同的素数也只会被用一次。复杂度 P+1E6 。
自己代码实现上还有很多细节没有掌握好.
发现了一个很细节的问题。
看起来枚举l~r的循环变量i 定义成int没有什么问题。
其实i++很致命。因为r可以等于2^31-1 这是时候i再++,就会超int 然后变成负数。于是又符合i≤2^31-1这个条件了,于是又会接着循环。一方面问题是会访问无效内存(下标为负了),另一方面会增加循环次数。
所以。。。以后这种擦边的问题一定要开longlong qwqwq
// Created by ZYD in 2015.
// Copyright (c) 2015 ZYD. All rights reserved.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define Size 100000
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array) memset(array,0,sizeof(array))
typedef pair<int,int> P;
int prime[65538],used[1000050],ans[1000050];
int l,r,p;
int twice(int l,int r){
for(int i=1;i<=p;i++)
{
int x=prime[i];
// cout<<x<<" ";
if(x>r) break;
// cout<<l/x<<","<<r/x<<" ";
for(int j=l/x;j<=r/x;j++)
{
if(j*x<l || j<=1) continue;
int xx=j*x-l;
// cout<<j*x-l<<" ";
used[xx]=1;
// cout<<xx<<" ";
}
}
return 0;
}
int main()
{
freopen("in.txt","r",stdin);
p=0;mem(used);
for(int i=2;i<=65536;i++)
if(used[i]==0)
{
used[i]=1;
for(int j=i+i;j<=65536;j+=i)
used[j]=1;
prime[++p]=i;
// cout<<i<<" ";
}cout<<p<<endl;
while(cin>>l>>r){
mem(used);mem(ans);
twice(l,r);
int t=0;
int L=l;
if (l==1) L++;
// cout<<r<<endl;
for(ll i=L;i<=r;i++)
{
cout<<i<<endl;
int xx=i-l;
// cout<<xx<<" ";
if(used[xx]==0)
// cout<<xx<<",i="<<i<<" ";
{
ans[++t]=i;
// cout<<xx<<" "<<i<<endl;
}
}
// cout<<used[97]<<endl;
if(t<=1){
printf("There are no adjacent primes.\n");
continue;
}
int maxx=0,minn=2147483647;
// cout<<minn<<endl;
int min1,min2,max1,max2;
for(int i=2;i<=t;i++){
if(ans[i]-ans[i-1]<minn){
min1=ans[i-1];
min2=ans[i];
minn=ans[i]-ans[i-1];
// cout<<minn<<" ";
}
if(ans[i]-ans[i-1]>maxx){
max1=ans[i-1];
max2=ans[i];
maxx=ans[i]-ans[i-1];
// cout<<maxx<<" ";
}
}//cout<<endl;
printf("%d,%d are closest, %d,%d are most distant.\n",min1,min2,max1,max2);
}
return 0;
}