求帮忙
查看原帖
求帮忙
1098172
wshzy2023楼主2024/8/3 22:41
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int n = 1e8 + 10;
int cnt , p[n / 10];
bitset<n + 1> vis;
void prime1(){
	for(int i = 2; i <= n; i++) {
		if(vis == 0){
		    p[++cnt] = i;    // 质数 
	    }
	    for(int j = 1; i * p[j] <= n; j++){
	    	vis[i * p[j]] = 1;//质数的倍数是合数
			if(i % p[j] == 0){
			    break; // 最小质因数防止浪费时间 
		    }
		}
	}
} //线性筛 

int lp(int x){
	int ans = 0;
	while(x){
		ans += x % 10;
		x /= 10;
	}
	return ans;
}//各个位置的和

void prime2(){
	int cntt = 0;
	for(int i = 1; i <= cnt; i++){
		if(!vis[lp(p[i])]){
			p[++cntt] = p[i];//这个是质数 统计质数的个数 
		} 
	}
	cnt = cntt;
} 

int main(){
	prime1();
	prime2();
	int t;
	cin >> t;
	while(t--){
		int l , r;
		cin >> l >> r;
		int ans1 = upper_bound(p + 1 , p + cnt + 1 , r) - p;
		int ans2 = lower_bound(p + 1 , p + cnt + 1 , l) - p;
		cout << ans1 - ans2 << endl;
	}
    return 0;
}

为什么re了? 而且为什么要upper_bound(p + 1 , p + cnt + 1 , r) - p;为什么要-p呢?不理解

2024/8/3 22:41
加载中...