T T T 组询问。
设 d ( x ) d(x) d(x) 为 x x x 的约数个数,给定 N N N、 M M M,求 ∑ i = 1 N ∑ j = 1 M d ( i j ) \sum_{i=1}^N \sum_{j=1}^M d(ij) i=1∑Nj=1∑Md(ij)
1 ≤ n , m , T ≤ 50000 1\le n, m, T\le 50000 1≤n,m,T≤50000
莫比乌斯反演即可:
∑ n = 1 N ∑ m = 1 M d ( n m ) \sum_{n = 1}^{N} \sum_{m = 1}^{M} d(nm) n=1∑Nm=1∑Md(nm)
= ∑ n = 1 N ∑ m = 1 M ∑ a ∣ n ∑ b ∣ m [ g c d ( a , b ) = = 1 ] =\sum_{n = 1}^{N} \sum_{m = 1}^{M} \sum_{a|n} \sum_{b|m} [gcd(a,b) == 1] =n=1∑Nm=1∑Ma∣n∑b∣m∑[gcd(a,b)==1]
= ∑ n = 1 N ∑ m = 1 M ∑ a ∣ n ∑ b ∣ m ∑ d ∣ g c d ( a , b ) μ ( d ) =\sum_{n = 1}^{N} \sum_{m = 1}^{M} \sum_{a|n} \sum_{b|m} \sum_{d|gcd(a,b)} \mu (d) =n=1∑Nm=1∑Ma∣n∑b∣m∑d∣gcd(a,b)∑μ(d)
= ∑ d = 1 min { N , M } μ ( d ) ∑ a = 1 ⌊ N d ⌋ ∑ b = 1 ⌊ M d ⌋ ⌊ N a d ⌋ ⌊ M b d ⌋ =\sum_{d = 1}^{\min\{N,M\}} \mu (d) \sum_{a = 1}^{\left \lfloor \frac{N}{d}\right \rfloor} \sum_{b = 1}^{\left \lfloor \frac{M}{d}\right \rfloor} \left \lfloor \frac{N}{ad}\right \rfloor \left \lfloor \frac{M}{bd}\right \rfloor =d=1∑min{N,M}μ(d)a=1∑⌊dN⌋b=1∑⌊dM⌋⌊adN⌋⌊bdM⌋
= ∑ d = 1 min { N , M } μ ( d ) ∑ a = 1 ⌊ N d ⌋ ⌊ N a d ⌋ ∑ b = 1 ⌊ M d ⌋ ⌊ M b d ⌋ =\sum_{d = 1}^{\min\{N,M\}} \mu (d) \sum_{a = 1}^{\left \lfloor \frac{N}{d}\right \rfloor}\left \lfloor \frac{N}{ad}\right \rfloor \sum_{b = 1}^{\left \lfloor \frac{M}{d}\right \rfloor} \left \lfloor \frac{M}{bd}\right \rfloor =d=1∑min{N,M}μ(d)a=1∑⌊dN⌋⌊adN⌋b=1∑⌊dM⌋⌊bdM⌋设 g ( x ) = ∑ i = 1 x ⌊ x i ⌋ g(x) = \sum_{i=1}^x \left \lfloor \frac{x}{i} \right \rfloor g(x)=∑i=1x⌊ix⌋。
预处理 g ( x ) g(x) g(x), μ \mu μ 函数前缀和。
整除分块,时间复杂度 O ( T x ) O(T\sqrt x) O(Tx)
//Dlove's template
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R register
#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
#define sqr(_x) ((_x) * (_x))
#define Cmax(_a, _b) ((_a) < (_b) ? (_a) = (_b), 1 : 0)
#define Cmin(_a, _b) ((_a) > (_b) ? (_a) = (_b), 1 : 0)
#define Max(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define Min(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define Abs(_x) (_x < 0 ? (-(_x)) : (_x))
using namespace std;
namespace Dntcry
{
char Bs[1 << 22], *Ss = Bs, *Ts = Bs;
#define getchar() (Ss == Ts && (Ts = (Ss = Bs) + fread(Bs, 1, 1 << 22, stdin), Ss == Ts) EOF : *Ss++)
inline int read()
{
R int a = 0, b = 1; R char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) (c == '-') ? b = -1 : 0;
for(; c >= '0' && c <= '9'; c = getchar()) a = (a << 1) + (a << 3) + c - '0';
return a * b;
}
inline ll lread()
{
R ll a = 0, b = 1; R char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) (c == '-') ? b = -1 : 0;
for(; c >= '0' && c <= '9'; c = getchar()) a = (a << 1) + (a << 3) + c - '0';
return a * b;
}
const int Maxn = 50010;
int T, n, m;
int Pri[Maxn], tot, mu[Maxn];
int g[Maxn];
bool vis[Maxn];
int getv(R int x)
{
R int res = 0;
for(R int i = 1, next = 0; i <= x; i = next + 1)
{
next = x / (x / i);
res += (next - i + 1) * (x / i);
}
return res;
}
int Main()
{
mu[1] = 1;
for(R int i = 2; i <= 50000; i++)
{
if(!vis[i]) Pri[++tot] = i, mu[i] = -1;
for(R int j = 1; j <= tot && Pri[j] * i <= 50000; j++)
{
vis[i * Pri[j]] = 1;
if(i % Pri[j] == 0)
{
mu[i * Pri[j]] = 0;
break;
}
else mu[i * Pri[j]] = -mu[i];
}
mu[i] += mu[i - 1];
}
for(R int i = 1; i <= 50000; i++) g[i] = getv(i);
T = read();
while(T--)
{
n = read(), m = read();
if(n > m) swap(n, m);
R ll Ans = 0;
for(R int i = 1, next = 0; i <= n; i = next + 1)
{
next = Min(n / (n / i), m / (m / i));
Ans += 1ll * (mu[next] - mu[i - 1]) * g[n / i] * g[m / i];
}
printf("%lld\n", Ans);
}
return 0;
}
}
int main()
{
return Dntcry :: Main();
}