码迷,mamicode.com
首页 > 其他好文 > 详细

Spoj PRIME1 - Prime Generator

时间:2018-06-30 12:45:31      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:printf   lag   output   with   crypto   spoj   ret   个数   peter   

题意翻译

求给定的两个数之间的素数

Translated by @kaiming

题目描述

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!

输入输出格式

输入格式:

 

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

 

输出格式:

 

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

 

输入输出样例

输入样例#1: 复制
2
1 10
3 5
输出样例#1: 复制
2
3
5
7

3
5

说明

Warning: large Input/Output data, be careful with certain languages (though most should be OK if the algorithm is well designed)
Information

After cluster change, please consider PRINT as a more challenging problem.

 

//Pro: Spoj PRIME1 - Prime Generator
//求给定的两个数之间的素数 

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;

const long long N=40000;

long long T;
long long cnt;
long long prime[N+1];
bool flag[N+1];

void init()
{
    flag[1]=1;
    long long tmp;
    for(long long i=2;i<=N;++i)
    {
        if(!flag[i])
            prime[++cnt]=i;
        for(long long j=1;j<=cnt&&(tmp=i*prime[j])<=N;++j)
        {
            flag[tmp]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}


long long l,r;
bool Flag;
int main()
{
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&l,&r);
        for(;l<=r;++l)
        {
            if(l<=N)
            {
                if(!flag[l])
                {
                    printf("%d\n",l);
                    continue;
                }
            }
            else
            {
                Flag=0;
                for(long long j=1;prime[j]<=sqrt(l)&& !Flag && j<=cnt;++j)
                {
                    if(l%prime[j]==0)
                        Flag=1;
                }
                if(!Flag)
                    printf("%d\n",l);
            }
        }
        puts("");
    }
    return 0;
}

 

Spoj PRIME1 - Prime Generator

标签:printf   lag   output   with   crypto   spoj   ret   个数   peter   

原文地址:https://www.cnblogs.com/lovewhy/p/9246205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!