Almost Prime CodeForces - 26A(质因数分解模板)

题意:一个恰好有2个质因数的数被称为“半素数”,请求出1到n中有多少个半素数。例如,6,12,18,35为半素数,而30,2,4,8,9不是半素数

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
#define int long long
using namespace std;
const int maxn=1e6+5;
bool judge(int x){
	set<int>s;
	//cout<<123<<" "<<x<<endl;
	for(int i=2;i<=x/i;i++){
		while(x%i==0){
			s.insert(i);
			x/=i; 
		}
	}
	if(x>1)s.insert(x); 
	if(s.size()==2)return true;
	return false;
}
main(){
	int n;
	cin>>n;
	int ans=0;
	for(int i=6;i<=n;i++){
		if(judge(i)){
			ans++;
			//cout<<i<<endl;
		}
	}
	cout<<ans<<endl; 
}

猜你喜欢

转载自blog.csdn.net/Alanrookie/article/details/107878353