SPOJ Problem 2: Prime Generator

嗯。。在SPOJ上刷的第二题。

一开始不知道哪错了,后来发现i出现了两遍。。

因为m<10^9,所以用素数筛筛32000以内的数,开一个4000的数组存储就行。然后再从n开始用素数筛,总之效率还行。

代码如下:

//0.01s 3.2M
#include<cstdio>
#include<cstring>
#include<cmath>
int n,i,j,t,m,k,tot;
int a[100005],b[4000];
int prime[40000];
int shai(int s){
    int i,j;
    memset(prime,0,sizeof(prime));
    prime[1]=2;
    i=2;
    while(i<s){
        prime[i]=1;
        for(j=i;j<=s/i;j++){
            prime[j*i]=2;
        }
        while(prime[i])i++;
    }
    tot=0;
    for (i=1;i<=s;i++){
        if (prime[i]==1)b[++tot]=i;
    }
    return 1;
}
int main(){
    shai(32000);
    for (scanf("%d",&t);t;t--){
        scanf("%d%d",&n,&m);
        k=m;
        k=int(sqrt(double(k)));
        memset(a,0,sizeof(a));
        for (j=1;j<=tot;j++){
            int less = n / b[j];
            if (less<b[j])less=b[j];
            for (i=less;i<=m/b[j];i++){
                if (i*b[j]<n)continue;
                a[i*b[j]-n]=1;
            }
            if (b[j]*b[j]>m)break;
        }
        for (i=0;i<=m-n;i++){
            if (a[i]==0&&i+n!=1)printf("%d\n",i+n);
        }
        if (t)printf("\n");
    }
    return 0;
}
时间: 2024-09-30 10:27:12

SPOJ Problem 2: Prime Generator的相关文章

素数筛法--SPOJ Problem 2 Prime Generator

质数(prime number)又称素数,除了1和它本身外,不能整除以其他自然数,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.最小的质数是2. 要判断一个整数N是不是质数很简单,看它是否能被2到sqrt(N)之间的整数整除即可. def isPrime(n): if n%2==0: return False for i in xrange(3,int(math.sqrt(n)+1),2): if n%i==0: return False return True 不过要找出1

Spoj PRIME1 - Prime Generator

题意翻译 求给定的两个数之间的素数 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 cas

Prime Generator(spoj)

原题: Prime Generator Problem code: PRIME1 Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t of test cases in a sing

SPOJ Python Day2: Prime Generator

2. Prime Generator 任务很简单,生成m到n之间的所有质数.一个比较常见的思路是: 自然数$1, 2, -, N$中的最大的质因子要小于$\sqrt{N}$.所以用m到n中的每一个数去试除1到$\sqrt{n}$中的所有数.能整除就是合数,全不能整除就是质数. 但是这么做会超时.. 一般生成质数有一个常用的算法:筛法 http://zh.wikipedia.org/wiki/%E5%9F%83%E6%8B%89%E6%89%98%E6%96%AF%E7%89%B9%E5%B0%B

SPOJ Problem 7974:What&#39;s Next

求数列下一项,啥都不说了,贡献了N多WA... #include<cstdio> int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&c)&&(a||b||c)){ if (b*2==c+a&&b!=a) printf("AP %d\n",2*c-b); else printf("GP %d\n",c*c/b); } return

SPOJ Problem 9948:Will it ever stop

如题..http://www.spoj.com/problems/WILLITST/ #include<cstdio> long long n; int main(){ scanf("%lld",&n); while(n>1){ if (n==3||n==6){printf("NIE\n");return 0;} if (n&1)n=(n+1)/2*3;else n>>=1; } printf("TAK\n&q

SPOJ Problem 302:Count on Cantor

题目不复述了,自己看吧.http://www.spoj.com/problems/CANTON/一如既往的暴力.. #include<cstdio> int n,i,j,s,ans; int main(){ scanf("%d",&n); while(n--){ scanf("%d",&s); printf("TERM %d IS ",s); for (i=1;s>i;i++){ s-=i; } if (i&am

Problem 005——Digit Generator

1583 - Digit Generator For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore,

Prime Generator

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t li