hdu 5072 Coprime (容斥)
Problem Description
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.
5
1 3 9 10 2
题意:从一个数组中找出所有 3个数 都 相互互质 和 相互不互质 的总个数
思路:首先先转化为求 与一个数互质和不互质的个数,然后将互质和不互质相乘,然后总数减去即可。
接下来就是怎么求互质和不互质,用到了容斥原理来求,模板套一下就可以了
sum【i】数组表示i这个数是数组中的元素的因子的个数
该题还可以先求出所有的素数,范围可以自己确定,然后在分解质因数的时候可以起到优化的作用,如注释掉的部分
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100006
#define ll long long
ll n;
ll a[N];
//ll prime[N];
//ll num[N];
//ll k=0;
ll fac[N];//分解质因数的质因数
ll sum[N];
ll have[N];
/*void init()
{
memset(num,0,sizeof(num));
for(int i=2;i<N;i++)
{
if(!num[i])
{
prime[k++]=i;
for(int j=i;j<N;j+=i)
{
num[j]=1;
}
}
}
}
*/
ll solve()
{
ll ans=;
for(ll i=;i<n;i++)
{
ll m=a[i];
ll num=;
ll cnt=;
for(ll j=;j*j<=m;j++)
{
if(m%j==)
{
fac[num++]=j;
while(m%j==)
m/=j;
}
}
if(m>) fac[num++]=m;
for(ll j=;j<(<<num);j++)
{
ll w=;
ll tmp=;
for(ll k=;k<num;k++)
{
if((<<k)&j)
{
tmp=tmp*fac[k];
w++;
}
}
if(w&) cnt+=sum[tmp];
else cnt-=sum[tmp];
}
if(cnt==) continue;
ans+=(cnt-)*(n-cnt);
}
return ans/;
}
int main()
{
//init();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%I64d",&n);
memset(have,,sizeof(have));
memset(sum,,sizeof(sum)); for(ll i=;i<n;i++) { scanf("%I64d",&a[i]); have[a[i]]=; } for(ll i=;i<N;i++)
{
for(ll j=i;j<N;j+=i)
{
if(have[j])
sum[i]++;
}
}
ll ans=n*(n-)*(n-)/;
ans=ans-solve();
printf("%I64d\n",ans);
}
return ;
}
hdu 5072 Coprime (容斥)的更多相关文章
- HDU - 4135 Co-prime 容斥定理
题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...
- HDU 4135 Co-prime (容斥+分解质因子)
<题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. ...
- hdu 4135 Co-prime(容斥)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 5514 Frogs(容斥)
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5213 分块 容斥
给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...
- HDU 2588 思维 容斥
求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...
- hdu 5072 Coprime
http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出 n 个互不相同的数,求满足以下条件的三元无序组的个数:要么两两互质要么两两不互质. 思路:根据 ...
- HDU 1695 GCD 容斥
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...
- HDU 5514 Frogs 容斥定理
Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...
随机推荐
- 整型数组处理算法(八)插入(+、-、空格)完成的等式:1 2 3 4 5 6 7 8 9=N[华为面试题]
有一个未完成的等式:1 2 3 4 5 6 7 8 9=N 当给出整数N的具体值后,请你在2,3,4,5,6,7,8,9这8个数字的每一个前面,或插入运算符号“+”,或插入一个运算符号“-”,或不插入 ...
- CSU1659: Graph Center(最短路)
Description The center of a graph is the set of all vertices of minimum eccentricity, that is, the s ...
- emacs快捷键学习(一)--Linux最强大的编辑器
emacs是一个非常强大的编辑器.经常使用的快捷键总结例如以下: 退出emacs:ctrl+x ctrl+c 移动到下一屏:ctrl+v 移动到上一屏:alt+v 将光标所在行移动到屏幕中间:ctrl ...
- 自定义控件(视图)2期笔记01:自定义控件之自定义View的步骤
1. 根据Android Developers官网的介绍,自定义控件你需要以下的步骤: (1)创建View (2)处理View的布局 (3)绘制View (4)与用户进行交互 (5)优化已定义的Vie ...
- Eclipse,hadoop2.7.2 hadoop-eclipse-plugin.jar的制作
装好了hadoop后发现有装个eclipse的必要,于是参照文章A(http://www.powerxing.com/hadoop-build-project-using-eclipse/)进行安装, ...
- DC综合环境的一些概念
DC综合环境的一些概念 启动文件 .synopsys_dc_setup 采用Tcl格式,包含工艺库的路径信息和其他环境变量 不同位置启动顺序 1.Synopsys安装目录 2.用户家目录 3.项目工作 ...
- 定时改变App主题的方案
1.将接口返回的图片缓存到本地,由于写data到本地是耗时操作,为了不阻塞主线程,可开启子线程来做此操作 dispatch_queue_t queue = dispatch_queue_create( ...
- 导出Exexcl类
前台: <%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation=" ...
- FileStream操作文件读写
FileStream fsRead = new FileStream(@"C:\Users\Administrator\Desktop\u.html",FileMode.OpenO ...
- effective_c++条款20,用pass-by-reference-to-const替换pass-by-value
pass-by-value void f(A a); 1)导致复制是浪费资源 2)多态是导致对象切割 所以我们使用 void f(const A& a) 上面的话针对class,不针对基本类型 ...