F - Make It One

思路:

dp + 容斥

首先, 答案不会超过7, 因为前7个质数的乘积大于3e5(最坏的情况是7个数, 每个数都缺少一个不同的因子)

所以从1到7依次考虑

dp[i][j]: 表示选取i个数且gcd==j的方案数

dp[i][j] = C(cntj, i) - ∑dp[i][k] (其中cntj表示ai中是j的倍数的个数, k表示所有j的倍数)

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 3e5 + ;
const int MOD = 1e9 + ;
int a[N], cnt[N], mul_of[N];
int dp[][N];
int fac[N], invfac[N];
LL q_pow(LL n, LL k) {
LL res = ;
while(k) {
if(k&) res = (res * n) % MOD;
n = (n * n) % MOD;
k >>= ;
}
return res;
}
void init() {
fac[] = ;
for (int i = ; i < N; i++) fac[i] = (1LL * fac[i-] * i) % MOD;
invfac[N-] = q_pow(fac[N-], MOD-);
for (int i = N-; i >= ; i--) invfac[i] = (1LL * invfac[i+] * (i+)) % MOD;
}
LL C(int n, int m) {
if(n < m) return ;
return (1LL * fac[n] * invfac[m]) % MOD * invfac[n-m] % MOD;
}
int main() {
int n;
init();
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", &a[i]), cnt[a[i]]++;
for (int i = ; i < N; i++) {
for (int j = i; j < N; j += i) {
mul_of[i] += cnt[j];
}
}
for (int i = ; i <= ; i++) {
for (int j = N-; j > ; j--) {
int sum = ;
for (int k = *j; k < N; k += j) sum = (sum + dp[i][k]) % MOD;
dp[i][j] = (C(mul_of[j], i) - sum) % MOD;
}
if(dp[i][]) {
printf("%d\n", i);
return ;
}
}
printf("-1\n");
return ;
}

Codeforces 1043 F - Make It One的更多相关文章

  1. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  2. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  3. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  4. Codeforces 1043 - A/B/C/D/E/F - (Undone)

    链接:http://codeforces.com/contest/1043 A - Elections - [水水水水题] 题意: 我和另一个人竞争选举,共有 $n$ 个人投票,每个人手上有 $k$ ...

  5. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  6. Codeforces 622 F. The Sum of the k-th Powers

    \(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...

  7. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  8. Codeforces 538 F. A Heap of Heaps

    \(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...

  9. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

随机推荐

  1. linux时间修改-hwclock和date

    修改系统时间date 设定日期:date -s 月/日/年,例如设定日期为2018年12月1日,date -s 12/01/2018(年也可以是两位) 设定时间:date -s hh:mm:ss,例如 ...

  2. mysql Out of range value adjusted for column导致Warning(1265)Data truncated for column 'column_name' at row 1

    今天下午,我们的一个开发来找我,说线上有个环境报了"Warning(1265)Data truncated for column 'column_name' at row 1",定 ...

  3. centos-6.5安装部署LNMP环境

    安装部署前,确保安装了gcc和gcc-c++ 系统信息: [root@zww ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@ ...

  4. (4opencv)OpenCV PR 成功的收获和感悟

     2018-09-12,第一次对OpenCV PR成功 https://github.com/opencv/opencv/pull/12206 <find innercircle of cont ...

  5. CEF 文件下载

    转载:https://blog.csdn.net/liuyan20092009/article/details/53819473?locationNum=6&fps=1 转载:https:// ...

  6. Python3 tkinter基础 Button command 单击按钮 在console中打印文本

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. LVM基本应用,扩展及缩减实现

    一.基本概念 如上图所示:底层PV(物理卷可能是硬盘设备,分区或RAID等),一个或多个PV组织成一个VG(卷组),卷组是不能直接格式化使用的,所以在VG之上,还需要创建LV进行格式化使用.VG在逻辑 ...

  8. SpringBoot Redis使用fastjson进行序列化

    在使用spring-data-redis,默认情况下是使用org.springframework.data.redis.serializer.JdkSerializationRedisSerializ ...

  9. Jenkins serving Cake: our recipe for Windows

    https://novemberfive.co/blog/windows-jenkins-cake-tutorial/ Where we started, or: why Cake took the ...

  10. 【问题解决:死锁】Lock wait timeout exceeded; try restarting transaction的问题

    执行数据删除操作时一直超时并弹出Lock wait timeout exceeded; try restarting transaction错误 解决办法 1.先查看数据库的事务隔离级别 select ...