【51nod】1742 开心的小Q
题解
我们由于莫比乌斯函数如果有平方数因子就是0,那么我们可以列出这样的式子
\(\sum_{i = 1}^{n} \sum_{d|i} (1 - |\mu(d)|)\)
然后枚举倍数
\(\sum_{t = 1}^{n} \sum_{d = 1}^{\lfloor \frac{n}{t} \rfloor} (1 - |\mu(d)|)\)
\(\sum_{t = 1}^{n} F(\lfloor \frac{n}{t} \rfloor)\)
\(F(x)\)就表示1 - x有多少数有平方因子
可以用容斥得到
\(F(n) = n - \sum_{i = 1}^{\sqrt{n}}\mu(i) \lfloor \frac{n}{i^2}\rfloor\)
这个复杂度是\(n^{\frac{1}{3}}\)的,因为对于大于\(n^{\frac{1}{3}}\)的i,除数肯定小于\(n^{\frac{1}{3}}\)
然后我们的复杂度就是
\(O(\sqrt{n} + \sum_{i = 1}^{\sqrt{n}} (\frac{n}{i})^{\frac{1}{3}})\)可以解决问题
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <cmath>
#include <bitset>
#include <queue>
#define enter putchar('\n')
#define space putchar(' ')
//#define ivorysi
#define pb push_back
#define mo 974711
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define MAXN 200005
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 - '0' + c;
c = getchar();
}
res = res * f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int prime[100005],tot,mu[100005],M[100005];
bool nonprime[100005];
int F(int x) {
int res = 0;
for(int i = 1 ; i <= x / i ; ++i) {
int r = sqrt(x / (x / (i * i)));
if(x / ((r + 1) * (r + 1)) == x / (i * i)) ++r;
if(x / (r * r) > x / (i * i)) --r;
res += (x / (i * i)) * (M[r] - M[i - 1]);
i = r;
}
return x - res;
}
int64 Solve(int x) {
int64 res = 0;
for(int i = 1 ; i <= x ; ++i) {
int r = x / (x / i);
res += 1LL * (r - i + 1) * F(x / i);
i = r;
}
return res;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
mu[1] = 1;M[1] = 1;
for(int i = 2 ; i <= 100000 ; ++i) {
if(!nonprime[i]) {
prime[++tot] = i;
mu[i] = -1;
}
for(int j = 1 ; j <= tot ; ++j) {
if(prime[j] > 100000 / i) break;
nonprime[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
else mu[i * prime[j]] = -mu[i];
}
M[i] = M[i - 1] + mu[i];
}
int a,b;
read(a);read(b);
out(Solve(b) - Solve(a - 1));
enter;
}