c++ - 我可以优化埃拉托色尼的分段筛吗
问题描述
我试图在 c++ 中找到给定范围内的素数。但它仍然显示 TLE。我可以进一步优化它吗?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
std::cin>>T;
while(T--){
int r,Y;
cin >> r>>Y;
int count=0;
int l=1;
vector<bool>isPrime(r - l + 1,true); //filled by true
for (long long i = 2; i * i <= r; ++i) {
for (long long j = max(i * i, (l + (i - 1)) / i * i); j <= r; j += i) {
if(isPrime[j - l]){
isPrime[j - l] = false;
count++;
}
}
}
count=isPrime.size()-count-1;
std::cout<<count<<"\n";
}
return 0;
}
解决方案
推荐阅读
- angular - 角度重定向到身份验证
- android - How can i pass a variable from my adapter to a fragment i instantiate in Kotlin
- javascript - Socket.IO 中间件,io.use
- ios - 数据结构中的节点应该持有对对象的引用还是对象本身?
- node.js - Node.js WebdriverIO 错误
- php - PHP 5.x 与 7.x - 使用行为不同的变量的对象属性
- python - Python ctypes:在c中调用具有自定义类型的函数
- r - 如何让 ggplot 从特定月份开始 x 轴
- javascript - 将图像从网络下载到桌面文件夹 JS
- java - multiple executions of PMD maven plugin