https://vjudge.net/contest/279745#problem/G

每次将质数的倍数放进一个集合中,那么如果最后的集合数为n的话;

方案数: 2^n -2 ;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/
int a[maxn];
int fa[maxn];
int p[1000004];
int prime[1000004];
int tot;
bool vis[1000005]; void init() {
for (int i = 2; i <= 1000000; i++) {
if (!vis[i]) {
prime[++tot] = i;
}
for (int j = 1; j <= tot &&(ll) i*(prime[j]) <= 1000000; j++) {
vis[i*prime[j]] = 1;
if (i%prime[j] == 0)break;
}
}
}
int findfa(int x) {
if (x == fa[x])return x;
else return fa[x] = findfa(fa[x]);
}
int qpow(int a, int b, int mod) {
int ans = 1;
while (b>0) {
if (b & 1)ans = (ll)ans * a%mod;
a =(ll) a * a%mod; b >>= 1;
}
return ans;
} int main() { init();
int T; cin >> T;
while (T--) {
int n; rdint(n);
int cnt = 0;
for (int i = 1; i <= n; i++) {
rdint(a[i]);
if (a[i] == 1) {
cnt++; n--; i--;
}
}
if (n) {
sort(a + 1, a + 1 + n);
n = unique(a + 1, a + 1 + n) - a - 1;
ms(p);
for (int i = 1; i <= n; i++) {
p[a[i]] = i; fa[i] = i;
}
for (int i = 1; i <= tot; i++) {
int pp = 0;
for (int j = prime[i]; j <= 1000000; j += prime[i]) {
if (p[j]) {
if (!pp)pp = findfa(p[j]);
else {
int q = findfa(p[j]);
fa[q] = pp;
}
}
}
}
for (int i = 1; i <= n; i++) {
findfa(i);
if (i == fa[i])cnt++;
} }
cout << (qpow(2, cnt, mod) - 2 + mod) % mod << endl;
}
return 0;
}

HackerRank - array-partition 并查集的更多相关文章

  1. HackerRank - journey-to-the-moon 【并查集】

    HackerRank - journey-to-the-moon [并查集] 题意 有很多不同的宇航员,有些宇航员来自同一个国家,有些宇航员来自不同的国家,然后美国航天局想要选出两名来自不同国家的宇航 ...

  2. codeforces 1023 D. Array Restoration 并查集

    D. Array Restoration time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. CodeForces - 722C Destroying Array (并查集/集合的插入和删除)

    原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...

  4. CF722C. Destroying Array[并查集 离线]

    链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...

  5. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集

    C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...

  6. [并查集+逆向思维]Codeforces Round 722C Destroying Array

    Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. C. Destroying Array 并查集,逆向思维

    用并查集维护线段,从后往前枚举没个删除的位置id[i] 那么,现在删除了这个,就是没有了的,但是上一个id[i + 1]就是还没删除的. 然后现在进行合并 int left = id[i + 1];( ...

  8. CodeForces 722C Destroying Array (并查集)

    题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...

  9. ZOJ 3963:Heap Partition(贪心+set+并查集)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3963 题意:给出一个n个数的序列,可以在其中找一个子序列建堆,并且堆中的父 ...

  10. CF469D Two Set (并查集)

    Codeforces Round #268 (Div. 2)D Codeforces Round #268 (Div. 1)B CF468B D. Two Sets time limit per te ...

随机推荐

  1. [Codeforces]#179 div1-----295ABCDE

    摘自我的github:https://github.com/Anoxxx The Solution Source: Codeforces Round #179 (Div. 1) VJudge链接: h ...

  2. vi,sed,tr,awk技巧

    将文件中的换行替换为逗号 使用sed: sed -e :a -e N -e '$!ba' -e 's/\n/,/g' filename 使用tr: cat filename | tr '\n' ',' ...

  3. docker 笔记(2) base镜像

    base 镜像有两层含义: 不依赖其他镜像,从 scratch 构建. 其他镜像可以之为基础进行扩展. 所以,能称作 base 镜像的通常都是各种 Linux 发行版的 Docker 镜像,比如 Ub ...

  4. 图片缓存核心类LruCache

    该类类似一个缓存池,具体可参考 http://www.fengfly.com/plus/view-214546-2.html

  5. [poj3348]Cows

    题目大意:求凸包面积. 解题关键:模板题,叉积求面积. 这里的cmp函数需要调试一下,虽然也对,与普通的思考方式不同. #include<cstdio> #include<cstri ...

  6. 客户注册功能,发短信功能分离 通过ActiveMQ实现

    客户注册功能,发短信功能分离 通过ActiveMQ 配置链接工厂, 配置session缓存工厂(引入链接工厂) 2.配置模板对象JmsTemplate 引入缓存工厂    指定消息模式(队列,发布和订 ...

  7. Blender 安装

    Blender 安装 Blender 安装 windows 上安装 Blender 搞定 Ubuntu Linux 上安装 Blender 搞定 windows 上安装 Blender Step 1 ...

  8. ps和ai的一些认识

    ps主要是一个后期软件,它很大程度上不是一个创作型的软件,这是它的定位.我觉得李涛老师那句话说的很好,ps是对已有的素材进行加工的.这个已有的素材来源包括但不限于拍照.扫描.数绘板.下载的.如果说你想 ...

  9. C语言-郝斌笔记-006排序及查找

    1. int partion(int *a, int low, int high) { int value = a[low]; int t; while (low < high) { while ...

  10. Umbraco Form 中需要为一个Form的某个field设置特别的CSS样式

    在项目开发中,我有一个需求,需要建立一个页面,这个页面上有一个form, 这个form上有一个checkbox, 就是普通的接受terms & conditions, 类似下图 这个项目中的U ...