求助p1835素数密度
  • 板块学术版
  • 楼主sunny_chyz
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/5/26 21:06
  • 上次更新2023/11/4 22:41:25
查看原帖
求助p1835素数密度
39786
sunny_chyz楼主2021/5/26 21:06
#include<bits/stdc++.h>//错 P1835 素数密度
using namespace std;
#define maxn 1000010
int n,N,x,pri[10000];
typedef long long LL;
bool a[maxn];
int getss(int n,int pri[])
{
	for(int i=2;i*i<=n;i++)
	   if(a[i]==0)
	       for(int j=i<<1;j<=n;j+=i)  a[j]=1;
	int cnt=0;
	for(int i=2;i<=n;i++)
	   if(a[i]==0)  pri[cnt++]=i;
	return cnt;
}
int main()
{
	int cnt=getss(50000,pri);
	LL L,R;
	
	scanf("%lld %lld",&L,&R);
	for(int i=0;i<cnt;i++)
	   for(LL j=max(2ll,(L-1)/pri[i]+1)*pri[i];j<=R;j+=pri[i])
           a[j-L]=1;
    int ans=0;
    for(LL i=L;i<=R;i++)
        if(a[i-L]==0)  ans++;
    printf("%d",ans);
    return 0;

}
2021/5/26 21:06
加载中...