You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
const int N = 50010 ;
int l , r ;
//答案
int ans ;
//欧拉筛
int primes [ N ] , cnt ; // primes[]存储所有素数
bool st [ N ] ; // st[x]存储x是否被筛掉
void get_primes ( int n ) {
for ( int i = 2 ; i < = n ; i + + ) {
if ( ! st [ i ] ) primes [ cnt + + ] = i ;
for ( int j = 0 ; primes [ j ] < = n / i ; j + + ) {
st [ primes [ j ] * i ] = true ;
if ( i % primes [ j ] = = 0 ) break ;
}
}
}
int main ( ) {
//优化一下
ios : : sync_with_stdio ( false ) ;
cin . tie ( 0 ) ;
//求出50000以内的质数:sqrt(INT32_MAX)=46341
get_primes ( N ) ;
//读入左右边界
cin > > l > > r ;
//第十一个测试点是后加上的, 导致有1 3这样的测试用例
if ( l < 2 ) l = 2 ;
//开始遍历所有数字,判断是不是已知质数区间的倍数
for ( int i = l ; i < = r ; i + + ) {
//是不是质数
bool f = false ;
//k是i的质数因子上限
int k = sqrt ( i ) ;
//遍历已经筛出来的质数数组, 从第一个2开始, 到k结束
for ( int j = 0 ; primes [ j ] < = k ; j + + ) {
//i:区间内的每个数字
//j:primes[j]代表每一个可能的质数因子
//如果能被某个质数整除,并且它不是质数因子。那么它就是合数
if ( i % primes [ j ] = = 0 & & i / primes [ j ] > 1 ) {
f = true ;
break ; //是合数就不用再继续尝试了
}
}
if ( ! f ) ans + + ;
}
//输出结果
cout < < ans < < endl ;
}