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 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.
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 ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by a single space, where ai stands for the id number of the i-th person.
For each test case, the first line contains an integer n(3 ≤ n ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by a single space, where ai 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
题目原形是同色三角形, 引用:就是求同色三角形的个数。总的三角形的个数是C(n,3),只需减去不同色的三角形即可。对于每个点(数),与它互质的连红边,不互质的连蓝边,那么对于该点不同色三角形个数为蓝边数*红边数/2,因为同一个三角形被计算了两次。那么同色三角形个数为C(n,3) - ∑蓝边数*红边数/2。
问题是:如何求 原来序列里面的n个数跟某个数k不互质的个数(互质的就是n-k了)?
可以将原来的n个数,每一个都把他们的不同的质因数都求出来,然后枚举它们能够组合的数(1 << cnt),用一个数组num记录,每枚举到一个数,那么数组对应的就+1
对于数k,也把它的不同质因数求出来,同样枚举它能够组合的所有数t,然后奇加偶减num

#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <algorithm> #include <queue> #include <map> using namespace std; typedef long long ll; const int N = 200005; int p[N][15], vis[N], a[N], num[N]; int n; void Prime() { memset(vis, 0, sizeof vis); for(int i = 0; i < N; ++i) p[i][0] = 0; for(int i = 2; i < N; ++i) if(!vis[i]) { p[i][ ++p[i][0] ] = i; for(int j = i + i; j < N; j += i) { vis[j] = 1; p[j][ ++p[j][0] ] = i; } } p[0][ ++p[0][0] ] = 1; //考虑0的情况 } void init() { memset(num, 0, sizeof num); for(int k = 0; k < n; ++k) { int now = a[k]; int cnt = p[ now ][0]; for(int i = 1; i < (1 << cnt); ++i) { int t = 1; for(int j = 0; j < cnt; ++j) if((1 << j) & i) { t *= p[ now ][j + 1]; } num[t]++; } } } void solve() { ll ans = 0, res, sum = 0; ans = (ll)n * (n - 1) * (n - 2) / 6; //类型转换一下,避免爆掉 int tot = 0; for(int k = 0; k < n; ++k) { int now = a[k]; int cnt = p[now][0]; res = 0; for(int i = 1; i < (1 << cnt); ++i) { int t = 1, g = 0; for(int j = 0; j < cnt; ++j) if((1 << j) & i) { t *= p[ now ][j + 1]; g++; } if(g & 1) res += num[t]; else res -= num[t]; } if(res == 0) continue; sum += (res - 1) * (n - res); } printf("%lld\n", ans - sum / 2); } int main() { // freopen("in", "r", stdin); int _; scanf("%d", &_); Prime(); while(_ --) { scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%d", &a[i]); init(); solve(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧