• [POJ2689] 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.

    题目大意:给定两个整数l,r,求闭区间[l,r]中相邻两个素数的差最大 最小分别是多少,输出相应的素数

    思路:由于每个合数n都包含一个不超过根号n的质因子,故我们可以筛出根号下INT_MAX的所有素数,并以此筛掉[l,r]之间的合数,将其中的所有素数按顺序保存.
    若少于2个素数则输出There are no adjacent primes.,否则我们先提出第1 2个素数并将其保存为差最大 最小的素数,然后从第三个素数开始每次与前一个素数相减,更新差最大最小即可.
    (代码保留了历史的伤痕)

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <bitset>
    #include <cstdio>
    #include <ctime>
    #include <cmath>
    #include <queue>
    #include <map>
    #include <set>
    //#include <windows.h>
    using namespace std;
    #define rg register
    #define LL long long
    #define __space putchar(' ')
    #define __endl putchar('
    ')
    template <typename qwq> inline void read(qwq & x)
    {
    	x = 0;
    	rg LL f = 1;
    	rg char c = getchar();
    	while (c < '0' || c > '9')
    	{
    		if (c == '-') f = -1;
    		c = getchar();
    	}
    	while (c >= '0' && c <= '9')
    	{
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	x *= f;
    }
    template <typename qaq> inline void prLL(qaq x)
    {
    	if (x < 0)
    	{
    		putchar('-');
    		x = -x;
    	}
    	if (x > 9) prLL(x / 10);
    	putchar(x % 10 + '0');
    }
    const int maxn = 50005;
    LL l,r;
    vector <LL> res;
    vector <LL> ::iterator it;
    #define max(a,b) (a > b ? a : b)
    LL pri[maxn];
    int tot;
    bool flag[maxn];
    inline void make_prime_list(int r)
    {
    	for (rg int i = 2;i <= r;++i)
    	{
    		if (!flag[i]) pri[++tot] = i;
    		for (rg int j = 1;j <= tot && pri[j] * i <= r;++j)
    		{
    			flag[i * pri[j]] = true;
    			if (!(i % pri[j])) break;
    		}
    	}
    }
    //inline bool isPrime(int x)
    //{
    //	if (x == 1) return false;
    //	for (rg int i = 1;i <= tot;++i)
    //	{
    //		if (pri[i] >= x) return true;
    //		if (!(x % pri[i])) return false;
    //	}
    //	return true;
    //}
    bitset <2333333> vis;
    map<LL,int> mp;
    inline void shai()
    {
    	vis.reset();
    //	cout << clock() << endl;
    	if (l == 1) vis[0] = true;
    //	mp.clear();
    //	mp[1] = 1;
    	for (rg int i = 1;i <= tot;++i)
    	{
    		rg LL p = pri[i];
    		for (rg LL j = ceil(1.0 * l / p);j * p <= r;++j)
    			if (j > 1 && j * p - l >= 0) vis[j * p - l] = true;//,prLL(j * p),putchar(' '),prLL(j*p-l),__endl;
    //		for (rg int j = max(2,(l + p - 1) / p) * p;j <= r;j += p) mp[j] = 1;
    	}
    //	cout << clock() << endl;
    }
    LL x,y,xx,yy;
    inline void work()
    {
    	res.clear();
    	shai();
    	for (rg LL i = l;i <= r;++i)//插入l ~ r的素数 
    	{
    		if (!vis[i - l]) res.push_back(i);
    //		if (!mp.count(i)) res.push_back(i);
    	}
    	if (res.size() < 2)//不足2个 
    	{
    		puts("There are no adjacent primes.");
    		return;
    	}
    	rg bool first = false,second = false;
    	for (it = res.begin();it != res.end();++it)//先让两种情况均为第一个 第二个素数 
    	{
    		if (!first) x = y = *it,first = true;//x = y = res[0]
    		else if (first && !second) xx = yy = *it,second = true;//xx = yy = res[1]
    		else break;
    	}
    //	x = y = res[0],xx = yy = res[1];
    
    	rg LL maxx = yy - y,minn = xx - x;//minn:最近距离,maxx:最远距离 
    	first = true,second = true;
    	for (it = res.begin();it != res.end();++it)//end + 1? end - 1?
    	{
    		if (first)//不访问res[0] 
    		{
    			first = false;
    			continue;
    		}
    		if (!first && second)//不访问res[1]
    		{
    			second = false;
    			continue;
    		}
    		if (*it - *(it - 1) < minn)//距离更小 
    		{
    			x = *(it - 1),xx = *it;
    			minn = xx - x;
    		}
    		if (*it - *(it - 1) > maxx)//距离更大 
    		{
    			y = *(it - 1),yy = *it;
    			maxx = yy - y;
    		}
    	}
    	printf("%lld,%lld are closest, %lld,%lld are most distant.
    ",x,xx,y,yy);
    	return;
    }
    int main()
    {
    //	freopen("fuck.out","w",stdout);
    //	if (isPrime(2146841093)) cout << "yes";
    //	else cout << "no";
    //	return 0;
    	make_prime_list(50000);
    	while(~scanf("%lld%lld",&l,&r))
    	{
    //		return 0;
    		work();
    //		shai();
    //		cout << mp.count(2);
    	}
    }
    
  • 相关阅读:
    25个优秀的 ASP.NET MVC教程及文章
    获取SQLSERVER数据库insert into操作的主键返回值,SCOPE_IDENTITY
    Silverlight 3.0 不再包含 asp:silverlight 控件
    MVP模式最佳实践(1)—MVP模式简介
    小谈.NET CLR
    XmlDocument,XmlNode,XmlElement创建复杂XML文档
    NHibernate的关联映射(onetoone,onetomany,manytomany)以及cascade分析
    关于CSS优先级的探讨
    使用ASP.NET实现Model View Presenter(MVP)
    用IIS来启用SSL
  • 原文地址:https://www.cnblogs.com/Here-is-SG/p/10617333.html
Copyright © 2020-2023  润新知