HackerRank - array-partition 并查集
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 并查集的更多相关文章
- HackerRank - journey-to-the-moon 【并查集】
HackerRank - journey-to-the-moon [并查集] 题意 有很多不同的宇航员,有些宇航员来自同一个国家,有些宇航员来自不同的国家,然后美国航天局想要选出两名来自不同国家的宇航 ...
- codeforces 1023 D. Array Restoration 并查集
D. Array Restoration time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CodeForces - 722C Destroying Array (并查集/集合的插入和删除)
原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...
- CF722C. Destroying Array[并查集 离线]
链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...
- 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 ...
- [并查集+逆向思维]Codeforces Round 722C Destroying Array
Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- C. Destroying Array 并查集,逆向思维
用并查集维护线段,从后往前枚举没个删除的位置id[i] 那么,现在删除了这个,就是没有了的,但是上一个id[i + 1]就是还没删除的. 然后现在进行合并 int left = id[i + 1];( ...
- CodeForces 722C Destroying Array (并查集)
题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...
- ZOJ 3963:Heap Partition(贪心+set+并查集)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3963 题意:给出一个n个数的序列,可以在其中找一个子序列建堆,并且堆中的父 ...
- CF469D Two Set (并查集)
Codeforces Round #268 (Div. 2)D Codeforces Round #268 (Div. 1)B CF468B D. Two Sets time limit per te ...
随机推荐
- VMware安装操作系统(Operating System not found一个错误原因)
因为指定的IOS文件是多种操作系统的组合,如Win7(32位和64位完全版),那么安装的时候选择一个操作系统类型和ios文件的类型就匹配不上,所以出现这种错误.
- 开发环境入门 linux基础 (部分) 复制 用户和组操作 权限更改
复制 用户和组操作 权限更改 CP 复制命令 cp 源文件 目标文件 a) –r(recursive,递归的):递归地复制目录.当复制一个目录时,复制该目录中所有的内容,其中包括子目录的全部内容. b ...
- DVWA平台v1.9-Brute Force
Low: 随便输一下用户名,密码,test 点击Login 显示用户名或密码错误 在owasp-zap查看数据包 点击,就会转到这 右键,点击Fuzz 点击Remove删除默认的 选定参数变量值,点击 ...
- python中匹配中文,解决不匹配,乱码等问题
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal 字符串前加 ur‘str’即可;
- 清空select标签中option选项的4种不同方式
转自:https://blog.csdn.net/pt_sm/article/details/53521560 方法一 document.getElementById("selectid&q ...
- Spring配置hibernate读取实体类映射mappingResources,annotatedClasses,packagesToScan
转自:https://blog.csdn.net/chendc201/article/details/16886545 这两个是有本质区别的,光看名字也能看出来 mappingResources用于指 ...
- 关于EF中实体和数据表以及查询语句映射的问题
关于EF中实体和数据表以及查询语句映射的问题? 很多人在使用的时候分不清楚 实体字段应该少于等于(数据库中的表字段或者SQL查询中的临时字段).这样在查询或者添加修改都不会出现问题 如果实体的字段大于 ...
- HDU 4899 Hero meet devil (状压DP, DP预处理)
题意:给你一个基因序列s(只有A,T,C,G四个字符,假设长度为n),问长度为m的基因序列s1中与给定的基因序列LCS是0,1......n的有多少个? 思路:最直接的方法是暴力枚举长度为m的串,然后 ...
- opencv3.2加载图片
#include <opencv2/opencv.hpp>#include <iostream> using namespace cv;using namespace std; ...
- C语言中的static的作用?
在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. (1)第一个作用:隐藏. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.为理解这句话 ...