题解:P14073 [GESP202509 五级] 数字选取
题目大意:
从正整数
思路分析:
由题意得,选出来的这些数两两互质就是没有除
:::info[埃氏筛]
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,s=1;//先把1选了
bool f[N];
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=2;i<=n;i++){//从2开始标记,从1开始标记就会把所有的数都标记了
if(!f[i]){
s++;
for(int j=i<<1;j<=n;j+=i){
f[j]=1;
}
}
}
cout<<s;
return 0;
}
:::
:::info[线性筛]
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,s=1,t,p[N];
bool f[N];
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=2;i<=n;i++){
if(!f[i]){
s++;
p[++t]=i;
}
for(int j=1;j<=t&&1ll*i*p[j]<=n;j++){
f[i*p[j]]=1;
if(i%p[j]==0){
break;
}
}
}
cout<<s;
return 0;
}
:::