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)个点,这些点中的一些被涂上了红色,剩下的被涂上了黑色.然后将这些点两两相连.于是每三个点都会组成 ...
随机推荐
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- ios 横竖屏通知
屏幕切换时,会发送一个通知.只要注册一个通知: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(do ...
- UbuntuLinux安装java
jdk1.7,jdk1.8详情,参见:http://www.cnblogs.com/a2211009/p/4265225.html
- Android RadioButton selector背景
RadioButton selector 背景 <?xml version="1.0" encoding="utf-8"?> <selecto ...
- max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
在/etc/syscurity/limits.conf 加入以下两行: elastic hard nofile 65536 elastic soft nofile 65536 #备注:elastic ...
- yum install 安装 下载好的rpm包 会并依赖包一起安装 zoom电话会议的安装
[root@ok-T Downloads]# rpm -ivh zoom_x86_64.rpm error: Failed dependencies: libxcb-image.so.()(64bit ...
- linux 普通用户切换成root免密码
[root@ok ~]# vim /etc/pam.d/su 下面是/etc/pam.d/su文件的内容 #%PAM-1.0 auth sufficient pam_rootok.so # Uncom ...
- 与你相遇好幸运,Tippecanoe在Centos下の安装
全新的CentOS 7 x86_64 安装编译工具 yum install -y gcc automake autoconf libtool make yum insyall -y gcc gcc-c ...
- Ext Js【Hello World】 ——4.1 beta 1
准备:vs+ExtJs4.1Beta1 ExtJS 4.1 xiazai_ https://yunpan.cn/cqv6bdBwtRjAj (提取码:2733) 引用,cs文件,js主入口,zh—c ...
- 让那些为Webkit优化的网站也能适配IE10(转载)
转载地址:http://www.w3cplus.com/css3/adapting-your-webkit-optimized-site-for-internet-explorer-10.html 特 ...