zoukankan      html  css  js  c++  java
  • POJ2689 Prime Distance

    题意

    Language:
    Prime Distance
    Time Limit: 1000MSMemory Limit: 65536K
    Total Submissions: 25344Accepted: 6620

    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.
    

    Source

    分析

    不可能预处理出所有质数,由于U-L很小,所以考虑把[L,U]中的所有质数筛出来。

    预处理出(sqrt{U})中的所有质数,用类似Eratothenes的方法把合数划去。最后扫一遍就好了。

    时间复杂度(O(sqrt{U} log log U+(U-L) log log U))

    代码

    #include<iostream>
    #include<vector>
    #include<cstring>
    #define rg register
    #define il inline
    #define co const
    template<class T>il T read(){
    	rg T data=0,w=1;
    	rg char ch=getchar();
    	while(!isdigit(ch)){
    		if(ch=='-') w=-1;
    		ch=getchar();
    	}
    	while(isdigit(ch))
    		data=data*10+ch-'0',ch=getchar();
    	return data*w;
    }
    template<class T>il T read(rg T&x){
    	return x=read<T>();
    }
    typedef long long ll;
    using namespace std;
    
    co int N=100006,L=1000006,M=46340,INF=0x7fffffff;
    bool v[L];
    vector<int> p,ans;
    int main(){
    //	freopen(".in","r",stdin);
    //	freopen(".out","w",stdout);
    	memset(v,1,sizeof v);
    	for(int i=2;i<=M;++i)
    		if(v[i]){
    			p.push_back(i);
    			for(int j=2;j<=M/i;++j) v[i*j]=0;
    		}
    	unsigned l,r;
    	while(~scanf("%d%d",&l,&r)){
    		memset(v,1,sizeof v);
    		ans.clear();
    		if(l==1) v[0]=0;
    		for(unsigned i=0;i<p.size();++i)
    			for(unsigned j=(l-1)/p[i]+1;j<=r/p[i];++j)
    				if(j>1) v[p[i]*j-l]=0;
    		for(unsigned i=l;i<=r;++i)
    			if(v[i-l]) ans.push_back(i);
    		int minn=INF,maxx=0,x1,y1,x2,y2;
    		for(unsigned i=0;i+1<ans.size();++i){
    			int num=ans[i+1]-ans[i];
    			if(num<minn) minn=num,x1=ans[i],y1=ans[i+1];
    			if(num>maxx) maxx=num,x2=ans[i],y2=ans[i+1];
    		}
    		if(!maxx) puts("There are no adjacent primes.");
    		else printf("%d,%d are closest, %d,%d are most distant.
    ",x1,y1,x2,y2);
    	}
    	return 0;
    }
    
  • 相关阅读:
    MongoDB 安装和即基本操作
    技术型创业者easy遇到的三大问题
    Swoole源代码学习记录(十五)——Timer模块分析
    利用NSInvocation对方法进行抽象,实现对方法的加锁
    Atitit.hibernate体系结构大总结
    HDU
    程序猿工作效率的影响因素和管理者怎样推断
    Linux下tomcat使用
    [1204 寻找子串位置] 解题报告
    Team Foundation Server 2013 with Update 3 Install LOG
  • 原文地址:https://www.cnblogs.com/autoint/p/10439102.html
Copyright © 2011-2022 走看看