http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1222
求([a,b])中的个数转化为求([1,b])中的个数减去([1,a))中的个数。
[egin{aligned}
&sum_{i=1}^nsum_{j=1}^nleft[frac{ij}{(i,j)}leq n
ight]\
=&sum_{d=1}^nsum_{i=1}^{leftlfloorfrac nd
ight
floor}sum_{j=1}^{leftlfloorfrac nd
ight
floor}[(i,j)=1][ijdleq n]\
=&sum_{d=1}^nsum_{i=1}^{leftlfloorfrac nd
ight
floor}sum_{j=1}^{leftlfloorfrac nd
ight
floor}sum_{d'|(i,j)}mu(d')[ijdleq n]\
=&sum_{d=1}^nsum_{d'=1}^{leftlfloorfrac nd
ight
floor}mu(d')sum_{i=1}^{leftlfloorfrac n{dd'}
ight
floor}sum_{j=1}^{leftlfloorfrac n{dd'}
ight
floor}left[ijdd'^2leq n
ight]\
=&sum_{d'=1}^{leftlfloorsqrt n
ight
floor}mu(d')sum_{d=1}^{leftlfloorfrac n{d'}
ight
floor}sum_{i=1}^{leftlfloorfrac n{dd'}
ight
floor}sum_{j=1}^{leftlfloorfrac n{dd'}
ight
floor}left[ijdleqleftlfloorfrac n{d'^2}
ight
floor
ight]\
end{aligned}
]
枚举(leftlfloorsqrt n
ight
floor)个(d'),对于每个(d'),先假定(dleq ileq j),枚举(leftlfloorleft(frac n{d'}
ight)^{frac 13}
ight
floor)个(d),(i)再从(d)枚举到(leftlfloorleft(frac n{dd'}
ight)^{frac 12}
ight
floor),最后计算(j)的个数,乘上一个排列数即可。
(f(x))表示对每个(d')计算的复杂度。
时间复杂度为(Oleft(sumlimits_{d'=1}^{leftlfloorsqrt n
ight
floor}f(d')
ight))。
[egin{aligned}
f(x)=&int_{0}^{left(frac n{x^2}
ight)^{frac 13}}left(left(frac n{x^2i}
ight)^{frac 12}-i
ight)di\
=&left(frac n{x^2}
ight)^{frac 12}int_0^{left(frac n{x^2}
ight)^{frac 13}}i^{-frac 12}di-int_0^{left(frac n{x^2}
ight)^{frac 13}}idi\
=&left(frac n{x^2}
ight)^{frac 12}left(2left({left(frac n{x^2}
ight)^{frac 13}}
ight)^{frac 12}-2
ight)-left(frac 12left({left(frac n{x^2}
ight)^{frac 13}}
ight)^2
ight)\
=&frac 34left(frac n{x^2}
ight)^{frac 23}-2left(frac n{x^2}
ight)^{frac 12}
end{aligned}
]
[egin{aligned}
&Oleft(sum_{d'=1}^{leftlfloorsqrt n
ight
floor}f(d')
ight)\
=&Oleft(int_0^{sqrt n}left(frac n{x^2}
ight)^{frac 23}dx
ight)\
=&Oleft(n^{frac 23}int_0^{sqrt n}x^{-frac 43}dx
ight)\
=&Oleft(n^{frac 23}left(left(-3sqrt{n}^{-frac 13}
ight)-left(-3 imes0^{-frac 13}
ight)
ight)
ight)\
=&Oleft(3n^{frac 23}-3n^{frac 12}
ight)\
=&Oleft(n^{frac 23}
ight)
end{aligned}
]
所以总时间复杂度为(Oleft(n^{frac 23} ight)),好神奇啊qwq
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int sq = 316228;
bool notp[sq + 1];
int mu[sq + 1], prime[sq], num = 0;
void Euler_shai() {
mu[1] = 1;
for (int i = 2; i <= sq; ++i) {
if (!notp[i]) prime[++num] = i, mu[i] = -1;
for (int j = 1; j <= num && prime[j] * i <= sq; ++j) {
notp[prime[j] * i] = true;
if (i % prime[j] == 0)
break;
mu[prime[j] * i] = -mu[i];
}
}
}
ll a, b;
ll cal(ll n) {
ll ret = 0;
for (int d = 1; 1ll * d * d <= n; ++d)
if (mu[d]) {
ll up = n / d / d, r = 0;
for (int i = 1; 1ll * i * i * i <= up; ++i) {
ll up2 = up / i;
r += (up2 / i - i) * 3 + 1;
for (int j = i + 1; 1ll * j * j <= up2; ++j)
r += (up2 / j - j) * 6 + 3;
}
mu[d] > 0 ? ret += r : ret -= r;
}
return (ret + n) >> 1;
}
int main() {
scanf("%lld%lld", &a, &b);
Euler_shai();
printf("%lld
", cal(b) - cal(a - 1));
return 0;
}