【BZOJ5244】【FJWC2018】—最大真因数(min_25筛)

传送门

开始没管这函数不是积性函数,暴力码完狂wawa不止
naiive!!naiive!!

考虑一个数xx的最大真因数就是xmin primexfrac{x}{min prime_x}
考虑在min25min_{25}第一个过程求gg的过程就是用最小质因子去筛

所以只需要求出被筛去得到数的和除以该质数即可

不会min25min_{25}的看这个

#include<bits/stdc++.h>
using namespace std;
#define gc getchar 
inline int read(){
    char ch=gc();
    int res=0,f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
} 
#define re register
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int N=100005;
int pr[N],tot,isp[N];
inline void init(cs int M=N-5){
    for(int i=2;i<=M;i++){
        if(!isp[i])pr[++tot]=i;
        for(int j=1;j<=tot&&i*pr[j]<=M;j++){
            isp[i*pr[j]]=1;
            if(i%pr[j]==0)break;
        }
    }
}
ll l,r,ans,ans1,ans2;
inline ll ksm(ll a,int b,ll res=1){
    for(;b;b>>=1,a=a*a)if(b&1)res=res*a;
    return res;
}
ll f1[N],f2[N];
inline ll C(ll x){return (x&1)?((x+1)/2*x):(x/2*(x+1));}
inline ll query(ll k){
    if(k<=1)return 0;
    int lim=sqrt(k);ll res=0;
    for(int i=1;i<=lim;i++)f1[i]=C(i)-1,f2[i]=C(k/i)-1;
    for(int p=2;p<=lim;p++){
        if(f1[p]==f1[p-1])continue;
        ll pre=f2[1];
        for(int i=1;i<=lim/p;i++)f2[i]-=(f2[i*p]-f1[p-1])*p;
        for(int i=lim/p+1;1ll*p*p*i<=k&&i<=lim;i++)f2[i]-=(f1[k/i/p]-f1[p-1])*p;
        for(int i=lim;i>=1ll*p*p;i--)f1[i]-=(f1[i/p]-f1[p-1])*p;
        res+=(pre-f2[1])/p;
    }
    return res;
}
int main(){
    init();
    scanf("%lld%lld",&l,&r);
    cout<<query(r)-query(l-1)<<'
'; 
}
原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328745.html