快速求质数 题解

快速求质数

时间限制: 1500ms

空间限制: 65536kB

题目描述:

输入正整数 N N N , M M M ,输出 N N N , M M M 之间的所有 质数。

输入格式:

输入一行,包含两个正整数 N N N , M M M

输出格式:

输出一行,以空格隔开。

样例输入:

23 40

样例输出:

23 29 31 37

约定:

( N < = M < 2 31 , M − N < = 20000 ) (N<=M<2^{31},M−N<=20000) (N<=M<231,MN<=20000)

提示:

RE的仔细想想RE的原因可能是什么


题意:

输入正整数 N , M 输出 N , M 之间的所有质数。

看看这题的数据,哇

约定
( N < = M < 2 31 , M − N < = 20000 ) (N<=M<2_{31},M−N<=20000) (N<=M<231,MN<=20000)

看来筛素数是不行了,只能判断素数 ( 用 is_ prime )

但是这题会卡常 !!!

1、开 long long , 而且要开 define int long long , int main 可删 int 或改成 signed main
2、用cin、cout要在前面加

ios::sync_with_stdio (false) ;
cin.tie (NULL) ; cout.tie (NULL) ;

3、(选) 最后开 return 0;
4、开  O 2 O_2 O2 、  O 3 O_3 O3 优化

只能想到这么多了,这题真的很恶心

放出AC代码,有些自己思考一模一样都会 TLE !!!

#include <bits/stdc++.h>
#define int long long
#define LL (long long)
#pragma G++ optimize (2)
#pragma G++ optimize (3)
using namespace std ;
int n , m ;
bool is_prime (int x) {
    if (x < 2)  return 0 ;
    for (int i = 2 ; i <= sqrt (x) ; i ++)
        if (x % i == 0)  return 0 ;
    return 1 ;
}
main () {
    ios::sync_with_stdio (false) ;
    cin.tie (NULL) ; cout.tie (NULL) ;
    cin >> n >> m ;
    for (int i = n ; i <= m ; i ++)
        if (is_prime (i))  cout << i << " " ;
    return 0 ;
}

编译结果:

恭喜,你通过了这道题
compiled successfully
time: 160ms, memory: 3516kb, score: 100, status: Accepted
> test 1: time: 0ms, memory: 3468kb, points: 20, status: Accepted
> test 2: time: 1ms, memory: 3516kb, points: 20, status: Accepted
> test 3: time: 11ms, memory: 3468kb, points: 20, status: Accepted
> test 4: time: 147ms, memory: 3392kb, points: 20, status: Accepted
> test 5: time: 1ms, memory: 3396kb, points: 20, status: Accepted
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值