Problem
- 给出
n
n
n ,统计满足以下条件的数对
(
a
,
b
)
(a,b)
(a,b) 的个数:
1. 1 ≤ a < b ≤ n 1≤a<b≤n 1≤a<b≤n
2. a + b ∣ a b a+b|ab a+b∣ab - n < 2 31 n<2^{31} n<231
Solution
-
设 d = g c d ( a , b ) , a = d i , b = d j d=gcd(a,b),a=di,b=dj d=gcd(a,b),a=di,b=dj,那么根据题意有:
-
( d i + d j ) ∣ d 2 i j (di+dj)|d^2ij (di+dj)∣d2ij
-
即:
-
( i + j ) ∣ i j d (i+j)|ijd (i+j)∣ijd
-
又因为:
-
g c d ( i , j ) = 1 gcd(i,j)=1 gcd(i,j)=1
-
所以:
-
i j % ( i + j ) ! = 0 ij\%(i+j)!=0 ij%(i+j)!=0
-
那么:
-
( i + j ) ∣ d (i+j)|d (i+j)∣d
-
那么问题转化为求满足以下条件的三元组 ( i , j , d ) (i,j,d) (i,j,d) 的个数:
1. g c d ( i , j ) = 1 gcd(i,j)=1 gcd(i,j)=1
2. i < j i<j i<j
3. d i , d j ≤ n di,dj≤n di,dj≤n -
a n s = ∑ i = 1 n ∑ j = 1 i − 1 ⌊ ⌊ n i ⌋ i + j ⌋ [ g c d ( i , j ) = 1 ] ans=\sum_{i=1}^{\sqrt{n}}\sum_{j=1}^{i-1}\lfloor{\frac{\lfloor{\frac{n}{i}}\rfloor}{i+j}}\rfloor [gcd(i,j)=1] ans=i=1∑nj=1∑i−1⌊i+j⌊in⌋⌋[gcd(i,j)=1]
-
a n s = ∑ i = 1 n ∑ j = 1 i − 1 ⌊ n i ( i + j ) ⌋ ∑ k ∣ g c d ( i , j ) μ ( k ) ans=\sum_{i=1}^{\sqrt{n}}\sum_{j=1}^{i-1}\lfloor{\frac{n}{i(i+j)}}\rfloor\sum_{k|gcd(i,j)}μ(k) ans=i=1∑nj=1∑i−1⌊i(i+j)n⌋k∣gcd(i,j)∑μ(k)
-
设 i = x k , j = y k i=xk,j=yk i=xk,j=yk,那么:
-
a n s = ∑ k = 1 n μ ( k ) ∑ x = 1 ⌊ n k ⌋ ∑ y = 1 x − 1 ⌊ ⌊ n x k 2 ⌋ x + y ⌋ ans=\sum_{k=1}^{\sqrt{n}}μ(k)\sum_{x=1}^{\lfloor{\frac{\sqrt{n}}{k}}\rfloor}\sum_{y=1}^{x-1}\lfloor{\frac{\lfloor{\frac{n}{xk^2}}\rfloor}{x+y}}\rfloor ans=k=1∑nμ(k)x=1∑⌊kn⌋y=1∑x−1⌊x+y⌊xk2n⌋⌋
-
那么枚举 x , k x,k x,k,然后对 x + y x+y x+y 整除分块即可。
Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int e = 1e6 + 5;
int n, mul[e];
bool bo[e];
ll ans;
inline ll solve(int x, int k)
{
int m = 2 * x, j, t = n / (x * k * k), i;
ll res = 0;
for (i = x + 1; i < m; i = j + 1)
{
if (!(t / i)) return res;
j = min(m - 1, t / (t / i));
if (j - i + 1 >= 0) res += (ll)(j - i + 1) * (t / i);
}
return res;
}
int main()
{
cin >> n;
int s = sqrt(n), x, k, i, j;
for (i = 1; i <= s; i++) mul[i] = 1;
for (i = 2; i <= s; i++)
if (!bo[i])
{
mul[i] = -1;
for (j = 2 * i; j <= s; j += i)
{
bo[j] = 1;
if (j / i % i == 0) mul[j] = 0;
else mul[j] *= -1;
}
}
for (k = 1; k <= s; k++)
if (mul[k])
for (x = 1; x * k * k <= n && x * k <= s; x++)
ans += mul[k] * solve(x, k);
cout << ans << endl;
return 0;
}