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. mysql查询最近30天、7天、每天、昨天、上个月的记录

      一些变量说明: add_time为插入的时间 to_days是sql函数,返回的是个天数 data_sub(date,INTERVAL expr type)给指定的日期减去多少天 data()函数 ...

  2. Visual Studio 小工具

    折叠解决方案的每个项(Collapse Solution) 功能:折叠解决方案视图窗口中的解决方案或项目级别的每个项.包括嵌套在解决方案文件夹中的子项,引用和属性文件夹扩展节点. 源代码的文件头实现方 ...

  3. Solaris10上如何识别新增加的HDLM LUN

    先在磁盘阵列上将新加LUN映射给主机组,然后在光纤交换机上增加相关zone信息.以下是Solaris10上需要执行的操作步骤. 在Solaris10上重新扫描磁盘 -bash-3.2# cfgadm ...

  4. div的作用

    <div></div>主要是用来设置涵盖一个区块为主,所谓的区块是包含一行以上的数据,所以在<div></div>的开始之前与结束后,浏览都会自动换行, ...

  5. LAMP 2.2 Apache配置静态缓存

    这里的静态文件指的是图片.js.css 等文件,用户访问一个站点,其实大多数元素都是图片.js.css 等,这些静态文件其实是会被客户端的浏览器缓存到本地电脑上的,目的就是为了下次再请求时不再去服务器 ...

  6. 34款Firefox渗透测试插件

    1:Firebug Firefox的 五星级强力推荐插件之一,不许要多解释 2:User Agent Switcher 改变客户端的User Agent的一款插件 3:Hackbar 攻城师必备工具, ...

  7. struts2学习笔记(4)接收参数

    ①用action属性接收 登录界面例子 在webroot下创建login.jsp和success.jsp login.jsp中加入表单: <form action="LoginActi ...

  8. SQLAchemy ORM框架

    SQLAchemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行S ...

  9. C++面向对象类的实例题目二

    题目描述: 编写一个程序,设计一个产品类Product,其定义如下: class Product { public: Product(char *n,int p,int q); //构造函数 ~Pro ...

  10. matlab rand(3,5)

    rand()函数在(0,1)上创建均匀分布的随机数的数组 >> rand(3,5) ans = 0.8147 0.9134 0.2785 0.9649 0.9572 0.9058 0.63 ...