Coprime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 433    Accepted Submission(s): 192

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

题意:求n个数字中取出3个数字,统计   都互质  或  都不互质  的对数。

思路:取出的3个数字a,b,c 有8种情况。

其中题目要求的只是其中两种情况,还有6种情况,要么就是1对互质对,要么有2队互质对存在。

如果能求得 ai 在n个数字中 有 k 个数字与其不互质   k2个数字与其互质。

那么题目就能转化为求所有的   sum(k*k2);

对于数字ai来说,我们知道已经有一对互质对必然存在了,但是也可能会存在第2对,因为k个中和k2中。

所以,就相当于对ai来说,有至少一对互质对,最多2个互质对的情况。 统计所有。

我们会发现,会多算了一次,/2即可。

下面就是关于如何求ai 有多少个数字与ai互质的问题了。

容斥。

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
const int maxn = 1e5+; int a[maxn];
bool s[maxn];
int prime[maxn],len;
vector<int>Q[maxn];
vector<int>X[maxn];
int Hash[maxn]; int H[maxn],hlen;
void init()
{
memset(s,false,sizeof(s));
s[]=true;
len = ;
for(int i=; i<maxn; i++)
{
if(s[i]==true)continue;
prime[++len]=i;
for(int j=i+i; j<maxn; j=j+i)
s[j] = true;
}
/**筛选i素因子**/
for(int i=; i<=len; i++)
{
for(int j=prime[i]; j<maxn; j=j+prime[i])
Q[j].push_back(prime[i]);
}
int k,tmp;
for(int i=; i<maxn; i++){
hlen = ;
H[]=-;
k = Q[i].size();
for(int j=; j<k; j++){
tmp = hlen;
for(int ss=; ss<=tmp; ss++){
H[++hlen]=-*H[ss]*Q[i][j];
X[i].push_back(H[hlen]);
}
}
}
}
int main()
{
int T,n,k;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(Hash,,sizeof(Hash));
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
k = X[a[i]].size();
for(int j=; j<k; j++)
if(X[a[i]][j]<)
Hash[-X[a[i]][j]]++;
else
Hash[X[a[i]][j]]++;
}
int sum;
__int64 tom = ;
for(int i=; i<=n; i++)
{
if(a[i]==) continue;
sum = ;
k = X[a[i]].size();
/**sum 与ai 不互质的个数**/
for(int j=; j<k; j++)
{
if(X[a[i]][j]<)
sum=sum-Hash[-X[a[i]][j]];
else sum=sum+Hash[X[a[i]][j]];
}
sum --; //减去本身
tom = tom + sum*(__int64)(n--sum);
}
printf("%I64d\n",(n*(__int64)(n-)*(n-))/-tom/);
}
return ;
}

HDU Coprime的更多相关文章

  1. 数学: HDU Co-prime

    Co-prime Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Subm ...

  2. hdu Co-prime

    题意:求出在一个区间[A,B]内与N互质的个数 . 思路: 先求出n的质因子,然后求出与N的质因子不互质的个数然后总个数减去就是.用位运算二进制表示那个因子用到过,实现容斥原理.在1到n之间是c倍数的 ...

  3. [容斥原理] hdu 4135 Co-prime

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...

  4. 容斥 - HDU 4135 Co-prime

    Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...

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

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

  6. hdu 5072 Coprime 容斥原理

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

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

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

  8. hdu 5072 Coprime

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

  9. hdu 5072 Coprime (容斥)

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

随机推荐

  1. mysqld诡异crash

    突然收到告警短信,提示有一组服务器MHA已经切换,登录服务器后查看错误日志如下(其中相关insert语句已经处理): mysql版本:5.5.24 :: InnoDB: Assertion failu ...

  2. BizTalk动手实验(五)Map开发测试

    1 课程简介 通过本课程熟悉Map的相关开发与测试技术 2 准备工作 熟悉XML.XML Schema.XSLT等相关XML开发技术 新建BizTalk空项目 演示 3.1 基本操作 打开MapDev ...

  3. Linux vi编辑器的基本命令

    vi编辑器的文字说明 模式:命令模式,编辑模式,末行模式. 切换方式:命令模式→i→编辑模式,编辑模式→Esc→命令模式,命令模式→:→末行模式. 功能: 命令模式(Command Mode): 控制 ...

  4. 【iCore3 双核心板】例程十八:USB_VCP实验——虚拟串口

    实验指导书及代码包下载: http://pan.baidu.com/s/1c1erqIc iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  5. 【iCore3 双核心板_FPGA】实验二十四:Niosii——SDRAM读写实验

    实验指导书及代码包下载: http://pan.baidu.com/s/1c2xAJT2 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  6. DataSet, BindingSource, BindingNavigator Relationship

    Multiple Bindings caused dataBing weird???? Text.DataBindings.Add(new Binding("Text", bs1, ...

  7. JQuery-筛选

    /* 六丶筛选 */ // console.log( $("div:first") ); // div#id_1 所有div中的第一个 // console.log( $(&quo ...

  8. Gradle--ubuntu

    在Ubuntu安装Gradle也是很简单.切记请勿使用apt-get安装Gradle.因为Ubuntu源的Gradle实在太旧.我用的搜狐的源,竟然是2011年. 下面是安装步骤: 1.在官网下载最新 ...

  9. Python高级特性(1):Iterators、Generators和itertools(参考)

    对数学家来说,Python这门语言有着很多吸引他们的地方.举几个例子:对于tuple.lists以及sets等容器的支持,使用与传统数学类 似的符号标记方式,还有列表推导式这样与数学中集合推导式和集的 ...

  10. android NumberPicker 数组越界的坑

    被这个问题耽误了一个多小时... 直接上解决方案,参考红色部分. private void initViews() { wheel = (NumberPicker) findViewById(R.id ...