zoukankan      html  css  js  c++  java
  • POJ 2689 Prime Distance (埃氏筛 区间筛)

    原题链接(英文题面)
    中文题面
    acwing上wa时会有错误数据,还是很喜欢的~
    **

    Prime Distance

    **

    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

    Waterloo local 1998.10.17

    题解以后补吧
    数组开小了段错误,开大了内存超限…
    还有这是多组输入

    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include<stdlib.h>
    #include<stdio.h>
    using namespace std;
    typedef long long ll;
    const int N = 1e5;
    int prim[N];
    int judge[N];//0为素数,1为质数 
    int cnt=0;
    void init(){
    	for(ll i=2;i<N;i++){
    		if(!judge[i])
    			prim[++cnt]=i;
    			for(ll j=1;j<=cnt&&i*prim[j]<N;j++){
    				judge[prim[j]*i]=1;
    				if(i%prim[j]==0) break;
    			}
    	}
    }
    int mp[N];
    int prim2[N];
    int ans=0;
    void qvjian(ll l,ll r){
    	 ans=0;
    	memset(prim2,0,sizeof prim2);
    	memset(mp,0,sizeof mp);
    	if(l==1) l=2;
    	for(int i=1;i<=cnt&&prim[i]*prim[i]<=r;i++){
    		//ll x=l/prim[i];
    		//x*=prim[i];
    		//if(l%prim[i]==0){
    	//		x+=prim[i];
    	//		if(l!=2) mp[0]=1;
    	//	}  
    		ll x=(l+prim[i]-1)/prim[i]*prim[i];
    		if(x==prim[i]) x+=prim[i];
    		//cout<<prim[i]<<" "<<x<<endl;
    		
    		for(ll j=x;j<=r;j+=prim[i]) 
    			mp[j-l]=1;//合数 
    		
    	}
    	for(int i=0;i<=r-l;i++) if(!mp[i]) prim2[++ans]=i+l;
    }
    int main(){
    	init();
    	//for(int i=1;i<10;i++) cout<<prim[i]<<endl;
    //	cout<<endl;
    	ll l,r;
    	while(~scanf("%lld%lld",&l,&r)){
    		qvjian(l,r);
    		//for(int i=1;i<=ans;i++) cout<<prim2[i]<<endl;
    		if(ans<2) cout<<"There are no adjacent primes."<<endl;
    		else{
    			int minn=1,maxx=1;
    			for(int i=1;i<ans;i++){
    				int t=prim2[i+1]-prim2[i];
    				if(t<prim2[minn+1]-prim2[minn]) minn=i;
    				if(t>prim2[maxx+1]-prim2[maxx]) maxx=i;
    			}
    			printf("%d,%d are closest, %d,%d are most distant.
    ",prim2[minn],prim2[minn+1],prim2[maxx],prim2[maxx+1]);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    java中如何制作可双击执行的程序--jar打包工具的使用
    GUI菜单——菜单条、菜单、子条目之间关系
    设计模式——单例模式(饿汉式、懒汉式)
    深入理解利用new创建对象的执行过程以Person p=new Person("张三",20);为例
    构造代码块、构造函数、this执行顺序
    OpenGL 动画的制作
    UE4制作插件的插件神器pluginCreator
    3dmax中cs骨骼教程
    如何解决 应用程序无法启动 因为应用程序的并行配置不正确 问题
    如何自学游戏引擎的开发
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853221.html
Copyright © 2011-2022 走看看