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){ / ...
随机推荐
- 搭建Redis环境以及所遇问题(CentOS7+Redis 3.2.8)
一.安装步骤 1. 首先需要安装gcc,把下载好的redis-3.2.8-rc2.tar.gz 放到/usr/local文件夹下 2. 进行解压 tar -zxvf redis-3.2.8-rc2.t ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- mysql date_add日期函数的使用
select date_add(CURRENT_DATE()-day(CURRENT_DATE())+1,interval 3 month);##my sql 获取三个月之后的第一天日期select ...
- HUNAN -11566 Graduation Examination(找规律)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11566&courseid=0 输入n,求出第n个fi ...
- Idea其他设置
一.生成javadoc Tools->Gerenate JavaDoc 1. 选择是整个项目还是模块还是单个文件 2. 文档输出路径 3. Locale 选择地区,这个决定了文档的语言,中文就是 ...
- 接阿里云oss有感
看API,从头细看到尾,在这个过程中一定会找到你要找的东西.
- 【Java TCP/IP Socket】基于NIO的TCP通信(含代码)
NIO主要原理及使用 NIO采取通道(Channel)和缓冲区(Buffer)来传输和保存数据,它是非阻塞式的I/O,即在等待连接.读写数据(这些都是在一线程以客户端的程序中会阻塞线程的操作)的时候, ...
- Nginx官方配置文档收集
官方入口: https://www.nginx.com/resources/wiki/start/#pre-canned-configurations http://nginx.org/en/docs ...
- svm、logistic regression对比
相同点:都是线性分类算法 不同点: 1.损失函数不同 LR:基于“给定x和参数,y服从二项分布”的假设,由极大似然估计推导 SVM: hinge loss + L2 regularization的标准 ...
- 转:VMware中三种网络连接的区别
转自:http://www.cnblogs.com/rainman/archive/2013/05/06/3063925.html VMware中三种网络连接的区别 1.概述 2.bridged( ...