题意:D(n)定义为[1..n]中有多少个数,其因子个数正好是4个.给出n,求出D(n),n<=1e11.
x的因子正好4个 则除去1,x之外 n的因式分解形式中 只能为p^3,p*q,(p,q为不同的素数).
可以直接枚举n以内p^3个数.
x的因子正好4个 则除去1,x之外 n的因式分解形式中 只能为p^3,p*q,(p,q为不同的素数).
可以直接枚举n以内p^3个数.
对于p*q形式 枚举小的因子p(p<=sqrt(n) 则p<q的范围在<n/p之间 求出这段区间内的素数个数即可.
n<=1e11 套用模板prime counting function求[1,n]内素数个数即可.
#include<bits/stdc++.h>
typedef long long ll;
#define MAXN 100
#define MAXM 100010
#define MAXP 666666
#define MAX 10000010
#define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
#define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
#define isprime(x) (( (x) && ((x)&1) && (!chkbit(ar, (x)))) || ((x) == 2))
using namespace std;
namespace pcf{
long long dp[MAXN][MAXM];
unsigned int ar[(MAX >> 6) + 5] = {0};
int len = 0, primes[MAXP], counter[MAX];
void Sieve(){
setbit(ar, 0), setbit(ar, 1);
for (int i = 3; (i * i) < MAX; i++, i++){
if (!chkbit(ar, i)){
int k = i << 1;
for (int j = (i * i); j < MAX; j += k) setbit(ar, j);
}
}
for (int i = 1; i < MAX; i++){
counter[i] = counter[i - 1];
if (isprime(i)) primes[len++] = i, counter[i]++;
}
}
void init(){
Sieve();
for (int n = 0; n < MAXN; n++){
for (int m = 0; m < MAXM; m++){
if (!n) dp[n][m] = m;
else dp[n][m] = dp[n - 1][m] - dp[n - 1][m / primes[n - 1]];
}
}
}
long long phi(long long m, int n){
if (n == 0) return m;
if (primes[n - 1] >= m) return 1;
if (m < MAXM && n < MAXN) return dp[n][m];
return phi(m, n - 1) - phi(m / primes[n - 1], n - 1);
}
long long Lehmer(long long m){
if (m < MAX) return counter[m];
long long w, res = 0;
int i, a, s, c, x, y;
s = sqrt(0.9 + m), y = c = cbrt(0.9 + m);
a = counter[y], res = phi(m, a) + a - 1;
for (i = a; primes[i] <= s; i++) res = res - Lehmer(m / primes[i]) + Lehmer(primes[i]) - 1;
return res;
}
}
ll solve(ll n)
{
ll res=0;
for(int i=0;i<pcf::len;i++)
{
ll x=pcf::primes[i],y=n/x;
if(x*x>n)
break;
res+=(pcf::Lehmer(y)-pcf::Lehmer(x));
}
for(int i=0;i<pcf::len;i++)
{
ll x=pcf::primes[i];
if(x*x*x>n)
break;
res++;
}
return res;
}
int main()
{
ll n;
cin>>n;
pcf::init();
cout<<solve(n)<<endl;
return 0;
}