查询区间素数个数

博客探讨了如何处理查询在给定范围内素数个数的问题,适用于T次询问,每个询问包含一对正整数a和b,范围从1到10,000,000。" 124458339,7446128,VMware虚拟机网络配置:NAT模式详解及问题解决,"['虚拟化技术', '网络配置', 'VMware NAT', 'Linux网络']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 第一行输入一个正整数 T,代表询问次数 (1 ≤ T ≤ 100000)
  • 接下来 T 行,每行输入两个正整数 a,b 表示查询素数个数得范围为 [ a,b ] (1 ≤ a ≤ 10000000,a ≤ b ≤ 10000000)
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define mod 1000000007
#define INF 0x3f3f3f
#define fi first
#define se second
#define it iterator
#define ins insert
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define np next_permutation
#define pq priority_queue
#define ll long long
#define ull unsigned long long
#define mem(a) memset(a,0,sizeof(a))
#define cio ios::sync_with_stdio(false)
#define gcd __gcd
inline ll lgcd(ll a,ll b){return b == 0? a:lgcd(b, a % b);}
inline int lowbit(int x){return x&(-x);}
#define T int t;scanf("%d",&t);while(t--)
#define Ce cout << endl
#define CE(n) cout << n << endl
#define C(n) cout << n;
#define CY cout << "YES" << 
#define CN cout << "NO" << endl
#define Cy cout << "Yes" << endl
#define Cn cout << "No" << endl
int s[10000010];

void init()
{
    mem(s);
    ll x = 10000000;
    for(ll i = 2; i < sqrt(x); i++){
        if(s[i]==0){
            ll j = 2;
            while(i*j<=x){
                s[i*j] = 1;
                j++;
            }
        }
    }
    for(int i = 2; i <= x; i++){
        if(s[i]==1){
            s[i] = 0;
        }else{
            s[i] = 1;
        }
    }
    for(int i = 2; i <= x; i++){
        s[i] += s[i-1];
    }
}

int main()
{
    init();
    int t;
    scanf("%d", &t);
    while(t--){
        int a, b;
        scanf("%d %d", &a, &b);
        printf("%d\n", s[b]-s[a-1]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值