我们发现l,r的距离很小,所以直接暴力。
P.S. 不能把i
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define clr(a,x) memset(x,a,sizeof(x))
#define ll long long
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
const int maxn = 1000005;
const int maxx = 65536;
bool a[maxn],mark[maxn*5];
ll r,l;
template <class T> inline void read(T &xx)
{
xx = 0;
T flag = 1;
char ch = (char)getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = (char)getchar();
}
while(ch>='0' && ch<='9')
{
xx = (xx<<1) + (xx<<3) + ch - '0';
ch = (char)getchar();
}
xx *= flag;
}
int main(){
freopen("prime.in","r",stdin);
freopen("prime.out","w",stdout);
read(l); read(r);
clr(true,a); clr(true,mark);
int ans = 0;
a[1] = 0;
for(int i = 2; i <= sqrt(maxx); i++)
for(int j = 2; j <= maxx/i; j++)
if(a[i]) a[i*j] = 0;
if (r <= maxx)
{
for(int i = l; i <= r; i++)
if(a[i] != 0) ans++;
printf("%d",ans);
return 0;
}
for(int i = 1; i <= maxx; i++)
if(a[i] != 0)
for(int j = l/i; j <= r/i; j++)
mark[i*j-l+1] = 0;
for(int i = 1; i <= r-l+1; i++)
if(mark[i]) ans++;
printf("%d",ans);
return 0;
}