Codeforces 1043 F - Make It One

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 + 5;
const int MOD = 1e9 + 7;
int a[N], cnt[N], mul_of[N];
int dp[10][N];
int fac[N], invfac[N];
LL q_pow(LL n, LL k) {
    LL res = 1;
    while(k) {
        if(k&1) res = (res * n) % MOD;
        n = (n * n) % MOD;
        k >>= 1;
    }
    return res;
}
void init() {
    fac[0] = 1;
    for (int i = 1; i < N; i++) fac[i] = (1LL * fac[i-1] * i) % MOD;
    invfac[N-1] = q_pow(fac[N-1], MOD-2);
    for (int i = N-2; i >= 0; i--) invfac[i] = (1LL * invfac[i+1] * (i+1)) % MOD;
}
LL C(int n, int m) {
    if(n < m) return 0;
    return (1LL * fac[n] * invfac[m]) % MOD * invfac[n-m] % MOD;
}
int main() {
    int n;
    init();
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), cnt[a[i]]++;
    for (int i = 1; i < N; i++) {
        for (int j = i; j < N; j += i) {
            mul_of[i] += cnt[j];
        }
    }
    for (int i = 1; i <= 7; i++) {
        for (int j = N-1; j > 0; j--) {
            int sum = 0;
            for (int k = 2*j; k < N; k += j) sum = (sum + dp[i][k]) % MOD;
            dp[i][j] = (C(mul_of[j], i) - sum) % MOD;
        }
        if(dp[i][1]) {
            printf("%d\n", i);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}

 

posted @ 2018-11-12 16:28  Wisdom+.+  阅读(296)  评论(0编辑  收藏  举报