题意

题目链接

给出\(n\)个数,问最少选几个数,使他们的\(gcd = 1\)

Sol

好神仙啊qwq。

首先,如果答案存在,那么最多为\(7\)(因为前\(7\)个质数乘起来\(>= 3e5\))

考虑dp,设\(f[i][j]\)表示选了\(i\)个数,他们\(gcd = j\)的方案数!

没错是方案数!

那么我们只要最后考虑一下\(f[i][1]\)是否有解就行了

设\(cnt[i]\)表示有多少个\(a_i\)存在\(i\)这个约数

转移的时候\(f[i][j] = C_{cnt[j]}^i - f[i][j * k], k >= 2\)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
#include<tr1/unordered_map>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define ull unsigned long long
#define rg register
#define pt(x) printf("%d ", x);
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
//char obuf[1<<24], *O = obuf;
//void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + '0';}
//#define OS *O++ = ' ';
using namespace std;
//using namespace __gnu_pbds;
const int MAXN = 3e5 + 11, INF = 1e9 + 10, mod = 998244353, Mx = 3e5;
const double eps = 1e-9;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, a[MAXN], f[12][MAXN], cnt[MAXN], fac[MAXN], ifac[MAXN];
int add(int x, int y) {
if(x + y < 0) return x + y + mod;
return x + y >= mod ? x + y - mod : x + y;
}
int mul(int x, int y) {
return 1ll * x * y % mod;
}
int C(int N, int M) {
if(N < M) return 0;
return mul(fac[N], mul(ifac[M], ifac[N - M]));
}
int fp(int a, int p) {
int base = 1;
while(p) {
if(p & 1) base = mul(base, a);
a = mul(a, a); p >>= 1;
}
return base;
}
main() {
N = read();
for(int i = 1; i <= N; i++) {
a[i] = read(), cnt[a[i]]++, f[1][a[i]]++;
if(a[i] == 1) {puts("1"); return 0;}
}
fac[0] = 1; for(int i = 1; i <= N; i++) fac[i] = mul(i, fac[i - 1]);
ifac[N] = fp(fac[N], mod - 2);
for(int i = N; i >= 1; i--) ifac[i - 1] = mul(i, ifac[i]);
for(int i = 1; i <= Mx; i++)
for(int j = i + i; j <= Mx; j += i) cnt[i] += cnt[j];
for(int i = 2; i <= 11; i++) {
for(int j = Mx; j >= 1; j--) {
f[i][j] = C(cnt[j], i);
for(int k = j + j; k <= Mx; k += j) f[i][j] = add(f[i][j], -f[i][k]);
}
if(f[i][1] > 0) {printf("%d", i); return 0;}
}
puts("-1");
return 0;
}

cf1043F. Make It One(dp 容斥原理)的更多相关文章

  1. 【uoj#37/bzoj3812】[清华集训2014]主旋律 状压dp+容斥原理

    题目描述 求一张有向图的强连通生成子图的数目对 $10^9+7$ 取模的结果. 题解 状压dp+容斥原理 设 $f[i]$ 表示点集 $i$ 强连通生成子图的数目,容易想到使用总方案数 $2^{sum ...

  2. 【bzoj2560】串珠子 状压dp+容斥原理

    题目描述 有 $n$ 个点,点 $i$ 和点 $j$ 之间可以连 $0\sim c_{i,j}$ 条无向边.求连成一张无向连通图的方案数模 $10^9+7$ .两个方案不同,当且仅当:存在点对 $(i ...

  3. 【bzoj2339】[HNOI2011]卡农 dp+容斥原理

    题目描述 题解 dp+容斥原理 先考虑有序数列的个数,然后除以$m!$即为集合的个数. 设$f[i]$表示选出$i$个集合作为满足条件的有序数列的方案数. 直接求$f[i]$较为困难,考虑容斥,满足条 ...

  4. 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)

    P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...

  5. [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩)

    [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩) 题面 给出一棵树和一个图,点数均为n,问有多少种方法把树的节点标号,使得对于树上的任意两个节点u,v,若树上u ...

  6. HDU5838 Mountain(状压DP + 容斥原理)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...

  7. 【BZOJ-2669】局部极小值 状压DP + 容斥原理

    2669: [cqoi2012]局部极小值 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 561  Solved: 293[Submit][Status ...

  8. 51Nod1518 稳定多米诺覆盖 动态规划 插头dp 容斥原理

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1518.html 题目传送门 - 51Nod1518 题意 51Nod真是个好OJ ,题意概括的真好, ...

  9. BZOJ 1042 硬币购物(背包DP+容斥原理)

    可以看出这是个多重背包,运用单调队列优化可以使每次询问达到O(s).这样总复杂度为O(s*tot). 会TLE. 因为改题的特殊性,每个硬币的币值是不变的,变的只是每次询问的硬币个数. 我们不妨不考虑 ...

随机推荐

  1. js判断浏览器类型以及语言

    1.检查是否是移动端(Mobile).ipad.iphone.微信.QQ等 <script type="text/javascript"> //判断访问终端 var b ...

  2. Leetcode 856. Score of Parentheses 括号得分(栈)

    Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...

  3. 《[MySQL技术内幕:SQL编程》读书笔记

    <[MySQL技术内幕:SQL编程>读书笔记 2019年3月31日23:12:11 严禁转载!!! <MySQL技术内幕:SQL编程>这本书是我比较喜欢的一位国内作者姜承尧, ...

  4. 963 AlvinZH打怪刷经验(背包DP大作战R)

    963 AlvinZH打怪刷经验 思路 这不是一道普通的01背包题.大家仔细观察数据的范围,可以发现如果按常理来的话,背包容量特别大,你也会TLE. 方法一:考虑01背包的一个常数优化----作用甚微 ...

  5. 1.ajax简单实现异步交互

    效果:点击获取信息 testAjax.jsp: <%@ page language="java" contentType="text/html; charset=U ...

  6. [转] vagrant系列(2):使用Vagrantfile实现集成预安装

    在我们的开发目录下,有一个文件Vagrantfile,里面包含有大量的配置信息,主要包括三个方面:虚拟机配置.SSH配置.基础配置.Vagrant是使用Ruby开发的,所以它的配置语法也是Ruby的, ...

  7. annotation-config和component-scan

    以前学到<context:annotation-config></context:annotation-config>和<context:component-scan b ...

  8. 使用Filter解决登录中乱码问题

    使用Filter解决登录中乱码问题 衔接:https://www.cnblogs.com/zst18980621948/p/10654088.html 1.工作目录 新增Filter类 2.Login ...

  9. 3dsmax2013卸载/安装失败/如何彻底卸载清除干净3dsmax2013注册表和文件的方法

    3dsmax2013提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dsmax2013失败提示3dsmax2013安装未完成,某些产品无法安装,也有时候想重新 ...

  10. volatile和synchronized与lock的理解

    volatile 特征: a:可见性:一个线程修改了某个共享变量的值,其他线程能够立马得知这个修改. b:禁止特定的处理器重排序. volatile的内存语义: 1.当写一个volatile变量的时候 ...