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.

 
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.

 
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 = ; int p[N][], vis[N], a[N], num[N];
int n;
void Prime()
{
memset(vis, , sizeof vis);
for(int i = ; i < N; ++i) p[i][] = ;
for(int i = ; i < N; ++i) if(!vis[i]) {
p[i][ ++p[i][] ] = i;
for(int j = i + i; j < N; j += i) {
vis[j] = ;
p[j][ ++p[j][] ] = i;
}
}
p[][ ++p[][] ] = ; //考虑0的情况
} void init()
{
memset(num, , sizeof num);
for(int k = ; k < n; ++k)
{
int now = a[k];
int cnt = p[ now ][];
for(int i = ; i < ( << cnt); ++i)
{
int t = ;
for(int j = ; j < cnt; ++j) if(( << j) & i) {
t *= p[ now ][j + ];
}
num[t]++;
}
}
} void solve()
{
ll ans = , res, sum = ;
ans = (ll)n * (n - ) * (n - ) / ; //类型转换一下,避免爆掉
int tot = ;
for(int k = ; k < n; ++k)
{
int now = a[k];
int cnt = p[now][];
res = ;
for(int i = ; i < ( << cnt); ++i)
{
int t = , g = ;
for(int j = ; j < cnt; ++j) if(( << j) & i) {
t *= p[ now ][j + ];
g++;
}
if(g & ) res += num[t];
else res -= num[t];
} if(res == ) continue;
sum += (res - ) * (n - res);
}
printf("%lld\n", ans - sum / ); }
int main()
{
// freopen("in", "r", stdin);
int _;
scanf("%d", &_);
Prime();
while(_ --)
{
scanf("%d", &n);
for(int i = ; i < n; ++i) scanf("%d", &a[i]);
init();
solve();
}
}

hdu 5072 Coprime 容斥原理的更多相关文章

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

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

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

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

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

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

  4. HDU 4135 Co-prime(容斥原理)

    Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...

  5. hdu 5072 Coprime

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

  6. hdu 5072 Coprime (容斥)

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

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

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

  8. hdu 5072 计数+容斥原理

    /* 题意: 给出n个数(n<100000), 每个数都不大于100000,数字不会有重复.现在随意抽出3个,问三个彼此互质 或者 三个彼此不互质的数目有多少. 思路: 这道题反着想,就是三个数 ...

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

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

随机推荐

  1. iOS-JavaScript向WKWebView传值

    一.本地代码所需操作 1.创建viewController并遵守协议 @interface ViewController ()<WKNavigationDelegate,WKScriptMess ...

  2. UVa1593_Allgnment_Of_Code

    /** start: integer; // begins hear stop: integer; // ends here s: string; c: char; // temp **/ //测试数 ...

  3. ASP.NET MVC中解决日志并发处理log4net

    本章主要内容是将异常信息写到队列中,然后通过线程写到文本文件中,速度非常快,没有阻塞和延迟加载 1.首先在Model中建一个类MyExceptionAttribute.cs public class ...

  4. 浅谈我的编程之路——感谢引领我的leader

    在开发的道路上,就始终无法避开版本控制,哪怕你是独自一人进行开发,版本控制也是有必要的,从最早开始使用CVS,到后来使用SVN,再到git,最后又回到了SVN,但是不知道为什么真的对SVN很无爱. 现 ...

  5. set -x 跟踪脚本执行信息

    注意set -x其中"-"与"x"之间没有空格 [root@GitLab sh]# ./sx.sh heelo + a=heelo + echo heelo h ...

  6. haproxy 安装 各个参数的测试

    ---------------------------------------------------------------------------------------------------- ...

  7. @ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4 "/" $NF}'中"[ :]+" 是什么意思?@

    http://blog.csdn.net/zhuying_linux/article/details/6822987

  8. Redis不同数据类型的的数据结构实现

    我们知道Redis支持五种数据类型, 分别是字符串.哈希表(map).列表(list).集合(set)和有序集合,和Java的集合框架类似,不同数据类型的数据结构实也是不一样的. >>Re ...

  9. windows操作技巧

    -------------------win7专有------------------- WIN+上下左右可以将当前窗口 最大化.左侧放置.右侧放置.最小化 WIN+Shift+左右是让窗口在多个屏幕 ...

  10. [LeetCode] Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...