https://abc084.contest.atcoder.jp/tasks/abc084_d
分析:题意明确,当时直接素筛, 没考虑复杂度,连T几次 后来改了一下,把所有的满足题意的素数的个数记录。复杂度O(n) 看着能不能优化 开挂试试,快了一倍
1.暴力打表
2.素筛
#include <bits/stdc++.h>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define rep(i,a,n) for(int i=a;i<n;i++)
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
const double eps=1e-6;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const int N=1e5+5;
int prime[N],sum[N];
bool is_pr[N];
int cnt;
inline int Scan()
{
int r = 0,f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
r = (r << 3) + (r << 1) + ch - '0';
ch = getchar();
}
return r * f;
}
inline void Print(int x)
{
if(x < 0)
{
x = -x;
putchar('-');
}
if(x > 9)Print(x/10);
putchar(x % 10 + '0');
}
void sieve()
{
mem(is_pr,0);
for(int i=2; i<N-4; i++)
{
if(!is_pr[i])
{
for(int j=i+i; j<N-4; j+=i)///j 初始值 必须是 i+i
is_pr[j]=1;
}
}
}
int main()
{
sieve();
for(int i=3;i<N-4;i+=2) if(!is_pr[i] && !is_pr[(i+1)/2]) sum[i]++;///题意
rep(i,2,N-4) sum[i] += sum[i-1];///记录
int q;
while(~scanf("%d",&q))
{
while(q--)
{
int l,r;
l = Scan(),r = Scan();
Print(sum[r] - sum[l-1]);
puts("");
}
}
return 0;
}