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){ / ...
随机推荐
- Java 新手进阶:细说引用类型
在前几天的帖子<Java性能优化[1]:基本类型 vs 引用类型>里,俺大概介绍了“引用类型”与“基本类型”在存储上的区别.昨天有网友在评论中批评说“引用类型变量和它所引用的对象”没区分清 ...
- 洛谷——P2074 危险区域
P2074 危险区域 题目背景 一个恐怖组织在一座城市中安放了定时炸弹,其威力巨大,现在这里的警长想知道最坏的情况下会有多少街区受威胁. 题目描述 在一个城市有N*M个街区,每个街区由坐标描述,如图所 ...
- Ubuntu 16.04下更新Atom
在Ubuntu下Atom好像不会自动更新,但是可以通过这些方法去实现: 1.安装插件:https://atom.io/packages/up2date 2.使用apt源更新: sudo apt-get ...
- DELPHI方法注释的标准写法
/// <summary> /// 查询数据 /// </summary> /// <param name="accountno">帐套号< ...
- 【IntelliJ Idea】启动参数JVM参数的配置 优先级高于 application.yaml/application.properties中的配置,前者可以覆盖后者的配置
- Python基础语法06--文件
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
- chrome备份网站
chrome备份网站 https://www.chromedownloads.net/
- Python奇技
本文目录 1. 显示有限的接口到外部 2. with的魔力 3. filter的用法 4. 一行作判断 5. 装饰器之单例 6. staticmethod装饰器 7. property装饰器 8. i ...
- Solidworks如何创建投影曲线
画好草图之后(草图是在上视基准面上画的)然后点击曲线,投影曲线 面选择要投影的曲面,然后就得到了平面曲线在曲面上的投影得到的空间曲线 注意这种方法对于开环轮廓也是可以用的,比如下面,我定义一个 ...
- Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。——贪心、转化
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...