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 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 a
1, a
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
 
Source

这几周花了好多时间在看代码,虽然明知道是容斥原理,但是搜遍百度得到的答案也就是容斥原理四个字,呵呵,算我学艺不精,先把题解留在这里等以后再完善吧。

题目大意:给一个数组,求出这些数字两两互质或者两两不互质的情况的个数。
题目思路:毫无疑问暴力绝对超时。于是我们把问题转换成求两两搭配的个数减去正好一个数a与另一个数b互质,a同时和另一个数c不互质的情况的个数。但这样求
一个数a与另一个数b互质,a同时和另一个数c不互质的情况的个数这个问题还稍显复杂,我们想想两个数之间不就只有互质和不互质的关系吗(也就是最大公约数为1和最大公约数不为1)?好了,接下来是网上搜到的比较易懂的代码(有些大神的代码真是看得眼晕,凡是主函数低于5行的都转得我眼晕)。
代码因为是很久搜到的,现在已经无法查明出处了,在这里要向原作者道歉,也希望有人提示一下原博客的网址(话说有人看么)。
#include <cstdio>
#include <cstring>
#include<iostream>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 100005;
int a[maxn], cnt[maxn];
vector <int> tmp;
int main() {
int T, n, x;
scanf("%d", &T);
while(T--) {
long long ans = 0;
scanf("%d", &n);
memset(a, 0, sizeof(a));
memset(cnt, 0, sizeof(cnt));
//a[x]表示x的个数有多少个
for(int i = 0; i < n; i++) {
scanf("%d", &x);
a[x]++;
}
//cnt[i]表示数组中约数为i的个数
for(int i = 1; i < maxn; i++) {
for(int j = i; j < maxn; j += i) {
cnt[i] += a[j];
}
}
//tmp用来储存一个数字的所有质因数(好专业的样子)
for(int i = 1; i < maxn; i++) {
if(a[i]) {
tmp.clear();
int t = i;
int u = (int)sqrt(i);
for(int j = 2; j <= u; j++) {
if(t % j == 0) {
tmp.push_back(j);
while(t % j == 0) t /= j;
}
}
if(t > 1) tmp.push_back(t);
if(tmp.size() == 0) continue;
//接下来这个循环我就看不懂了
int nop = 0;
int v = tmp.size();
int nn = 1 << v;
for(int j = 1; j < nn; j++) {
int flag = 0;
int mul = 1;
for(int k = 0; k < v && j >> k; k++) {
if((j >> k) & 1) {
flag++;
mul *= tmp[k];
}
}
if(flag & 1) nop += cnt[mul]; else nop -= cnt[mul];
}
//nop大概表示不互质的个数,因为一个数本身也会和自己不互质,所以要减1
//ans很明显就是一个数a与另一个数b互质,a同时和另一个数c不互质的情况的个数了
ans += (long long)max(0, nop - 1) * (n - nop);
}
}
/*有些人会奇怪ans要除以2,其实ans实际上里面包括位置的不同的情况,就正如有三个位置,
a的位置已经固定在第一个不能改变,b和c因为两个不同的位置要算两种情况,但是我们并不需要
计较位置不同的情况,所以要除以2
*/
printf("%I64d\n", (long long)n * (n - 1) * (n - 2) / 6 - ans / 2);
}
return 0;
}
/*
1
5
1 3 9 10 2
ans:4
*/


hdu 5072 coprime不完整题解的更多相关文章

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

    题目链接:Coprime pid=5072"> 题面: Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  2. hdu 5072 Coprime

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出 n 个互不相同的数,求满足以下条件的三元无序组的个数:要么两两互质要么两两不互质. 思路:根据 ...

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

    pid=5072">http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了.感觉好可惜 ...

  4. hdu 5072 Coprime 容斥原理

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

  5. hdu 5072 Coprime (容斥)

    Problem Description There are n people standing in a line. Each of them has a unique id number. Now ...

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

    Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...

  7. Hdu 5072 Coprime(容斥+同色三角形)

    原题链接 题意选出三个数,要求两两互质或是两两不互质.求有多少组这样的三个数. 分析 同色三角形n个点 每两个点连一条边(可以为红色或者黑色),求形成的三条边颜色相同的三角形的个数反面考虑这个问题,只 ...

  8. HDU 5072 Coprime 同色三角形问题

    好吧,我承认就算当时再给我五个小时我也做不出来. 首先解释同色三角形问题: 给出n(n >= 3)个点,这些点中的一些被涂上了红色,剩下的被涂上了黑色.然后将这些点两两相连.于是每三个点都会组成 ...

  9. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

  10. HDU 4135 Co-prime(容斥:二进制解法)题解

    题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...

随机推荐

  1. dotnet 警惕使用 StackTrace 加获取方法标记 Attribute 特性在 Release 下被内联

    大家都知道,在 dotnet 里的 Debug 下和 Release 下的一个最大的不同是在 Release 下开启了代码优化.启用代码优化,将会对生成的 IL 代码进行优化,同时优化后的 IL 也会 ...

  2. WPF 推荐一个剪贴板内容查看工具

    本文来安利大家一个好用的 Windows 剪贴板的内容查看工具 这是在 GitHub 上完全免费开源的应用,由 walterlv 开发的应用,详细请看 https://github.com/walte ...

  3. dotnet 5 的 bin 文件夹下的 ref 文件夹是做什么用的

    本文来和大家聊聊在 dotnet 5 和 dotnet 6 或更高版本的 dotnet 构建完成,在 bin 文件夹下,输出的 ref 文件夹.在此文件夹里面,将会包含项目程序集同名的 dll 文件, ...

  4. 读 MAUI 源代码 理解可绑定对象和可绑定属性的存储机制

    和 UWP 与 WPF 不同的是在 MAUI 里面,使用可绑定对象 BindableObject 替换了依赖对象的概念,我阅读了 MAUI 的源代码发现其实只是命名变更了,里面的机制和设计思想都是差不 ...

  5. Anaconda环境下GPT2-Chinese的基本使用记录

    偶然在看到了这个项目,感觉很厉害,于是就折腾了下,跑了一跑 项目地址:https://github.com/Morizeyao/GPT2-Chinese 如果Github下载太慢的可以用这个代下载:h ...

  6. 超好用的 Redis GUI 工具,你值得拥有

    超好用的 Redis GUI 工具,你值得拥有 提供原生的性能,并且比使用 Electron 等 Web 技术开发的同等应用程序消耗的资源少得多. 下载地址:http://www.redisant.c ...

  7. 深入理解 C++ 中的多态与文件操作

    C++ 多态 多态(Polymorphism)是面向对象编程(OOP)的核心概念之一,它允许对象在相同操作下表现出不同的行为.在 C++ 中,多态通常通过继承和虚函数来实现. 理解多态 想象一个场景, ...

  8. 【2023最新B站评论爬虫】用python爬取上千条哔哩哔哩评论

    目录 一.爬取目标 二.展示爬取结果 三.爬虫代码 四.同步视频 五.附完整源码 您好,我是@马哥python说,一枚10年程序猿. 一.爬取目标 之前,我分享过一些B站的爬虫: [Python爬虫案 ...

  9. 01.Markdown 语法

    标题 # 一级标题 ## 二级标题 ### 三级标题 ...(最多六级标题) 字体 **hello**:粗体 *hello*:斜体 三个*:粗体+斜体 ~~hello~~:删除线 引用 > 引用 ...

  10. gin框架对接快递100 查询快递跟踪记录 Golang实现快递查询

    参考ui效果: https://www.kuaidi100.com/?from=openv gin框架: 请求地址 http://localhost:8822/kd100/auto_com_num?n ...