$des$

给定一个长度为 $n$ 的正整数序列 ${a_i }$。
将 ${1,2,...,n}$ 划分成两个非空集合 $S、T$,使得 $gcd(\prod_{i \in S} a_i,

\prod_{i \in T} a_i) = 1$
求划分方案数,对 $10^9 + 7$ 取模。

$sol$

对于两个数 $a, b$ 必须处于同一个集合,当其含有相同因子。
这样的话,将含有相同因子的数用并查集维护。
最后统计所有的 $n$ 个数被分成了 $x$ 个集合
答案就是 $2 ^ n - 2$,$1$ 需要特判

#include <bits/stdc++.h>

using namespace std;

#define gc getchar()
inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} #define Rep(i, a, b) for(int i = a; i <= b; i ++)
#define LL long long const int N = 1e5 + , To = 1e6; bool Check[To + ];
int tot, prime[To]; int fa[To + ];
int n;
int A[N];
bool Use[To]; void Get() {
Rep(i, , To) {
if(!Check[i]) prime[++ tot] = i;
Rep(j, , tot) {
if(prime[j] * i > To) break;
Check[i * prime[j]] = ;
if(i % prime[j] == ) break;
}
}
} int Get(int x) {
return fa[x] == x ? x : fa[x] = Get(fa[x]);
} const int Mod = 1e9 + ; LL Ksm(LL a, LL b) {
LL ret = ;
while(b) {
if(b & ) ret = ret * a % Mod;
a = a * a % Mod;
b >>= ;
}
return ret;
} int vis[To]; int main() {
Get();
for(int T = read(); T; T --) {
n = read();
int js = ;
memset(Use, , sizeof Use);
int Max = ;
Rep(i, , n) A[i] = read(), Use[A[i]] = , Max = max(Max, A[i]), js += (A[i] == );
Rep(i, , To) fa[i] = i;
Rep(i, , tot) {
if(prime[i] > Max) break;
for(int j = ; j * prime[i] <= Max; j ++) {
int num = j * prime[i];
if(Use[num]) {
int fa1 = Get(num), fa2 = Get(prime[i]);
if(fa1 != fa2) fa[fa1] = fa2;
}
}
}
LL up = ;
Rep(i, , n) {
int f = Get(A[i]);
if(vis[f] != T) vis[f] = T, up ++;
}
if(js > ) up += (js - );
LL ans = Ksm(, up);
ans -= ;
if(ans < ) ans += Mod;
cout << ans << "\n";
} return ;
}

Problem 5 素数筛法+并查集的更多相关文章

  1. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

  2. poj 1182 食物链 (并查集)

    http://poj.org/problem?id=1182 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...

  3. poj1182 并查集

     题目连接:http://poj.org/problem?id=1182 基础并查集,需要维护与根节点关系,解析见代码: /* poj 1182 并查集 思路分析:让你分析这些话里面多少假的 只需要用 ...

  4. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

  5. D. Mahmoud and a Dictionary 种类并查集

    http://codeforces.com/contest/766/problem/D 所谓种类并查集,题型一般如下:给定一些基本信息给你,然后又给出一些信息,要求你判断是真是假.例如给出a和b支持不 ...

  6. C. Hongcow Builds A Nation 并查集

    http://codeforces.com/contest/745/problem/C 把他们并查集后, 其他没有连去government的点,全部放去同一个并查集,然后选择一个节点数最多的gover ...

  7. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集

    Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...

  8. HDU 6326.Problem H. Monster Hunter-贪心(优先队列)+流水线排序+路径压缩、节点合并(并查集) (2018 Multi-University Training Contest 3 1008)

    6326.Problem H. Monster Hunter 题意就是打怪兽,给定一棵 n 个点的树,除 1 外每个点有一只怪兽,打败它需要先消耗 ai点 HP,再恢复 bi点 HP.求从 1 号点出 ...

  9. CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)

    Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex c ...

随机推荐

  1. SpringBoot 多数据库支持:

    SpringBoot 多数据库支持: springboot2.0+mybatis多数据源集成 https://www.cnblogs.com/cdblogs/p/9275883.html Spring ...

  2. error C2338: You've instantiated std::aligned_storage<Len, Align> with an extended alignment (in other words, Align >

    报的完整错误为: error C2338: You've instantiated std::aligned_storage<Len, Align> with an extended al ...

  3. JavaScript之循环语句

    (1)while语句 while(条件){ 条件为真,进入循环体.出现0 null undefined false其中任意一种情况,条件即为假 循环体 } 案例: var n=0; var count ...

  4. js实现frame框架部分页面的刷新

    一.先来看一个简单的例子: 下面以三个页面分别命名为frame.html.top.html.bottom.html为例来具体说明如何做. frame.html 由上(top.html)下(bottom ...

  5. SQL----EXISTS 关键字

    转自:http://blog.sina.com.cn/s/blog_65dbc6df0100mvfx.html 1.EXISTS基本意思 英语解释就是存在,不过他的意思也差不多,相当于存在量词'З'. ...

  6. JavaWeb开发回顾总结【不断更新中】

    1.Java语言编程 2.Servlet & JSP(JavaServer Pages) JSPGet方法中文乱码,以上是我的一篇解决Get方法的文章. 关于编码的约定,从项目的一开始就要规定 ...

  7. Python学习笔记【1】

    1.%r和%s的区别 (1)stackflow 上面的一个解答 (2) x = "There are %d types of people." %10 binary = " ...

  8. pandas数据处理

    首先,数据加载 pandas提供了一些用于将表格型数据读取为DataFrame对象的函数,期中read_csv和read_table这两个使用最多. 1.删除重复元素 使用duplicated()函数 ...

  9. IDG资本

    https://baike.baidu.com/item/IDG/10412 美国国际数据集团(International Data Group) 是全世界最大的信息技术出版.研究.发展与风险投资公司 ...

  10. html布局-子div浮动后,父容器撑不开解决

    文章:子div撑不开父div的几种解决办法: 1,可以在所有子元素后增加<div style="clear:both;"></div> 2,给父容器增加样式 ...