转学长博客。
传送门
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10010
using namespace std;
int n;
int p[N],cnt;
bool he[N];
void getp()
{
int i,j;
for(i=2;i<=n;i++)
{
if(!he[i]) cnt++,p[cnt]=i;
for(j=1;j<=cnt&&i*p[j]<=n;j++)
{
he[i*p[j]]=true;
if(i%p[j]==0) break;
}
}
}
int main()
{
scanf("%d",&n);
getp();
int i=1,j=cnt;
int ans=0;
while(i<=j)
{
ans++;
if(p[i]*p[j]<=n) i++,j--;
else j--;
}
printf("%d",ans);
}

本文介绍了一种利用素数筛法求解特定区间内素数对的数量的方法,并给出了完整的C++代码实现。通过标记合数的方式找出所有小于等于n的素数,并统计在该区间内的素数对数目。
1044

被折叠的 条评论
为什么被折叠?



