bzoj5244 [Fjwc2018]最大真因数 min_25筛

版权声明:虽然是个蒟蒻但是转载还是要说一声的哟 https://blog.csdn.net/jpwang8/article/details/85371445

Description


一个合数的真因数是指这个数不包括其本身的所有因数,例如6的正因数有1,2,3,6,其中真因数有1,2,3。一个合
数的最大真因数则是这个数的所有真因数中最大的一个,例如6的最大真因数为3。给定正整数l和r,请你求出l和r
之间(包括l和r)所有合数的最大真因数之和。

输入共一行,包含两个正整数l和r。保证l≤r。L,R<=5*10^9

Solution


min_25筛板子题

首先有一个比较显然的性质:若x为合数,那么x的最大真因数c满足 x c \frac{x}{c} 为质数,且 x c \frac{x}{c} 一定是最小质因子

注意到我们在min_25求g的时候有一个用最小质因子筛去合数的步骤,此时这些合数的最小质因子显然都是相同的,那么最大质因子之和就是他们的和除掉最小质因子了。注意还要减去本来就是质数的部分

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define rep(i,st,ed) for (register int i=st;i<=ed;++i)

typedef unsigned long long LL;
const int N=200005;

LL p[N],s[N],w[N],g[N],f[N],id1[N],id2[N],m,B;

bool np[N];

void pre_work(int n) {
	p[0]=0;
	rep(i,2,n) {
		if (!np[i]) p[++p[0]]=i,s[p[0]]=s[p[0]-1]+i;
		for (int j=1;j<=p[0]&&i*p[j]<=n;++j) {
			np[i*p[j]]=1;
			if (i%p[j]==0) break;
		}
	}
}

LL solve(LL n) {
	B=sqrt(n); pre_work(B);
	m=0; g[1]=0;
	for (LL i=1,j;i<=n;i=j+1) {
		w[++m]=n/i; j=n/w[m];
		(w[m]<=B)?(id1[w[m]]=m):(id2[j]=m);
		if (w[m]&1) f[m]=(LL)((w[m]-1)/2)*(w[m]+2);
		else f[m]=(LL)((w[m]+2)/2)*(w[m]-1);
		g[m]=0;
	}
	LL res=0;
	rep(j,1,p[0]) {
		for (int i=1;i<=m&&p[j]*p[j]<=w[i];++i) {
			int k=(w[i]/p[j])<=B?(id1[w[i]/p[j]]):(id2[n/(w[i]/p[j])]);
			if (i==1) res+=f[k]-s[j-1];
			f[i]-=p[j]*(f[k]-s[j-1]);
		}
	}
	return res;
}

int main(void) {
	LL L,R; scanf("%llu%llu",&L,&R);
	printf("%llu\n", solve(R)-solve(L-1));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/jpwang8/article/details/85371445