Codeforces 1043 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的更多相关文章
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces 1043 - A/B/C/D/E/F - (Undone)
链接:http://codeforces.com/contest/1043 A - Elections - [水水水水题] 题意: 我和另一个人竞争选举,共有 $n$ 个人投票,每个人手上有 $k$ ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
- 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 ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
- Codeforces 538 F. A Heap of Heaps
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
随机推荐
- Golang中map的三种声明方式和简单实现增删改查
package main import ( "fmt" ) func main() { test3 := map[string]string{ "one": & ...
- Golang的数组初始化方式及for-range遍历
package main import ( "fmt" ) func main() { ]], , } //定义并初始化长度为3的数组 fmt.Println(arr1) //[1 ...
- P2221 [HAOI2012]高速公路(线段树)
P2221 [HAOI2012]高速公路 显然答案为 $\dfrac{\sum_{i=l}^r\sum_{j=l}^{r}dis[i][j]}{C_{r-l+1}^2}$ 下面倒是挺好算,组合数瞎搞 ...
- Iris Classification on PyTorch
Iris Classification on PyTorch code # -*- coding:utf8 -*- from sklearn.datasets import load_iris fro ...
- eclipse在注释时候字体变成繁体字
输入法和java中的快捷键冲突了,按下ctrl+shift+F就切换回去了
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
- Java:The hierarchy of the type is inconsistent错误
错误 The type com.jiuqi.dna.ui.language.INLStringGroup cannot be resolved. It is indirectly referenced ...
- 【定制Android系统】Android O 在ROM中添加自己的 so 库(1)——Android.mk 与 Android.bp 的区别【转】
本文转载自: 版权声明:本文为博主原创文章,转载时请注明原作者及出处. https://blog.csdn.net/u014248312/article/details/82020204需求:在 ...
- shell编程(四)之循环控制语句(for,while,until,break,continue,case)
for循环 语法:for NAME in LIST; do 循环体 done 列表生成方式:1.整数列表 {start...end} $(seq,[start [step]] end)2.glob / ...
- 从0开始安装fedora23的笔记-- 以及使用fedora的常规问题
关于安装过程中的笔记本 触摸板 和鼠标的问题? 在安装过程中, 系统只认从usb接口插入的鼠标和触摸板上的 "左键和右键"按钮. 触摸板上的鼠标只支持"移动"的 ...