题目链接:Coprime

pid=5072">

题面:

Coprime

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

Total Submission(s): 1181    Accepted Submission(s): 471

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
2014 Asia AnShan Regional Contest



解题:

    题意求找出三数互质或都不互质的组数,直接肯定不行。

此题原型为单色三角形,结果为C(3,n)-res,当中res为每一个与每一个数互质和不互质数量的乘积的累加。求与每一个数不互质的数量,用到了容斥原理。看似简单的原理应用却这么广泛,假设没做过容斥原理的题目,能够先试一下HDU 1796。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
bool status[100010];
int factor[100010][8];
int store[100010],cnt[100010];
long long ans[100010],res;
int one_amount[300];
int refl[300][8];
int cal[8];
void prep()
{
memset(refl,0,sizeof(refl));
int cont=0,temp;
for(int i=0;i<256;i++)
{
temp=i;
one_amount[i]=cont=0;
while(temp)
{
if(temp%2)
{
one_amount[i]++;
refl[i][cont]=1;
}
cont++;
temp/=2;
}
}
}
bool is_prime(int a)
{
if(a<=3)return true;
int x=sqrt(1.0*a);
for(int i=2;i<=x;i++)
{
if(a%i==0)
return false;
}
return true;
}
long long C(int x,int y)
{
long long res=1;
for(int i=1;i<=x;i++)
{
res=res*(y-i+1)/i;
}
return res;
}
int main()
{
int t,n,p,tmp;
long long temp;
scanf("%d",&t);
prep();
while(t--)
{
res=0;
memset(status,0,sizeof(status));
memset(factor,0,sizeof(factor));
memset(cnt,0,sizeof(cnt));
memset(ans,0,sizeof(ans));
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&store[i]);
for(int i=0;i<n;i++)
status[store[i]]=1;
for(int i=2;i<=100000;i++)
{
if(is_prime(i))
{
for(int j=i;j<=100000;j+=i)
{
if(status[j])
{
cnt[i]++;
}
p=0;
while(factor[j][p])
{
p++;
}
factor[j][p]=i;
}
}
else
{
for(int j=i;j<=100000;j+=i)
{
if(status[j])
{
cnt[i]++;
}
} }
}
for(int i=0;i<n;i++)
{
tmp=store[i];
p=0;
while(factor[tmp][p])
{
cal[p]=factor[tmp][p];
p++;
}
if(p==0)
{
ans[i]=0;
continue;
}
tmp=1<<p;
for(int j=1;j<tmp;j++)
{
temp=1;
for(int k=0;k<p;k++)
{
if(refl[j][k])
{
temp=temp*cal[k];
}
}
if(one_amount[j]%2)
ans[i]+=cnt[temp];
else
ans[i]-=cnt[temp];
}
ans[i]-=1;
}
for(int i=0;i<n;i++)
{
res+=(ans[i]*(n-ans[i]-1));
}
res/=2;
printf("%I64d\n",C(3,n)-res);
}
return 0;
}

总结:

用好容斥原理的关键在于,搞清楚集合的交的含义。

 

HDU 5072 Coprime (单色三角形+容斥原理)的更多相关文章

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

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

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

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

  3. hdu 5072 Coprime 容斥原理

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

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

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

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

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

  6. hdu 5072 Coprime

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

  7. hdu 5072 Coprime (容斥)

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

  8. Coprime (单色三角形+莫比乌斯反演(数论容斥))

    这道题,先说一下单色三角形吧,推荐一篇noip的论文<国家集训队2003论文集许智磊> 链接:https://wenku.baidu.com/view/e87725c52cc58bd631 ...

  9. 单色三角形(hdu-5072

    单色三角形模型:空间里有n个点,任意三点不共线.每两个点之间都用红色或者黑色线段链接.如果一个三角形的三条边同色,责成这个三角形是单色三角形.对于给定的红色线段列表,找出单色三角形的个数. 分析:对于 ...

随机推荐

  1. 基于jQuery的楼层案例

    ~(function() { var flag = true; //点击切换效果 $(".oDR7_asideItem:not(:first)").click(function() ...

  2. 洛谷 P1014 Cantor表

    P1014 Cantor表 题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 ...

  3. Android学习笔记之网络接口(Http接口,Apache接口,Android接口)

    目前Android平台有三种网络接口可以使用,他们分别是:Java.NET.*(标准Java接口),org.apache(Apache接口),和android.Net.*(android网络接口). ...

  4. js面向对象1----了解构造函数

    一.构造函数与实例的区别 1 构造函数  构造函数主要是一种用于生成对象的饼干模具,这些对象具有默认属性和属性方法,它可以创建多个共享特定特性和行为的对象.  构造函数只是一个函数,但当函数遇到了ne ...

  5. 水题ing

    T1: https://www.luogu.org/problemnew/show/P1724幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆 ...

  6. 【CS Round #43 E】Coprime Pairs

    [链接]点击打开链接 [题意] 让你选择n个数字,组成一个数组,使得这n个数字中恰好有k对,它们是互质的. [题解] 我们可以先找出前n个质数,那么接下来的问题就转化为,凑出rest = n*(n-1 ...

  7. Eclipse Class Decompiler——Java反编译插件手工配置方法

    最近在eclipse上配置了java反编译插件,但是不好用,原因是我的eclipse之前有手动配置过一些类似的java反编译插件,当我将原来的插件完全卸载后重新配置才正常配置上去,自动配置java反编 ...

  8. linux删除svn版本号库

    当使用了svn版本号控制系统后每一个文件夹下都会有一个.svn文件夹存在,开发完当交付产品或者上传到server时一般要把这些文件夹删除.事实上在linux删除这些文件夹是非常easy的,命令例如以下 ...

  9. 并发,two

    引言 为了更加形象的描述并发的基础知识,因此本文LZ采用了园子里一度大火的标题形式--"没听说过XXXX,就不要说你XXXX了".希望能够给猿友们一个醒目的警醒,借此来普及并发的基 ...

  10. Altium Designer如何设置pcb尺寸