ACM-素数专题(持续更新)

埃拉托斯特尼筛法,或者叫埃氏筛法(听上去似乎很高大上的样子)

#include
using namespace std;
typedef long long ll;

const int N = 100005;
bool prime[N];
void init(){
    for(int i=2;i


预处理每个数的所有质因数

#include
#include
using namespace std;
const int N = 100000 + 5;
vector prime_factor[N];
void init(){
    for(int i = 2; i < N; i ++){
        if(prime_factor[i].size() == 0){//如果i是质数 
            for(int j = i; j < N; j += i){
                prime_factor[j].push_back(i); 
            }
        }
    }
}
int main(){
    init();
}


比如预处理每个数的所有因数

#include
using namespace std;
const int N = 100000 + 5;
vector factor[N];
void init(){
    for(int i = 2; i < N; i ++){
        for(int j = i; j < N; j += i){
            factor[j].push_back(i);
        }
    }
}
int main(){
    init();
}


预处理每个数的质因数分解

#include
using namespace std;
const int N = 100000 + 5;
vector prime_factor[N];
void init(){
    int temp;
    for(int i = 2; i < N; i ++){
        if(prime_factor[i].size() == 0){
            for(int j = i; j < N; j += i){
                temp = j;
                while(temp % i == 0){
                    prime_factor[j].push_back(i);
                    temp /= i;
                }
            }
        }
    }
}
int main(){
    init();
}




转载于:https://www.cnblogs.com/pearfl/p/10733175.html

你可能感兴趣的:(ACM-素数专题(持续更新))