You are given N(1<=N<=100000) integers. Each integer is square free(meaning it has no divisor which is a square number except 1) and all the prime factors are less than 50. You have to find out the number of pairs are there such that their gcd is 1 or a prime number. Note that (i,j) and (j,i) are different pairs if i and j are different.

Input

The first line contains an integer T(1<=T<=10) , the number of tests. Then T tests follows. First line of each tests contain an integer N. The next line follows N integers.

Output

Print T lines. In each line print the required result.

Sample Input

Sample Output

1

3

2 1 6

8

Explanation

gcd(1,2)=1

gcd(2,1)=1

gcd(2,6)=2, a prime number

gcd(6,2)=2, a prime number

gcd(1,6)=1

gcd(6,1)=1

gcd(2,2)=2, a prime number

gcd(1,1)=1

So, total of 8 pairs.

题意:给定数组a[],求多少对(i,j),使得a[i],a[j]互质或者gcd是质数,保证a[]只有小于50的素因子,而且不含平方因子。

思路:注意到只有15个素数,开始想到了用二进制来找互质的个数和有一个素因子的个数,但是复杂度好像还是过不去。第二天忍不住参考了vj上面的代码。。。

主要问题在于,如何快速地求一个二进制的子集,即对i,求所有的j,j<=i&&(i|j)==i。后面地就不难。

前辈写的是:

    for(i=;i<M;i++){
for(j=i;;j=(j-)&i){
s[i]+=num[j]; //关键,得到子集
if(!j) break;
}
}

时间大概是1.4e7。

int times=;
for(i=;i<M;i++){
for(j=i;;j=(j-)&i){
times++;
if(!j) break;
}
}
cout<<times<<endl;

。。。注意把0也要累加进去。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int M=<<;
int num[M],s[M];
int p[]={,,,,,,,,,,,,,,};
int main()
{
int T,N,i,j,tmp; ll ans,x;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
memset(num,,sizeof(num));
memset(s,,sizeof(s));
for(i=;i<=N;i++){
scanf("%lld",&x); tmp=;
for(j=;j<;j++) if(x%p[j]==) tmp+=<<j;
num[tmp]++;
}
for(i=;i<M;i++){
for(j=i;;j=(j-)&i){
s[i]+=num[j]; //关键,得到子集
if(!j) break;
}
} ans=;
for(i=;i<M;i++){
ans+=(ll)num[i]*s[i^(M-)];//互质
for(j=;j<;j++){ //刚好有一个素因子
if(i&<<j){
ans+=(ll)num[i]*(s[i^(M-)^(<<j)]-s[i^(M-)]);//减法保证这个素因子不被减去
}
}
}
cout<<ans<<endl;
}
return ;
}

SPOJ:NO GCD (求集合&秒啊)的更多相关文章

  1. 求集合中选一个数与当前值进行位运算的max

    求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...

  2. hdu 1856 求集合里元素的个数 输出最大的个数是多少

    求集合里元素的个数 输出最大的个数是多少 Sample Input41 23 45 61 641 23 45 67 8 Sample Output42 # include <iostream&g ...

  3. SQL_求集合中每天最大时间记录的总和

    --问题求 集合中每天最大时间的总和 表中的数据 列: 用户 分数 时间 A 2 2014-01-01 01:00:00 A 2 2014-01-01 02:00:00 A 2 2014-01-01 ...

  4. DFS算法-求集合的所有子集

    目录 1. 题目来源 2. 普通方法 1. 思路 2. 代码 3. 运行结果 3. DFS算法 1. 概念 2. 解题思路 3. 代码 4. 运行结果 4. 对比 1. 题目来源 牛客网,集合的所有子 ...

  5. JAVA求集合中的组合

    好几个月没弄代码了,今天弄个求组合的DEMO 思路是将集合的每个值对照一个索引,索引大小是集合的大小+2.索引默认为[000...000],当组合后选取的组合值demo为[0100..00].然后根据 ...

  6. hdu5175 gcd 求约数

    题意:求满足条件GCD(N,M) = N XOR M的M的个数 sol:和uva那题挺像的.若gcd(a,b)=a xor b=c,则b=a-c 暴力枚举N的所有约数K,令M=NxorK,再判断gcd ...

  7. BC68(HD5606) 并查集+求集合元素

    tree  Accepts: 143  Submissions: 807  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65 ...

  8. spoj 3871. GCD Extreme 欧拉+积性函数

    3871. GCD Extreme Problem code: GCDEX Given the value of N, you will have to find the value of G. Th ...

  9. GCD求最大公约数

    求最大公约数哪个强,果断GCD,非递归版本和递归版本如下: #include<iostream> using namespace std; int gcd(int a, int b){ / ...

随机推荐

  1. ubuntu下不同版本python安装pip及pip的使用

    由于ubuntu系统自带python2.7(默认)和python3.4,所以不需要自己安装python. 可以使用python -V和python3 -V查看已安装python版本. 在不同版本的py ...

  2. SGU 乱乱开

    本解题报告 乱抄,乱写,随性随心,不喜多喷! SGU 142: 思路:一个string的字串不会超过2^20个,我们枚举出来就好了. 我出错点:数组RE #include<stdio.h> ...

  3. setImageEdgeInsets 和 setImage配合使用达到button区域大并可调节其上图片显示区域大小的效果

    [self.indicator setImage:[UIImage imageNamed:@"01_login_moreicon@2x.png"] forState:UIContr ...

  4. Shannon-Fano-Elias编码的C语言实现

    Shannon-Fano-Elias编码 一.理论分析 Shannon-Fano-Elias编码是利用累积分布函数来分配码字. 不失一般性,假定取X={1,2,-m}.如果对于全部的x,有p(x)&g ...

  5. python发声

    python发声 学习了:http://www.jb51.net/article/62644.htm import winsound winsound.Beep(600,1000) #其中600表示声 ...

  6. HTML网页之进入站点口令脚本

    加入以下这个脚本在head标签中. <script language="JavaScript"> <!-- var password=""; ...

  7. node 爬虫 --- 批量下载图片

    步骤一:创建项目 npm init 步骤二:安装 request,cheerio,async 三个模块 request 用于请求地址和快速下载图片流. https://github.com/reque ...

  8. Android开发的环境搭建及HelloWorld的实现

    安装JDK和配置Java开发环境 http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-4321 ...

  9. Development of Intel chipsets interconnection

    http://en.wikipedia.org/wiki/Chipset Chipset From Wikipedia, the free encyclopedia     A chipset is ...

  10. HDU 1248 寒冰王座 (水题的N种做法!)(含完全背包)

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...