P2257 YY的GCD
Posted Jozky86
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P2257 YY的GCD相关的知识,希望对你有一定的参考价值。
题意:
求 1 ≤ x ≤ N , 1 ≤ y ≤ M 1 \\leq x \\leq N,1 \\leq y \\leq M 1≤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 LOCAL
startTime= clock();
freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
endTime= 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;
else
mu[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();
}
以上是关于P2257 YY的GCD的主要内容,如果未能解决你的问题,请参考以下文章