SPOJ:NO GCD (求集合&秒啊)
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 (求集合&秒啊)的更多相关文章
- 求集合中选一个数与当前值进行位运算的max
求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...
- hdu 1856 求集合里元素的个数 输出最大的个数是多少
求集合里元素的个数 输出最大的个数是多少 Sample Input41 23 45 61 641 23 45 67 8 Sample Output42 # include <iostream&g ...
- SQL_求集合中每天最大时间记录的总和
--问题求 集合中每天最大时间的总和 表中的数据 列: 用户 分数 时间 A 2 2014-01-01 01:00:00 A 2 2014-01-01 02:00:00 A 2 2014-01-01 ...
- DFS算法-求集合的所有子集
目录 1. 题目来源 2. 普通方法 1. 思路 2. 代码 3. 运行结果 3. DFS算法 1. 概念 2. 解题思路 3. 代码 4. 运行结果 4. 对比 1. 题目来源 牛客网,集合的所有子 ...
- JAVA求集合中的组合
好几个月没弄代码了,今天弄个求组合的DEMO 思路是将集合的每个值对照一个索引,索引大小是集合的大小+2.索引默认为[000...000],当组合后选取的组合值demo为[0100..00].然后根据 ...
- 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 ...
- BC68(HD5606) 并查集+求集合元素
tree Accepts: 143 Submissions: 807 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- 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 ...
- GCD求最大公约数
求最大公约数哪个强,果断GCD,非递归版本和递归版本如下: #include<iostream> using namespace std; int gcd(int a, int b){ / ...
随机推荐
- 关于MySQL的事务处理及隔离级别
原文地址 :http://blog.sina.com.cn/s/blog_4c197d420101awhc.html 事务是DBMS得执行单位.它由有限得数据库操作序列组成得.但不是任意得数据库操作序 ...
- asp.net开发的调试方法集合
调试是写代码一共非常重要的步骤,掌握好调试的技巧对于编程有事半功倍的效果,下面是我总结的菜鸟用方法 1.关于HTML和JS的调试 JS曾经是我最讨厌的错误,因为大多数错误VS不报错,这是因为js是解释 ...
- ffmpeg 时间戳
转http://blog.csdn.net/yfh1985sdq/article/details/5721953 AVpacket里的时间戳pts和dts.单位好像是us. 问 : 时间戳pts和dt ...
- spring boot 文件上传大小配置
在启动类中,添加bean import javax.servlet.MultipartConfigElement; import org.springframework.boot.SpringAppl ...
- scott登陆PLSQL时候出现insufficient privileges的解决方法
先用SYS登陆SQLPLUS,即: 再给scott授权:
- Filter和Interceptor的终归作用还是从入口修改或验证请求进来的数据
Filter是Java EE标准.Inteceptor是Spring 标准. Filter在servlet前面,Interveptor在servlet之后 Filter和Inteceptor都可以改变 ...
- win7配置java环境变量
http://jingyan.baidu.com/article/9f63fb91d87fb0c8400f0e93.html 安装JDK,从Oracel官方网站上下载,也可以通过搜索,进入链接.下载完 ...
- 修改ubuntu系统时区
ubuntu默认时区是Etc/UTC,和我们的北京时间相差8个时区,需要修改系统的时区,以下有两种简单方式修改系统时区: 1.修改/etc/timezone文件 vi /etc/timezone 把E ...
- 自己定义html中a标签的title提示tooltip
自己定义html中a标签的title提示tooltip 简单介绍 用简单的jquery+CSS创建自己定义的a标签title提示,用来取代浏览器默认行为.如图: watermark/2/text/aH ...
- oracle死锁解决经常用法(屡试不爽)
--1.查询被锁的情况 select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_objects o ,v$sessi ...