P2257 YY的GCD
题意:
求 1≤x≤N,1≤y≤M1 \leq x \leq N,1 \leq y \leq M1≤x≤N,1≤y≤M 且gcd(x, y) 为质数的 (x,y) 有多少对。
题解:
莫比乌斯反演
代码:
#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...);
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 1e7 + 9;
ll mu[maxn];
int vis[maxn], prim[maxn], cnt, f[maxn];
int sum[maxn];
void get_mu(int n)
{mu[1]= 1;for (int i= 2; i <= n; i++) {if (!vis[i]) {prim[++cnt]= i;mu[i]= -1;}for (int j= 1; j <= cnt && prim[j] * i <= n; j++) {vis[prim[j] * i]= 1;if (i % prim[j] == 0)break;elsemu[i * prim[j]]= -mu[i];}}for (int i= 1; i <= cnt; i++) {for (int j= 1; prim[i] * j <= n; j++) {f[j * prim[i]]+= mu[j];}}for (int i= 1; i <= n; i++)sum[i]= sum[i - 1] + f[i];
}
ll solve(int a, int b)
{ll ans= 0;for (int l= 1, r= 0; l <= a; l= r + 1) {r= min(a / (a / l), b / (b / l));ans+= 1ll * (sum[r] - sum[l - 1]) * 1ll * (a / l) * (b / l);}return ans;
}
int main()
{//rd_test();get_mu(10000000);int t;read(t);while (t--) {int n, m;read(n, m);if (n > m)swap(n, m);printf("%lld\n", solve(n, m));}//Time_test();
}