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. ubuntu文件名乱码convmv和iconv

    sudo apt install convmv sudo convmv -f gbk -t utf- -r --notest /home/pm/Desktop/p Linux下两个工具convmv和i ...

  2. 论证与测试 + 用EA画uml

    论证与测试,谁才是真正的不二法门 第十三次作业的时候,我们开始使用Junit对代码进行测试,主要是测试代码的覆盖率,以及分支的覆盖率.(主要是检查JSF写的是否是符合规范,……). 这里我给出我测试的 ...

  3. RAC +MVVM

    https://blog.csdn.net/capf_sam/article/details/60960530 https://blog.csdn.net/capf_Sam/article/detai ...

  4. python --- 05 字典 集合

    一.字典 可变数据类型 {key:value}形式   查找效率高   key值必须是不可变的数据类型 1.增删改查 1).增    dic["新key"] = "新va ...

  5. SSM集成activiti6.0错误集锦(二)

    项目环境 Maven构建 数据库:Orcle12c 服务器:Tomcat9 <java.version>1.8</java.version> <activiti.vers ...

  6. linux shell的for循环语法是怎样的?

    答:如下: ;i<100;i++)) do echo "i=${i}" done

  7. noip模拟【tea】

    tea [题目描述]有n个容量为V的瓶子,第i个瓶子中装着a[i]个单位的tea,使所有瓶子内的tea在不 超过其容量的前提下,非空的瓶子最少.在一个单位时间内,可以同时将多个瓶子中的tea倒入另外多 ...

  8. 【做题】agc008f - Black Radius——计数&讨论&思维

    原文链接 https://www.cnblogs.com/cly-none/p/9794411.html \[ \newcommand{\stif}[2]{\left[ \begin{matrix} ...

  9. JZ2440存储管理器--SDRAM

     为了cpu访问外部设备,ARM提供一个存储管理器部件,提供访问外部设备的所需的信号(对SDRAM.网卡.nor等设备进行初始化,以便存储器管理器配合CPU进行与外设数据通讯).   CPU通常读写一 ...

  10. Entity Framework Core

    Entity Framework是一种支持 .NET 开发人员使用 .NET 对象处理数据库的对象关系映射程序 (O/RM). 它不要求提供开发人员通常需要编写的大部分数据访问代码. Entity F ...