# BZOJ 3930

## 题意

1N,K1e91LH1e9HL1e5$1\leq N,K\leq 1e9，1\leq L\leq H\leq 1e9，H-L\leq 1e5$.

## 推导

f(i)$f(i)$gcd=i$gcd=i$ 的方案总数， F(i)$F(i)$i|gcd$i|gcd$ 的方案总数，显然有

F(i)=i|dgcd(d)

f(i)=i|dμ(di)F(d)

f(K)=K|dμ(dK)F(d)=i=1HKμ(i)F(ik)

F$F$ 很好求，即

F(x)=(HKLK+1)n

F(x)=(HKL1K)n

# 51nod 1244

## 推导

d|nμ(d)=[n==1]

μ(n)+d|n,d<nμ(d)=[n==1]

μ(n)=[n==1]d|n,d<nμ(d)

i=1nμ(i)=i=1n[i==1]i=1nd|i,d<iμ(d)=1i=1nd|i,d<iμ(d)=1k=2nd=1nkμ(d)

M(n)=ni=1μ(i)$M(n)=\sum_{i=1}^{n}\mu(i)$，上式可化为
M(n)=1k=2nM(nk)

## 奇怪的(…)延伸

i=1nd|i,d<iμ(d)=k=2nd=1nkμ(d)

d=1ni=2ndμ(d)

=d=1nμ(d)i=2nd=d=1nμ(d)(nd1)

k=2nd=1nkμ(d)

[n==1]=d|nμ(d)

1=i=1n[i==1]=i=1nd|iμ(d)=d=1ni=1ndμ(d)=d=1nμ(d)i=2nd=d=1nμ(d)nd

d=1nμ(d)nd=1

## Code

#include <bits/stdc++.h>
#include <map>
#define maxn 10000010
#define maxm maxn + 10
using namespace std;
typedef long long LL;
map<LL, LL> sum;
int prime[maxm], mu[maxm];
bool check[maxm];
void init() {
int tot = 0; mu[1] = 1;
for (int i = 2; i <= maxn; ++i) {
if (!check[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot; ++j) {
if (i * prime[j] > maxn) break;
check[i * prime[j]] = true;
if (i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = -mu[i];
}
}
for (int i = 1; i <= maxn; ++i) mu[i] += mu[i - 1];
}
LL mu_sum(LL x) {
if (x <= maxn) return mu[x];
if (sum.find(x) != sum.end()) return sum[x];
LL le, ri, ret = 0;
for (LL i = 2; i <= x; i = ri + 1) {
le = i, ri = x / (x / i);
ret += (ri - le + 1) * mu_sum(x / i);
}
return sum[x] = 1 - ret;
}
LL a, b;
void work() {
printf("%lld\n", mu_sum(b) - mu_sum(a - 1));
}
int main() {
init();
while (scanf("%lld%lld", &a, &b) != EOF) work();
return 0;
}


# BZOJ 3930 续

## Code

#include <bits/stdc++.h>
#include <map>
#define maxn 10000010
#define maxm maxn + 10
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
map<LL, LL> sum;
int prime[maxm], mu[maxm];
bool check[maxm];
void init() {
int tot = 0; mu[1] = 1;
for (int i = 2; i <= maxn; ++i) {
if (!check[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot; ++j) {
if (i * prime[j] > maxn) break;
check[i * prime[j]] = true;
if (i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = -mu[i];
}
}
for (int i = 1; i <= maxn; ++i) mu[i] += mu[i - 1];
}
LL mu_sum(LL x) {
if (x <= maxn) return mu[x];
if (sum.find(x) != sum.end()) return sum[x];
LL le, ri, ret = 0;
for (LL i = 2; i <= x; i = ri + 1) {
le = i, ri = x / (x / i);
ret = (ret + (ri - le + 1) * mu_sum(x / i) + mod) % mod;
}
return sum[x] = (1 - ret + mod) % mod;
}
LL poww(LL a, LL b) {
LL ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
LL n, k, l, h;
LL F(LL d) { return poww(h / d - (l-1) / d, n); }
void work() {
LL hi = h / k;
LL ans = 0, le, ri;
for (LL i = 1; i <= hi; i = ri + 1) {
LL temp = i * k;
le = i, ri = min(h / (h / temp), (l-1) / temp ? (l-1) / ((l-1) / temp) : inf) / k;
ans = (ans + mod + (mu_sum(ri) - mu_sum(le - 1) + mod) % mod * F(temp) % mod) % mod;
}
ans = (ans + mod) % mod;
printf("%lld\n", ans);
}
int main() {
init();
while (scanf("%lld%lld%lld%lld", &n, &k, &l, &h) != EOF) work();
return 0;
}


# BZOJ 2301

## 题意

x=aby=cd[gcd(x,y)=k]

（低配版的BZOJ 3930）

## 推导

i=1nj=1m[gcd(i,j)=k]

i=1nkj=1mk[gcd(i,j)=1]
d=1min(nk,mk)μ(d)i=1nkdj=1mkd
d=1min(nk,mk)μ(d)nkdmkd

## Code

#include <bits/stdc++.h>
#define maxn 50000
typedef long long LL;
using namespace std;
int kas, prime[maxn + 10], mu[maxn + 10], pre[maxn];
bool check[maxn + 10];
void mobius() {
int tot = 0;
mu[1] = 1;
for (int i = 2; i <= maxn; ++i) {
if (!check[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot; ++j) {
if (i * prime[j] > maxn) break;
check[i * prime[j]] = true;
if (i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = -mu[i];
}
}
for (int i = 1; i <= maxn; ++i) pre[i] = pre[i - 1] + mu[i];
}
LL calc(int c, int d) {
if (c > d) swap(c, d);
LL ans = 0;
int le, ri;
for (int i = 1; i <= c; i = ri + 1) {
le = i, ri = min(c / (c / i), d / (d / i));
ans += 1LL * (pre[ri] - pre[le - 1]) * (c / i) * (d / i);
}
return ans;
}
void work() {
int a, b, c, d, k;
scanf("%d%d%d%d%d", &a, &c, &b, &d, &k);
LL tot1 = calc(c / k, d / k), tot2 = calc(c / k, (b-1) / k),
tot3 = calc((a-1) / k, d / k), tot4 = calc((a-1) / k, (b-1) / k);
printf("%lld\n", tot1 - tot2 - tot3 + tot4);
}
int main() {
mobius();
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客