POJ2689 Prime Distance

题意:输入[l,r]代表,问区间里面最近和最远的两个素数(0<l<r<int_max, r-l<100000)

题解:素数筛,一个数是合数,那么它存在小于sqrt(n)的素因子,找到所有小于sqrt(int_max)的素数,注意数据:1 2

#include <iostream>
#include <string.h>
#include <stdio.h>
#define N 1001000
#define ll long long
using namespace std;
bool isprime[N], isp[N];
long long prime[N], num, ans[N];
void doprime(long long n){
    long long i,j;
    num = 0;
    memset(isprime, true,sizeof(isprime));
    isprime[1] = 0;
    for(i=2;i<=n;i++){
        if(isprime[i]){
            prime[num++] = i;
            for(j=i*i;j<=n;j+=i) isprime[j] = false;
        }
    }
}
void doprime2(long long l,long long r){
    memset(isp, true, sizeof(isp));
    for(long long i=0;i<num;i++){
        long long t = l/prime[i];
        while(prime[i]*t<l||t<=1) t++;
        for(long long j=t*prime[i];j<=r;j+=prime[i])
            isp[j-l] = 0;
    }
    if(l==1) isp[0] = 0;
}
int main(){
    long long ansl,ansr, l, r, ansll, ansrr;
    doprime(50000);
    while(cin>>l>>r){
        doprime2(l,r);
        long long t = 0;
        for(long long i=l;i<=r;i++)
            if(isp[i-l] == 1)
                ans[t++] = i;
        if(t < 2) printf("There are no adjacent primes.\n");
        else{
            long long mi = 10000000, ma = -10000000;
            for(long long i=0;i<t-1;i++){
                if(mi>ans[i+1]-ans[i]){
                    ansl = ans[i];
                    ansr = ans[i+1];
                    mi = ansr-ansl;
                }
                if(ma < ans[i+1]-ans[i]){
                    ansll = ans[i];
                    ansrr = ans[i+1];
                    ma = ans[i+1]-ans[i];
                }
            }
            printf("%lld,%lld are closest, %lld,%lld are most distant.\n", ansl, ansr, ansll, ansrr);
        }
    }
}
时间: 2024-10-04 11:08:52

POJ2689 Prime Distance的相关文章

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 (两重筛素数,区间平移)

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

POJ2689 Prime Distance(数论:素数筛选)

题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24073 Accepted: 6306 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of

POJ2689 Prime Distance 区间筛素数

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 prop

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 素数筛选法应用

题目来源: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