zoukankan      html  css  js  c++  java
  • Spoj PRIME1

    题意翻译

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

    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
    ",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
    ",l);
                }
            }
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    数据结构-图
    web.xml的运行顺序
    如何把自己打造成技术圈的 papi 酱
    也谈http中get和post
    手机充电速度及电池使用
    web项目Log4j日志输出路径配置问题
    JAVA模块化
    关于web安全
    Struts2中通配符
    2016第14周一
  • 原文地址:https://www.cnblogs.com/lovewhy/p/9246205.html
Copyright © 2011-2022 走看看