P3455 [POI2007]ZAP-Queries

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3455 [POI2007]ZAP-Queries相关的知识,希望对你有一定的参考价值。

P3455 [POI2007]ZAP-Queries

题意:

求满足 1 ≤ x ≤ a , 1 ≤ y ≤ b 1\\leq x\\leq a,1\\leq y\\leq b 1xa,1yb,且 g c d ( x , y ) = d gcd(x,y)=d gcd(x,y)=d的二元组(x,y)的数量

题解:

莫比乌斯反演板子

代码:

// Problem: P3455 [POI2007]ZAP-Queries
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3455
// Memory Limit: 125 MB
// Time Limit: 2000 ms
// Data:2021-08-20 13:18:12
// By Jozky

#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= 5e4;
int cnt= 0;
int prim[maxn], vis[maxn], mu[maxn];
ll sum[maxn];
void get_mu(int n)
{
    mu[1]= 1;
    prim[1]= 1;
    for (int i= 2; i <= n; i++) {
        if (!vis[i]) {
            prim[++cnt]= i;
            mu[i]= -1;
        }
        for (int j= 1; j <= cnt && i * prim[j] <= n; j++) {
            vis[i * prim[j]]= 1;
            if (i % prim[j] == 0)
                break;

            mu[i * prim[j]]= -mu[i];
        }
    }
    for (int i= 1; i <= n; i++)
        sum[i]= sum[i - 1] + mu[i];
}
ll solve(ll a, ll b, ll d)
{
    ll ans= 0;
    for (ll l= 1, r= 0; l <= a; l= r + 1) {
        r= min(a / (a / l), b / (b / l));
        ans+= 1ll * (sum[r] - sum[l - 1]) * (a / l) * (b / l);
    }
    return ans;
}
int main()
{
    //rd_test();
    get_mu(50000);
    int t;
    read(t);
    while (t--) {
        ll a, b, d;
        read(a, b, d);
        if (a > b)
            swap(a, b);
        a/= d;
        b/= d;
        printf("%lld\\n", solve(a, b, d));
    }
    //Time_test();
}

以上是关于P3455 [POI2007]ZAP-Queries的主要内容,如果未能解决你的问题,请参考以下文章

luogu P3455 [POI2007]ZAP-Queries

P3455 [POI2007]ZAP-Queries(莫比乌斯反演)

模板 - 莫比乌斯反演

GCD HDU - 1695

GCD HDU - 1695

P2522 [HAOI2011]Problem b