[HDU 5072] Coprime 莫比乌斯反演

题意

  ? 给定一个长度为 $n$ 的序列 $A = \left\{ a_1, a_2, ..., a_n \right\}$ .

  ? 问有多少个三元组 $(i, j, k)(i \ne j, i \ne k, j \ne k)$ , 满足 $\left\{ \begin{aligned} & a_i \perp a_j \\ & a_i \perp a_k \\ & a_j \perp a_k \end{aligned} \right.$ 或 $\left\{ \begin{aligned} & a_i \not \perp a_j \\ & a_i \not \perp a_k \\ & a_j \not \perp a_k \end{aligned} \right.$ .

  ? $1 \le n, a_i \le {10}^5$ .

预备知识

  ? $k | (x, y) \Leftrightarrow \left\{ \begin{aligned} & k | x \\ & k | y \end{aligned} \right.$

  ? 证明 (1) 必要性.

    ? $k | (x, y) , (x, y) | x, (x, y) | y \Rightarrow k | x, k | y$ .

  ? (2) 充分性.

    ? $\exists p, q \in \mathbb{Z}, ~s.t.~ px + qy = (x, y)$ .

    ? $k | x, k | y \Rightarrow k | px + qy \Rightarrow k | (x, y)?$ .

  ? $g_n = \sum_{d | n} f_n \Leftrightarrow f_n = \sum_{d | n} \mu(d) g(\frac{n}{d})$ .

  ? 由 $f_1, f_2, ..., f_n$ 求 $g_1, g_2, ..., g_n$ : 从小到大枚举 $d$ , 将 $f_d$ 加到 $g_d, g_{2d}, g_{3d}, ...$ .

  ? 由 $g_1, g_2, ..., g_n$ 求 $f_1, f_2, ..., f_n$ :

    ? 方法一 从小到大枚举 $d$ , 此时我们已经算出 $f_1, f_2, ..., f_d$ , 将 $g_{2d}, g_{3d}, ..., g_{kd}$ 减去 $f_d$ .

    ? 方法二 反演公式.

?

  ? $g_n = \sum_{n | d} f_d \Leftrightarrow f_n = \sum_{n | d} \mu(\frac{d}{n}) g(d)$

  ? 由 $f_1, f_2, ..., f_n$ 求 $g_1, g_2, ..., g_n$ : 枚举 $d$, 将 $f_d, f_{2d}, ...$ 加到 $g_d$ .

  ? 由 $g_1, g_2, ..., g_n$ 求 $f_1, f_2, ..., f_n$ :

    ? 方法一 从大到小枚举 $d$ , 将 $g_d$ 减去 $f_{2d}, f_{3d}, ...$ 得到 $f_d$ .

    ? 方法二 反演公式.

分析

  ? $n$ 个点的完全图, 边有两种颜色, 求同色三角形个数.

  ? 减法原理, $\binom{n}{3} - 不同色三角形个数$ .

  ? 枚举每个点, 算出该点除外的互质个数 $a$ , 该点除外的不互质个数 $b$ , 那么贡献 $a \times b$ .

  ? 发现每个不同色三角形, 我们恰好算了两次, 所以 $\frac{1}{2} (\sum a\times b)$ 即可.

  ? 当 $a_i = 1$ 时, 不产生贡献.

  ? 当 $a_i \ne 1$ 时, $该点除外的互质个数 = 互质个数 = a$ , $该点除外的不互质个数 = 不互质个数 - 1 = n - 1 - a$ .

  ? 给定 $a_1, a_2, ..., a_n$ , 求与 $x$ 互质的数的个数.

  ? 设 $c_d = \sum_{i} [a_i = d]$ , $f_x$ 为与 $x$ 互质的数的个数.

  $$\begin{aligned} f_x & = \sum{d} [d \perp x] c_d \\ & = \sum_{k | d, d | x}\mu(k)c_d \\ & = \sum_{k | x} \mu(k) \sum_{k | d} c_d \end{aligned}$$
  ? 枚举 $k$ , $O(\frac{n}{k})$ 求 $\sum_{k | d}c_d$ , 再 $O(\frac{n}{k})$ 对 $f_k, f_{2k}, ..., f_{pk}, ...$ 进行贡献.

  ? 总时间复杂度 $O(n \log n)$ .

?

实现

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>

#define F(i, a, b) for (register int i = (a); i <= (b); i++)

#define LL long long

const int N = 100005;
const int A = 100000;

bool v[N]; int p[N], tot, mu[N];
int n, a[N];
LL c[N], f[N];
LL res;

inline int rd(void) {
    int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -1;
    int x = 0; for (; isdigit(c); c = getchar()) x = x*10+c-‘0‘; return x*f;
}

int main(void) {
    #ifndef ONLINE_JUDGE
        freopen("hdu5072.in", "r", stdin);
        freopen("hdu5072.out", "w", stdout);
    #endif

    v[1] = true, mu[1] = 1;
    F(i, 2, A) {
        if (!v[i]) p[++tot] = i, mu[i] = -1;
        for (int j = 1; i * p[j] <= A && j <= tot; j++) {
            v[i * p[j]] = true;
            if (i % p[j] != 0) mu[i * p[j]] = -mu[i]; else break;
        }
    }

    for (int nT = rd(), t = 1; t <= nT; t++) {
        memset(a, 0, sizeof a);
        n = rd(); F(i, 1, n) a[i] = rd();

        memset(c, 0, sizeof c), memset(f, 0, sizeof f);
        F(i, 1, n) c[a[i]]++;
        F(k, 1, A) if (mu[k] != 0) {
            LL t = 0;
            for (int d = k; d <= A; d += k)
                t += c[d];
            t *= mu[k];
            for (int x = k; x <= A; x += k)
                f[x] += t;
        }

        res = 0;
        F(i, 1, n)
            if (a[i] != 1)
                res += f[a[i]] * (n - 1 - f[a[i]]);
        printf("%I64d\n", 1LL * n * (n-1) * (n-2) / 6 - res / 2);
    }

    return 0;
}
时间: 2024-10-13 11:11:53

[HDU 5072] Coprime 莫比乌斯反演的相关文章

HDU 5072 Coprime (莫比乌斯反演+容斥+同色三角形)

Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1469    Accepted Submission(s): 579 Problem Description There are n people standing in a line. Each of them has a unique id number. Now

hdu 5072 Coprime(数论)

题目链接:hdu 5072 Coprime 题目大意:给定N个数,问能选出多少个3元组,要么[(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1]. 解题思路:这题可以换个角度想,可以将三个数看做三角形的三条边,互质即边的颜色为1,否则为0,那么要求的即为 三条边颜色相同的三角形有多少个. 总的三角形的个数可求,那么如果求出三条边不完全相同的三角形个数,相减一下即可. 枚举顶点,然后确定以该点形成的

nyoj CO-PRIME 莫比乌斯反演

CO-PRIME 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 This problem is so easy! Can you solve it? You are given a sequence which contains n integers a1,a2……an, your task is to find how many pair(ai, aj)(i < j) that ai and aj is co-prime. 输入 There are multip

HDU 4746 Mophues (莫比乌斯反演应用)

Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others) Total Submission(s): 980    Accepted Submission(s): 376 Problem Description As we know, any positive integer C ( C >= 2 ) can be written as the multiply of

HDU - 6715 - 算术 = 莫比乌斯反演

http://acm.hdu.edu.cn/showproblem.php?pid=6715 题意: 求:\(\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\mu(lcm(i,j))\),其中n,m是1e6范围内,10组. 不会,想了很久,也不知道假在哪里.大概是一开始方向就错了. 正解: 所求:\(\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\mu(lcm(i,j))\) 即:\(\sum\limits_{i=1}^

hdu.5212.Code(莫比乌斯反演 &amp;&amp; 线性筛)

Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 300    Accepted Submission(s): 124 Problem Description WLD likes playing with codes.One day he is writing a function.Howerver,his computer b

ACM学习历程—HDU 5072 Coprime(容斥原理)

Description There are n people standing in a line. Each of them has a unique id number. Now the Ragnarok is coming. We should choose 3 people to defend the evil. As a group, the 3 people should be able to communicate. They are able to communicate if

hdu 5072 Coprime(同色三角形+容斥)

pid=5072">http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了.感觉好可惜的一道题,要是知道这个模型....就能够轻松的拿银了啊. . . 题意不再赘述,就是求同色三角形的个数.总的三角形的个数是C(n,3),仅仅需减去不同色的三角形就可以.对于每一个点(数),与它互质的连红边,不互质的连蓝边,那么对于该点不同色三角形个数为蓝边数*红边数/2,由于同一个三角形被计算了两次. 那么同

HDU 5072 Coprime (单色三角形+容斥原理)

题目链接:Coprime 题面: Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1181    Accepted Submission(s): 471 Problem Description There are n people standing in a line. Each of them has a uniq