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,那么要求的即为

三条边颜色相同的三角形有多少个。

总的三角形的个数可求,那么如果求出三条边不完全相同的三角形个数,相减一下即可。

枚举顶点,然后确定以该点形成的三角会形成多少个不满足三角形,即在1中选一条,0中选一条。这样的话一个不满足

三角形会被计算2次,ans / 2即可。

那么问题转换成求每个数互质或者不互质的数有多少个,将每个数差分质因子,统计每个包含质因子x的数有多少个,

最用计算每个数的时候,用容斥原理计算即可。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;
const int maxn = 1e5;

int np, pri[maxn + 5], vis[maxn + 5];
int k, fac[maxn + 5];

int v[maxn + 5];
vector<int> g[maxn + 5];

void prime_table(int n) {
    np = 0;
    for (int i = 2; i <= n; i++) {
        if (vis[i])
            continue;
        pri[np++] = i;
        for (int j = i * 2; j <= n; j += i)
            vis[j] = 1;
    }
}

void div_fac(int n, int& cnt) {
    cnt = 0;
    for (int i = 0; i < np && pri[i] * pri[i] <= n; i++) {
        if (n % pri[i] == 0) {
            fac[cnt++] = pri[i];
            while (n % pri[i] == 0)
                n /= pri[i];
        }
    }
    if (n != 1)
        fac[cnt++] = n;
}

void dfs (int u, int d, int idx) {
    if (d >= v[idx]) {
        if (u != 1)
            g[idx].push_back(u);
        return;
    }

    dfs(u, d + 1, idx);
    dfs(u * fac[d], d + 1, idx);
}

void init () {
    np = 0;
    memset(vis, 0, sizeof(vis));
    prime_table(maxn);

    for (int i = 2; i <= maxn; i++) {
        div_fac(i, v[i]);
        dfs(1, 0, i);
    }
}

int N, f[maxn + 5], c[maxn + 5];

void input () {
    int x;
    scanf("%d", &N);
    memset(c, 0, sizeof(c));
    memset(f, 0, sizeof(f));

    for (int i = 0; i < N; i++) {
        scanf("%d", &x);
        c[x]++;
    }
    for (int i = 2; i <= maxn; i++) {
        if (c[i] == 0)
            continue;
        for (int j = 0; j < g[i].size(); j++)
            f[g[i][j]] += c[i];
    }
}

typedef long long ll;

int count (int n) {
    int ret = 0;
    for (int i = 0; i < g[n].size(); i++) {
        int k = g[n][i];
        if (v[k]&1)
            ret += (f[k] - 1);
        else
            ret -= (f[k] - 1);
    }
    return ret;
}

ll solve () {
    ll ret = 0;
    for (int i = 2; i <= maxn; i++) {
        if (c[i] == 0)
            continue;
        ll k = count(i);
        ret += k * (N - 1 - k);
    }
    ll a = N;
    ll sum = 1LL * a * (a - 1) * (a - 2) / 6;
    return sum - ret / 2;
}

int main () {
    init();
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        input();
        printf("%I64d\n", solve());
    }
    return 0;
}
时间: 2024-12-03 16:03:22

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(同色三角形+容斥)

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

hdu 5072 Coprime

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

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 (容斥)

Problem 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 communi

hdu 5072 Coprime 容斥原理

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

HDU 5072 Coprime 同色三角形问题

好吧,我承认就算当时再给我五个小时我也做不出来. 首先解释同色三角形问题: 给出n(n >= 3)个点,这些点中的一些被涂上了红色,剩下的被涂上了黑色.然后将这些点两两相连,于是每三个点都会组成一个三角形, 即总共有sum = C(3,n)个三角形.对于一个三角形,如果三个点颜色一样则称其为同色三角形. 那么一个很直观的思路就是容斥,sum - 非同色三角形个数ans. ans = (sigma (Xi*Yi) ) / 2;(1 <= i <= n,Xi,Yi分别表示与第 i 个点相连的

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

我们先来介绍一下单色三角形问题,如下 单色三角形 在空间中给出了n个点.这些点任三点不共线,并且每两个点之间都有一条线相连,每一条线不是红的就是黑的.在这些点和线组成的三角形中,如果一个三角形的三条边的颜色都相同,那么我们就称这个三角形为单色三角形.现给出所有涂红色的线,试求出单色三角形的数目. 任务: 请写一个程序: 从文本文件中读入点数和对红色连线的描述: 找出该图中红色三角形的数目: 把结果输出到文件TRO.OUT中. 输入格式: 在文本文件TRO.IN的第一行包括一个整数n,3 <= n