A. Slackline Adventure
考虑枚举每个矩形的长跟宽,再统计这个矩形能在坐标轴上出现几次(同行相邻同列相邻的单独算),然后有如下式子:
2
×
∑
i
=
1
n
−
1
∑
j
=
1
m
−
1
(
n
−
i
)
(
m
−
j
)
[
gcd
(
i
,
j
)
=
1
]
[
L
2
≤
i
2
+
j
2
≤
R
2
]
2 \times \sum_{i = 1} ^{n - 1} \sum_{j = 1} ^{m - 1}(n - i)(m - j)[\gcd(i, j) = 1][L ^ 2 \leq i ^ 2 + j ^ 2 \leq R ^ 2]\\
2×i=1∑n−1j=1∑m−1(n−i)(m−j)[gcd(i,j)=1][L2≤i2+j2≤R2]
设
F
(
n
,
m
,
T
2
)
=
∑
i
=
1
n
−
1
∑
j
=
1
m
−
1
(
n
−
i
)
(
m
−
j
)
[
gcd
(
i
,
j
)
=
1
,
i
2
+
j
2
≤
T
2
]
F(n, m, T ^ 2) = \sum\limits\limits_{i = 1} ^{n - 1} \sum\limits_{j = 1} ^{m - 1} (n - i)(m - j)[\gcd(i, j) = 1, i ^ 2 + j ^ 2 \leq T ^ 2]
F(n,m,T2)=i=1∑n−1j=1∑m−1(n−i)(m−j)[gcd(i,j)=1,i2+j2≤T2],则
a
n
s
=
F
(
n
,
m
,
R
2
)
−
F
(
n
,
m
,
L
2
−
1
)
ans = F(n,m, R ^ 2) - F(n, m, L ^ 2 - 1)
ans=F(n,m,R2)−F(n,m,L2−1)。
∑
k
=
1
n
−
1
μ
(
k
)
∑
i
=
1
n
−
1
k
∑
j
=
1
m
−
1
k
(
n
−
i
k
)
(
m
−
j
k
)
[
i
2
k
2
+
j
2
k
2
≤
T
2
]
∑
k
=
1
n
−
1
μ
(
k
)
∑
i
=
1
n
−
1
k
(
n
−
i
k
)
c
a
l
c
(
)
\sum_{k = 1} ^{n - 1} \mu(k) \sum_{i = 1} ^{\frac{n - 1}{k}} \sum_{j = 1} ^{\frac{m - 1}{k}} (n - ik)(m - jk)[i ^ 2 k ^ 2 + j ^ 2 k ^ 2 \leq T ^ 2]\\ \sum_{k = 1} ^{n - 1} \mu(k) \sum_{i = 1} ^{\frac{n - 1}{k}} (n - ik) calc()\\
k=1∑n−1μ(k)i=1∑kn−1j=1∑km−1(n−ik)(m−jk)[i2k2+j2k2≤T2]k=1∑n−1μ(k)i=1∑kn−1(n−ik)calc()
c
a
l
c
(
)
calc()
calc()就是当已知
i
,
k
,
T
i, k, T
i,k,T,有多少
j
j
j满足
i
2
k
2
+
j
2
k
2
≤
T
2
i ^ 2 k ^ 2 + j ^ 2 k ^ 2 \leq T ^ 2
i2k2+j2k2≤T2,的
m
−
j
k
m - jk
m−jk求和,整体复杂度
O
(
k
×
n
log
n
)
O(k \times n \log n)
O(k×nlogn),
k
k
k是个比较小的常数
今天VP的时候碰到的,到了一直没看这题,到了最后才发现是个数论题。
说一点点坑点吧,注意函数不能写成 f ( n , m , T ) = ∑ i = 1 n − 1 ∑ j = 1 m − 1 ( n − i ) ( m − j ) [ gcd ( i , j ) = 1 , i 2 + j 2 ≤ T 2 ] f(n, m, T) = \sum\limits\limits_{i = 1} ^{n - 1} \sum\limits_{j = 1} ^{m - 1} (n - i)(m - j)[\gcd(i, j) = 1, i ^ 2 + j ^ 2 \leq T ^ 2] f(n,m,T)=i=1∑n−1j=1∑m−1(n−i)(m−j)[gcd(i,j)=1,i2+j2≤T2],
然后答案是 f ( n , m , R ) − f ( n , m , L − 1 ) f(n, m, R) - f(n, m, L - 1) f(n,m,R)−f(n,m,L−1),这样会使满足条件得满足条件的一些数 ( L − 1 ) 2 < i 2 k 2 + j 2 k 2 < L 2 (L - 1) ^ 2 <i ^ 2 k ^ 2 + j ^ 2 k ^ 2 < L ^ 2 (L−1)2<i2k2+j2k2<L2,
没有被删去而算在贡献里了,会导致答案出错,就因为这个调了一晚上,一直不知道哪里错了,,,
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, mod = 1e9 + 7;
int prime[N], mu[N], cnt, n, m, L, R;
bool st[N];
inline int add(int x, int y) {
return x + y < mod ? x + y : x + y - mod;
}
inline int sub(int x, int y) {
return x >= y ? x - y : x - y + mod;
}
void init() {
mu[1] = 1;
for (int i = 2; i < N; i++) {
if (!st[i]) {
mu[i] = -1;
prime[++cnt] = i;
}
for (int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {
st[i * prime[j]] = 1;
if (i % prime[j] == 0) {
break;
}
mu[i * prime[j]] = -mu[i];
}
}
}
int calc(int i, int k, long long T) { // i * i * k * k + j * j * k * k <= T
int l = 0, r = (m - 1) / k;
while (l < r) {
int mid = l + r + 1 >> 1;
if (1ll * i * i * k * k + 1ll * mid * mid * k * k > T) {
r = mid - 1;
}
else {
l = mid;
}
}
return 1ll * (m - k + m - l * k) * l / 2 % mod;
}
int f(long long T) {
int ans = 0;
for (int k = 1, lim, res; k <= n - 1; k++) {
if (mu[k] == 0) {
continue;
}
lim = (n - 1) / k, res = 0;
for (int i = 1; i <= lim; i++) {
res = add(res, 1ll * (n - i * k) * calc(i, k, T) % mod);
}
if (mu[k] == 1) {
ans = add(ans, res);
}
else {
ans = sub(ans, res);
}
}
ans = 2ll * ans % mod;
if (T >= 1) {
ans = add(ans, add(1ll * n * (m - 1) % mod, 1ll * (n - 1) * m % mod));
}
return ans;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
init();
scanf("%d %d %d %d", &n, &m, &L, &R);
printf("%d\n", sub(f(1ll * R * R), f(1ll * L * L - 1)));
return 0;
}