欧拉,素数,莫比乌斯反演函数筛法

欧拉,素数,莫比乌斯反演函数筛法

此筛法依据原理:每个合数肯定能被自己的最小素因子筛到,即val=素数*数。若没被筛到则为素数

#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#include<iomanip>
#include<stack>
#include<vector>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=100000;
const int branch=26;
const int inf=0x3f3f3f3f;
const int MOD=10007;
bool check[maxn+10];
int prime[maxn+10],mu[maxn+10];
int phi[maxn+10];
void init()//欧拉 素数 莫比乌斯反演函数
{
    mset(check,0);
    mu[1]=1;
    phi[1]=1;
    int tot=0;
    for(int i=2;i<=maxn;++i)
    {
        if(!check[i])
        {
            prime[tot++]=i;
            phi[i]=i-1;
            mu[i]=-1;
        }
        for(int j=0;j<tot;j++)
        {
            if(i*prime[j]>maxn) break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0)
            {
                mu[i*prime[j]]=0;
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
            else
            {
                mu[i*prime[j]]=-mu[i];
                phi[i*prime[j]]=phi[i]*phi[prime[j]];//积性函数的性质
            }
        }
    }
}
posted @ 2018-12-26 15:55  _年少有为  阅读(206)  评论(0编辑  收藏  举报