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
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.
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.
题目原形是同色三角形, 引用:就是求同色三角形的个数。总的三角形的个数是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 容斥原理的更多相关文章
- HDU 5072 Coprime (单色三角形+容斥原理)
题目链接:Coprime pid=5072"> 题面: Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- ACM学习历程—HDU 5072 Coprime(容斥原理)
Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...
- hdu 5072 Coprime(同色三角形+容斥)
pid=5072">http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了.感觉好可惜 ...
- HDU 4135 Co-prime(容斥原理)
Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...
- hdu 5072 Coprime
http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出 n 个互不相同的数,求满足以下条件的三元无序组的个数:要么两两互质要么两两不互质. 思路:根据 ...
- hdu 5072 Coprime (容斥)
Problem Description There are n people standing in a line. Each of them has a unique id number. Now ...
- Hdu 5072 Coprime(容斥+同色三角形)
原题链接 题意选出三个数,要求两两互质或是两两不互质.求有多少组这样的三个数. 分析 同色三角形n个点 每两个点连一条边(可以为红色或者黑色),求形成的三条边颜色相同的三角形的个数反面考虑这个问题,只 ...
- hdu 5072 计数+容斥原理
/* 题意: 给出n个数(n<100000), 每个数都不大于100000,数字不会有重复.现在随意抽出3个,问三个彼此互质 或者 三个彼此不互质的数目有多少. 思路: 这道题反着想,就是三个数 ...
- HDU 5072 Coprime 同色三角形问题
好吧,我承认就算当时再给我五个小时我也做不出来. 首先解释同色三角形问题: 给出n(n >= 3)个点,这些点中的一些被涂上了红色,剩下的被涂上了黑色.然后将这些点两两相连.于是每三个点都会组成 ...
随机推荐
- HDU 4946 Area of Mushroom (几何凸包)
题目链接 题意:给定n个人,每个人有一个速度v方向任意.如果平面中存在一个点只有某个人到达的时间最短(即没有人比这个人到的时间更短或相同),那么我们定义这个店归这个人管辖,现在问这些人中哪些人的管辖范 ...
- 35. Search Insert Position
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 赛车比赛(洛谷U4566)
题目背景 kkk在赛车~ 题目描述 现在有N辆赛车行驶在一条直线跑道(你可以认为跑道无限长)上.它们各自以某种速度匀速前进,如果有两辆车A车和B车,A车在B车的后面,且A车的速度大于B车的速度,那么经 ...
- C# 泛型约束
一.泛型简介1.1泛型通过使用泛型,可以创建这样的类.接口和方法,它们以一种类型安全的工作方式操作各种数据.本质上,术语“泛型”指的是“参数化类型”(parameterized types).参数化类 ...
- PHP数据库操作
PHP实现数据库的增删改查 <?php $conn=mysql_connect('localhost','root','root'); if(!$conn){ echo "connec ...
- hdu 1290 切糕
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1290 思路: n条直线最多能将一个平面分成几个区域其递推公式即为:f(n)=f(n-1)+n:递推一下 ...
- 理解Java中的引用传递和值传递
关于Java传参时是引用传递还是值传递,一直是一个讨论比较多的话题,有论坛说Java中只有值传递,也有些地方说引用传递和值传递都存在,比较容易让人迷惑.关于值传递和引用传递其实需要分情况看待,今天学习 ...
- 【PHP&&mysqli】
msyqli和mysql只有一个字母的差别,真正的含义是msyql的增强版扩展. MySQL可以处理满足程序员对MySQL数据库操作的各种需要了,为什么还需要mysqli呢?因为mysqli支持面性对 ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2897 巴什博弈变形 ***
大意:一堆石子共有n个,A,B两人轮流从中取,每次取的石子数必须在[p,q]区间内,若剩下的石子数少于p个,当前取者必须全部取完.最后取石子的人输.给出n,p,q,问先取者是否有必胜策略? Bash博 ...