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. Java到底是值传递还是引用传递

    什么是按值传递,什么是按引用传递 按值调用(call by value) : 在参数传递过程中,形参和实参占用了两个完全不同的内存空间.形参所存储的内容是实参存储内容的一份拷贝. 按引用调用:在参数传 ...

  2. for 循环进化史

    ECMAScript 6已经逐渐普及,经过二十多年的改进,很多功能也有了更成熟的语句,比如 for 循环 这篇博客将介绍一下从最初的 for 循环,到 ES6 的 for-of 等四种遍历方法 先定义 ...

  3. 干货--安装eclipse-hadoop-plugin插件及HDFS API编程两个遇到的重要错误的解决

    在Windows的eclipse上写hdfs的API程序,都会遇到两个错误,在网上查了很多资料,都没有解决的办法,经过了很多时间的研究,终于把这个问题解决了 错误是 1.java.io.IOExcep ...

  4. xgboost调参

    The overall parameters have been divided into 3 categories by XGBoost authors: General Parameters: G ...

  5. OCP47:155

  6. SQL SELECT TOP, LIMIT, ROWNUM 子句

    SQL SELECT TOP, LIMIT, ROWNUM 子句 SQL SELECT TOP 子句 SELECT TOP 子句用于规定要返回的记录的数目. SELECT TOP 子句对于拥有数千条记 ...

  7. lua 获取当前执行目录 lfs 库

    lua lfs 库 lfs.attributes(filepath [, aname]) 获取路径指定属性 lfs.chdir(path) 改变当前工作目录,成功返回true,失败返回nil加上错误信 ...

  8. react 使用 moment 进行 日期格式化

    在react中使用得先导入: import moment from 'moment'; 调用: npm install moment var moment = require('moment'); m ...

  9. win10 UWP 申请微软开发人员

    申请微软开发人员能够到https://dev.windows.com/zh-cn/programs/join 假设是学生,先去http://www.dreamspark.com/ 假设是英文,点stu ...

  10. Oracle db中禁止使用sqlplus的方法

    先记录下来: How to Disable a SQL*Plus Connection for a User (文档 ID 124121.1)