Blue Bridge Cup—Egyptian Screening Method

  • Angstrom sieve
    Angstrom sieve is a method in O ( nloglogn ) O(nloglogn)Find2 − N 2 -N in O ( n l o g l o g n )2The algorithm of all prime numbers in N , the basic idea is to traverse 2 − N 2-N2N , if the current number is not filtered, then it is a prime number, and then all the multiples of the current number are eliminated. When all the numbers have been traversed, the last remaining number is a prime number
for(int i=2;i<=N;i++){
    
    
	//被剔除了,直接pass
	if(flag[i]) continue;
	//未被剔除,当前数i是素数,淘汰它的所有倍数
	for(int j=i+i;j<=N;j=j+i){
    
    
		flag[j]=true;
	}
}

There is an accelerated form, change j=i+i to j=i i , because 2 i, 3*i,..., (i-1)*i have been traversed before

for(int i=2;i<=N;i++){
    
    
	//被剔除了,直接pass
	if(flag[i]) continue;
	//未被剔除,当前数i是素数,淘汰它的所有倍数
	for(int j=i*i;j<=N;j=j+i){
    
    
		flag[j]=true;
	}
}
  • Angstrom sieve method selects the smallest prime factor of a certain number N
    This is the application of the Angstrom sieve method, using the Angstrom sieve method to select the smallest prime factor of a certain number N is based on the fact that the smallest prime factor of N ⇔ the smallest of N Smallest prime factor of factor N\Leftrightarrow Smallest factor of Nsmallest prime factor of NThe minimum factor of N
    The basic idea is exactly the same as the Angstrom screening method: if the current number is not screened, then it is the minimum prime factor of itself, and also the minimum prime factor of its multiple
//埃式筛选法筛选最小质因数基于一个事实:一个数的最小因数一定是他的最小质因数 
void prime(int x){
    
    
	long long j=0;
	for(long long i=2;i<=x;i++){
    
    
		if(flag[i]) continue;
		else {
    
    
			//记录i的最小质因子
			f[i]=i;
			flag[i]=true;	
		}
		//筛选出i的倍数
		for(j=i*i;j<=x;j=j+i){
    
    
			//i的小于i-1倍的数字都在之前被筛选过了,因为k<i的数字一定对他的i倍进行过筛选
			//所以本次筛选从i的i倍开始
			if(flag[j]) continue;
			flag[j]=true;
			//记录j的最小质因子
			f[j]=i;				 
		} 
	} 
}
  • Minimum prime sum
#include <bits/stdc++.h>
using namespace std; 
int n;
//dp[i]表示前i个的质因子之和 
vector<long long>dp;
//f[i]表示i的质因子 
vector<int> f;
//flag[i]表示i是否被筛选出 
vector<bool> flag;
//埃式筛选法求2~N中每个数的质因子 
void prime(int x); 
int main()
{
    
    		
	cin>>n;
	vector<int> a(n,0);
	for(int i=0;i<n;i++){
    
    
		cin>>a[i];
	}
	int m=*max_element(a.begin(),a.end());
	f=vector<int>(m+1,0);
	flag=vector<bool>(m+1,false);
	dp=vector<long long>(m+1,0);
	prime(m); 
	f[2]=2;
	dp[2]=2;
	int j=0;
	//动态规划自底向上
	for(int i=3;i<=m;i++){
    
    
		dp[i]=dp[i-1]+f[i];
	}
	for(int i=0;i<n;i++){
    
    
		cout<<dp[a[i]]<<endl;
	}
	return 0;
}
//埃式筛选法筛选最小质因数基于一个事实:一个数的最小因数一定是他的最小质因数 
void prime(int x){
    
    
	long long j=0;
	for(long long i=2;i<=x;i++){
    
    
		if(flag[i]) continue;
		else {
    
    
			f[i]=i;
			flag[i]=true;	
		}
		//筛选出i的倍数
		for(j=i*i;j<=x;j=j+i){
    
    
			//i的小于i-1倍的数字都在之前被筛选过了,因为k<i的数字一定对他的i倍进行过筛选
			//所以本次筛选从i的i倍开始
			if(flag[j]) continue;
			flag[j]=true;
			f[j]=i;				 
		} 
	} 
}

Guess you like

Origin blog.csdn.net/qq_33880925/article/details/129130109