莫比乌斯反演
第一道莫比乌斯反演。。
尝试按套路推了公式,可以发现其实就是求莫比乌斯函数与k的倍数个数的乘积,预处理前缀和即可。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 50005;
int _ ,a ,b, d, tot, mo[N], prime[N], sum[N];
bool vis[N];
void mobius(){
full(vis, false);
mo[1] = 1;
for(int i = 2; i <= N; i ++){
if(!vis[i]){
prime[++tot] = i, mo[i] = -1;
}
for(int j = 1; j <= tot && i * prime[j] <= N; j ++){
vis[i * prime[j]] = true;
if(i % prime[j] == 0){
mo[i * prime[j]] = 0;
break;
}
else mo[i * prime[j]] = -mo[i];
}
}
for(int i = 1; i <= N; i ++)
sum[i] = sum[i - 1] + mo[i];
}
int main(){
mobius();
for(_ = read(); _; _ --){
a = read(), b = read(), d = read();
a /= d, b /= d;
if(a > b) swap(a, b);
int ans = 0;
for(int l = 1, r; l <= a; l = r + 1){
r = min(a / (a / l), b / (b / l));
ans += (a / l) * (b / l) * (sum[r] - sum[l - 1]);
}
printf("%d
", ans);
}
return 0;
}