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 and only if their id numbers are pairwise coprime or pairwise not coprime.
In other words, if their id numbers are a, b, c, then they can communicate if
and only if [(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and
(b, c) ≠ 1], where (x, y) denotes the greatest common divisor of x and y.

We want to know how many 3-people-groups can be chosen from the n people.

Input

The first line contains an integer T (T ≤
5), denoting the number of the test cases.

For each test case, the first line contains an integer n(3 ≤ n ≤ 10 5),
denoting the number of people. The next line contains n distinct integers a1,
2, . . . , a n(1 ≤ a i ≤
10 5) separated by a single space, where a i stands
for the id number of the i-th person.

Output

For each test case, output the answer in a
line.

Sample Input

1

5

1 3 9 10 2

Sample Output

4

题目大意是在n个数里面取出三个数a, b, c,如果三者两两互质或者两两都不互质就加入计算。求总的取法数。

首先这两种情况都比较难算,于是考虑补集,其中有且仅有两个互质或者不互质。

这两种情况发现有个共同特征,就是有一个互质且有一个不互质,另外一种关系就随便了。

这样的话就是对于每一个a,计算和它互质的乘上和它不互质的。

但是这样还是有问题的:

因为对于如果(a, b) = 1 && (a, c) != 1,那么如果(b, c) = 1,发现对于数c,同样满足a的条件。

所以对于每一个数都会被计算两次,也就是每一个三元组都被计入了两次,最后结果需要除以2。

接下来就是解决如何计算对于一个a,和a互质以及不互质的数个数。

首先如果可以枚举a的质因子的话,比如p,那么所有p的倍数都是与a不互质的。

但是如果对于q,也加上q的倍数的话,会出现重复,就是pq的倍数被计算了两次。

所以这里计算的时候需要进行容斥。

考虑到这里的话,就不需要枚举a的因子了,只需要枚举所有数找倍数,用容斥判断这里的倍数个数是加还是减, 还是没有贡献。这里的容斥我用莫比乌斯系数完成的。

最后用C(n,
3)-ans/2即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define LL long long using namespace std; const int maxN = ;
int n, d[maxN], top;
LL f[maxN];
int prime[maxN], u[maxN], cnt;
bool vis[maxN]; void mobius()
{
memset(vis, false,sizeof(vis));
u[] = ;
cnt = ;
for(int i = ; i < maxN; i++)
{
if(!vis[i])
{
prime[cnt++] = i;
u[i] = -;
}
for(int j = ; j < cnt && (LL)i*prime[j] < maxN; j++)
{
vis[i*prime[j]] = true;
if(i%prime[j])
u[i*prime[j]] = -u[i];
else
{
u[i*prime[j]] = ;
break;
}
}
}
} void input()
{
memset(d, , sizeof(d));
scanf("%d", &n);
int tmp;
top = ;
for (int i = ; i < n; ++i)
{
scanf("%d", &tmp);
d[tmp]++;
top = max(top, tmp);
}
} void work()
{
LL ans = ;
int num;
memset(f, , sizeof(f));
for (int i = ; i <= top; ++i)
{
num = ;
for (int j = i; j <= top; j += i)
num += d[j];
for (int j = i; j <= top; j += i)
f[j] += u[i]*(-num);
}
for (int i = ; i <= top; ++i)
ans += d[i]*f[i]*(n-f[i]-);
ans = (LL)n*(n-)*(n-)/-ans/;
printf("%I64d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
mobius();
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
input();
work();
}
return ;
}

ACM学习历程—HDU 5072 Coprime(容斥原理)的更多相关文章

  1. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  2. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  3. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  4. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

  5. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

  6. hdu 5072 Coprime 容斥原理

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  7. ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)

    Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = ...

  8. ACM学习历程—HDU 5317 RGCDQ (数论)

    Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...

  9. ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)

    Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...

随机推荐

  1. windows监控 排查蓝屏问题

    DUMP包分析 工具WinDbg,下载打开,按ctrl+S  输入:SRV*C:\websymbols*http://msdl.microsoft.com/download/symbols:点击OK, ...

  2. 【BZOJ3747】[POI2015]Kinoman 线段树

    [BZOJ3747][POI2015]Kinoman Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第 ...

  3. 【BZOJ2119】股市的预测 后缀数组+分块

    [BZOJ2119]股市的预测 Description 墨墨的妈妈热爱炒股,她要求墨墨为她编写一个软件,预测某只股票未来的走势.股票折线图是研究股票的必备工具,它通过一张时间与股票的价位的函数图像清晰 ...

  4. 频繁加载、删除swf造成flash崩溃解决办法

    最近在项目中遇到flash崩溃问题,经分析,都是在swfobject.embedSWF这一步卡死,页面及flash均无反应.   造成此问题的场景是,在同一根节点上频繁清空节点.调用swfobject ...

  5. 九度OJ 1250:矩阵变换 (矩阵运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:95 解决:47 题目描述: 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩 ...

  6. Struts中类型转换踩的坑

    出现的异常: 当我输入的数据很大时候,转换后如上,这并不是我想要的, 出现问题的原因: Struts2对常用的数据类型如String.Integer.Double等都添加了转换器进行对应的转换操作. ...

  7. 解决iOS11 UIScrollView下移问题

    iOS11 系统为UIScrollView增加一个contentInsetAdjustmentBehavior属性,默认为UIScrollViewContentInsetAdjustmentAutom ...

  8. jauery table

    $("#tableData tr:gt(0)").each(function() { }//橘色部分是查找id为tableData的DataTable里面除第一行以外的行

  9. Linux命令:grep,报错Binary file (standard input) matches

    在Linux使用grep命令,从文件中抓取显示特定的信息,如下: cat 文件名 | grep 特定条件 --->   cat xxxx | grep 12345 结果报错:Binary fil ...

  10. 牛客小白月赛1 D 多项式乘法 【循环】

    题目链接 https://www.nowcoder.com/acm/contest/85/D 思路 因为数据范围较小 ,所以 可以直接 一个一个乘 AC代码 #include <cstdio&g ...