UVA10140 Prime Distance

题意翻译

大致意思:给定两个整数L,R(1<=L<=R<=2147483648,R-L<=1000000),求闭区间 [L,R]中相邻两个质数的差的最小值和最大值是多少,分别输出这两个质数。

【输入格式】

输入有若干行,每行两个整数 L,R

【输出格式】

对于每个L,R输出最小值和最大值,格式参照样例。若区间内无质数,输出"There are no adjacent primes."。

分割线

看到这题,瞄一眼数据范围,噫,2的31次方,比int范围大了1,然后看到了下一句,咦?R-L好小,只有一百万,所以,我们从这方面入手。

首先,先把2到根号R中的质数筛出来,根号R很小,不超过五万,此为预处理,L到R中的质数其实只要到根号R,因为大于根号R的质数要么是一个小质数乘一个大质数,而先前因为小质数已经将这种的筛掉了,要么是大质数乘大质数,已经超过了R,不在处理范围,然而小质数乘小质数也不超过根号R(此处小质数指小于根号R的质数,大质数指大于根号R的质数)。

然后我注意到了是多组数据,所以开头的一堆附初值是少不了的,切记不要忘掉!

之后,枚举一个质数,找到它的第一个倍数(在L到R区间内),和最后一个倍数,在此区间内枚举,删掉质数的倍数,再扫一遍得到一个质数表。

最后,就是相邻质数之差选一个最小和最大,这好办,枚举一个i,和i+1做差即可。

数学题哭唧唧qwq

代码如下:(注意开longlong,有乘法的时候要乘1ll,把它暂时变为longlong类型其实全开longlong了乘不乘也没什么关系>_>)

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#define ll long long
#define M 1000010
using namespace std;
ll l,r,flag,cnt,cnt1,cnt2,ans1,ans2,ans3,ans4,vis[M],a[M],b[M],c[M],c2[M],c3[M],minn,maxx;
int main()
{
    for(ll i=2;i<=50000;i++)
    {
        if(vis[i]==0)
        {
            a[++cnt]=i;
            for(ll j=i+i;j<=50000;j+=i)
            {
                vis[j]=1;
            }
        }
    }
    while(scanf("%lld%lld",&l,&r)!=EOF)
    {
        flag=0;
        memset(vis,0,sizeof(vis));
        for(ll i=1;i<=cnt;i++)
        {
            for(ll j=l/a[i];j<=r/a[i];j++)//得到l到r区间的a[i]的倍数
            {
                if(1ll*a[i]*j-l<0)continue;
                if(j>1)vis[1ll*a[i]*j-l]=1;//我们不能删除这个质数本身,也就是1倍的a[i],大于等于2以上的倍数才能删掉,这里特判
            }
        }
        cnt1=cnt2=0;
        if(l==1)vis[0]=1;
        for(ll i=l;i<=r;i++)
        {
            if(vis[i-l]==0)b[++cnt1]=i,flag++;
        }
        if(flag<=1){printf("There are no adjacent primes.\n");}
        else if(flag>1)
        {
            for(ll i=1;i<=cnt1-1;i++)
            {
                c[++cnt2]=b[i+1]-b[i];
                c2[cnt2]=b[i];
                c3[cnt2]=b[i+1];
            }
            minn=1e18;
            maxx=0;
            for(ll i=1;i<=cnt2;i++)
            {
                if(minn>c[i]){minn=c[i];ans1=c2[i];ans2=c3[i];}
                if(maxx<c[i]){maxx=c[i];ans3=c2[i];ans4=c3[i];}
            }
            printf("%lld,%lld are closest, %lld,%lld are most distant.\n",ans1,ans2,ans3,ans4);
        }
    }
    return 0;
}

end.谢谢阅读

原文地址:https://www.cnblogs.com/lcccAngelo/p/9988993.html

时间: 2024-10-12 07:10:12

UVA10140 Prime Distance的相关文章

POJ2689 ZOJ1842 UVA10140 Prime Distance【筛选法】

Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25099 Accepted: 6567 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theore

[ACM] POJ 2689 Prime Distance (筛选范围大素数)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

UVA 10140 - Prime Distance(数论)

10140 - Prime Distance 题目链接 题意:求[l,r]区间内最近和最远的素数对. 思路:素数打表,打到sqrt(Max)即可,然后利用大的表去筛素数,由于[l, r]最多100W,所以可以去遍历一遍,找出答案.注意1的情况,一开始没判断1,结果WA了 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define INF 0x3f

POJ-2689 Prime Distance(线性筛法)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17021   Accepted: 4536 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

POJ 2689 Prime Distance 素数筛选法应用

题目来源:POJ 2689 Prime Distance 题意:给出一个区间L R 区间内的距离最远和最近的2个素数 并且是相邻的 R-L <= 1000000 但是L和R会很大 思路:一般素数筛选法是拿一个素数 然后它的2倍3倍4倍...都不是 然后这题可以直接从2的L/2倍开始它的L/2+1倍L/2+2倍...都不是素数 首先筛选出一些素数 然后在以这些素数为基础 在L-R上在筛一次因为 R-L <= 1000000 可以左移开一个1百万的数组 #include <cstdio>

POJ 2689 Prime Distance

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

POJ题目2689 Prime Distance(任何区间素数筛选)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13459   Accepted: 3578 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

poj 2689 Prime Distance 【数论】【筛法求素数】

题目链接:传送门 题目大意: 给你L和R两组数,L和R的范围是2^32,其间隔(即R-L最大为1,000,000.) .让你求出L和R之间素数的最大间隔和最小的间隔. 比如 2 17.之间的最小素数间隔是2 3,最大的素数间隔是11 17. 要是直接进行一个2^32次方筛法然后在判断是会T的. 我们这样来想,筛法求素数的原理是什么: /**vis数组标记为0则说明是素数*/ int vis[10005]; void getPrimevis(int n) { int m=sqrt(n+0.5);

POJ-2689 Prime Distance (两重筛素数,区间平移)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th