Problem
- 给出 \(n\) ,统计满足以下条件的数对 \((a,b)\) 的个数:
1.\(1≤a<b≤n\)
2.\(a+b|ab\) - \(n<2^{31}\)
Solution
-
设 \(d=gcd(a,b),a=di,b=dj\),那么根据题意有:
-
\[(di+dj)|d^2ij \]
-
即:
-
\[(i+j)|ijd \]
-
又因为:
-
\[gcd(i,j)=1 \]
-
所以:
-
\[ij\%(i+j)!=0 \]
-
那么:
-
\[(i+j)|d \]
-
那么问题转化为求满足以下条件的三元组 \((i,j,d)\) 的个数:
1.\(gcd(i,j)=1\)
2.\(i<j\)
3.\(di,dj≤n\) -
\[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=\sum_{i=1}^{\sqrt{n}}\sum_{j=1}^{i-1}\lfloor{\frac{n}{i(i+j)}}\rfloor\sum_{k|gcd(i,j)}μ(k) \]
-
设 \(i=xk,j=yk\),那么:
-
\[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 \]
-
那么枚举 \(x,k\),然后对 \(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;
}