Description
組詢問。
設 爲 的約數個數,給定 、,求
Solution
莫比烏斯反演即可:
設 。
預處理 , 函數前綴和。
整除分塊,時間複雜度
Code
//Dlove's template
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#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();
}