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. iphone配置实用工具iPhone Configuration Utility

    下载地址 http://support.apple.com/kb/DL1466 安装完毕后,在设备->控制台,可以很方便看到报错信息

  2. CPU, PSU, SPU的区别

    It all started in January 2005 with Critical Patch Updates (CPU).  Then Patch Set Updates (PSU) were ...

  3. 问题:sqlserver 跨服务器连接;结果:Sql Server 跨服务器连接

    Sql Server 跨服务器连接 用openrowset连接远程SQL或插入数据 --如果只是临时访问,可以直接用openrowset --查询示例 select * from openrowset ...

  4. C++的继承与接口

    1.继承方式 三种继承方式,public,private,protected.注意,继承方式是相对于某一层类的方法而言,并不是相对于子类的对象而言.对于外部世界()对象来说,protected和pri ...

  5. 手动编译安装tmux

    tmux的好处就不多说了,总之是多屏管理的神器.通常我们用系统通用的安装方式可以安装到tmux,但有时候,安装到的可能不是我们所需要的版本,又或者软件源里面没有带tmux.这个时候就需要手动编译安装了 ...

  6. day36-hibernate检索和优化 01-上次课内容回顾

    clear():直接清空一级缓存的所有对象.evict(Object obj):清空一个对象.flush():控制你的刷出的时机.refresh(Object obj);将你的数据查出来把一级缓存覆盖 ...

  7. matlab基础功能实践

    一.matlab在高等数学中的应用(<数学建模算法与应用>P453) 1.求极限 syms x b=limit((sqrt(1+x^2)-1)/(1-cos(x))) syms x a b ...

  8. 分布式锁2 Java非常用技术方案探讨之ZooKeeper 【转载】

    前言:       由于在平时的工作中,线上服务器是分布式多台部署的,经常会面临解决分布式场景下数据一致性的问题,那么就要利用分布式锁来解决这些问题.以自己结合实际工作中的一些经验和网上看到的一些资料 ...

  9. Flask框架 之 基本使用

    初识Flask Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求 ...

  10. Servlet HTTP 状态码 以及 获得浏览器URL

    Servlet HTTP 状态码 HTTP 请求和 HTTP 响应消息的格式是类似的,结构如下: 初始状态行 + 回车换行符(回车+换行) 零个或多个标题行+回车换行符 一个空白行,即回车换行符 一个 ...