【hdu1695】GCD(懵逼乌斯反演)

Description

bi=1dj=1[(i,j)=k]

Solution

考虑将 b,d 同时除 k ,然后求互质数对的数量。

f(x)=i=1nj=1m[gcd(i,j)=x]

F(x)=i=1nj=1m[x|gcd(i,j)]=nxmx

F(x)=x|df(d)

f(x)=x|dF(d)μ(dx)

直接算就行了,而且不用分块都可以过。。

但是计算时注意题目中数对是无序的!!

Code

暴算

/**************************
 * Au: Hany01
 * Date: Jan 17th, 2018
 * Prob: hdu1695
 * Email: [email protected]
**************************/

#include

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register LL i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register LL i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register LL i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline LL read()
{
    register LL _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("hdu1695.in", "r", stdin);
    freopen("hdu1695.out", "w", stdout);
#endif
}

const LL maxn = 100005;

LL T, a, b, c, d, k, mu[maxn], sum[maxn], pr[maxn], np[maxn], cnt, Case;

inline void Get_mu()
{
    mu[1] = 1;
    For(i, 2, maxn - 5) {
        if (!np[i]) pr[++ cnt] = i, mu[i] = -1;
        for (register LL j = 1; j <= cnt && pr[j] * i <= maxn - 5; ++ j) {
            np[i * pr[j]] = 1;
            if (!(i % pr[j])) { mu[pr[j] * i] = 0; break; }
            mu[pr[j] * i] = -mu[i];
        }
    }
    For(i, 1, maxn - 5) sum[i] = sum[i - 1] + mu[i];
}

inline LL Calc(LL n, LL m)
{
    if (n > m) swap(n, m);
    if (!n) return 0;
    register LL Ans = 0, tmp = 0;
    For(i, 1, n) Ans += mu[i] * (n / i) * 1ll * (m / i);
    For(i, 1, n) tmp += mu[i] * (n / i) * 1ll * (n / i);
    return Ans - (tmp >> 1);
    if (n < m) Ans -= (Calc(n, n) - 1) >> 1;
    return Ans;
}

int main()
{
    File();
    Get_mu();
    T = read();
    while (T --) {
        cout << "Case " << ++ Case << ": " ;
        a = read(), b = read(), c = read(), d = read(), k = read();
        if (!k) { puts("0"); continue; }
        b /= k, d /= k;
        cout << Calc(b, d) << endl;
    }
    return 0;
}
//金风玉露一相逢,便胜却人间无数。
//    -- 秦观《鹊桥仙·纤云弄巧》

整除分块

/**************************
 * Au: Hany01
 * Date: Jan 17th, 2018
 * Prob: hdu1695
 * Email: [email protected]
**************************/

#include

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("hdu1695.in", "r", stdin);
    freopen("hdu1695.out", "w", stdout);
#endif
}

const int maxn = 100005;

int T, a, b, c, d, k, mu[maxn], sum[maxn], pr[maxn >> 1], np[maxn], cnt, Case;

inline void Get_mu()
{
    mu[1] = 1;
    For(i, 2, maxn - 5) {
        if (!np[i]) pr[++ cnt] = i, mu[i] = -1;
        for (register int j = 1; j <= cnt && pr[j] * i <= maxn - 5; ++ j) {
            np[i * pr[j]] = 1;
            if (!(i % pr[j])) { mu[pr[j] * i] = 0; break; }
            mu[pr[j] * i] = -mu[i];
        }
    }
    For(i, 1, maxn - 5) sum[i] = sum[i - 1] + mu[i];
}

inline LL Calc(int n, int m)
{
    if (n > m) swap(n, m);
    register LL Ans = 0;
    for (register int i = 1, j; i <= n; i = j + 1)
        j = min(n / (n / i), m / (m / i)),
        Ans += (n / i) * 1ll * (m / i) * 1ll * (sum[j] - sum[i - 1]);
    if (n < m) Ans -= Calc(n, n); else Ans >>= 1;
    return Ans;
}

int main()
{
    File();
    Get_mu();
    T = read();
    while (T --) {
        cout << "Case " << ++ Case << ": " ;
        a = read(), b = read(), c = read(), d = read(), k = read();
        if (!k) { puts("0"); continue; }
        b /= k, d /= k;
        if (!b || !d) { puts("0"); continue; }
        cout << Calc(b, d) + (b == d ? 1 : 0) << endl;
    }
    return 0;
}
//野旷天低树,江清月近人。
//    -- 孟浩然《宿建德江》

你可能感兴趣的:(莫比乌斯反演,HDU,数论)