CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP

题目传送门

https://codeforces.com/contest/1103/problem/D

题解

失去信仰的低水平选手的看题解的心路历程。


一开始看题目以为是选出一些数,每个数可以除掉一个不超过 \(k\) 的因数,使得被选出这些数\(\gcd\)\(1\)

错的有点离谱。然后想了半天,想了一个奇怪的思路结果没有任何优化空间(因为选择的数不固定无法直接确定所有的质因子)。

然后就开始看题解(事实上就算我没看错题目肯定也不会做)。

以下为搬运题解内容。


我们可以先求出初始的 \(\gcd\),它的质因子个数不超过 \(11\)。我们可以状压这些质因子。我们的目标是把这些质因子全部消灭掉。以下,令 \(c=11\)

暴力做法是枚举每一个数,枚举子集暴力转移,时间复杂度 \(O(n3^c)\)

下面就是一堆神奇的优化。

我们把每一个 \(a_i\) 都去掉不在初始 \(\gcd\) 的质因子中的质因子。可以证明(我不会)这样子以后,去重以后的 \(a_i\) 不超过 \(25000\) 个。我们只需要保留每一个 \(a_i\) 的对应的 \(e\) 值最小的 \(c\) 个就可以了。设这样以后的总数为 \(m \leq 25000c\)

然后,转移的时候,对于合法的相同的集合状态 \(s\),只有所需的 \(e\) 值最小的 \(c\) 个有用。(因为前 \(c-1\) 个对应的人可能被用来干掉别的质因子,但是第 \(c\) 个还不用就说不过去了)

这样,时间复杂度可以优化为 \(O(m2^c+c^23^c)\)


看题解 = 失去信仰。

但是水平低的选手是在没办法啊。


#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

const int N = 1e6 + 7;
const int M = 12000 * 11 + 7;
const int NP = (1 << 11) + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;

#define lowbit(x) ((x) & -(x))

int n, m, c, S;
int cont[NP];
ll g, k;
ll p[12], cnt[12], f[NP], dp[2][12][NP];

struct Orzthx {
	ll a, e;
	inline bool operator < (const Orzthx &b) { return a < b.a || (a == b.a && e < b.e); }
} a[N], b[M];

inline void ycl() {
	S = (1 << c) - 1;
	for (int i = 1; i <= n; ++i) {
		ll x = a[i].a, y = 1;
		for (int j = 1; j <= c; ++j)
			while (x % p[j] == 0) x /= p[j], y *= p[j];
		a[i].a = y;
	}
	std::sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; ++i) if (i == 1 || a[i].a != a[i - 1].a) {
		int cnt = 1;
		b[++m] = a[i];
		while (i < n && cnt < c && a[i].a == a[i + 1].a) b[++m] = a[++i], ++cnt;
	}
	std::sort(b + 1, b + m + 1, [](const Orzthx &a, const Orzthx &b) { return a.e < b.e; });
}

inline void work() {
	ycl();
	int now = 0, pre = 1;
	memset(dp[now], 0x3f, sizeof(dp[now]));
	dp[now][0][0] = 0;
	for (int i = 1; i <= m; ++i) {
		ll x = b[i].a;
		for (int j = 1; j <= c; ++j) {
			cnt[j] = 1;
			while (x % p[j] == 0) x /= p[j], cnt[j] *= p[j];
		}
		f[0] = 1;
		for (int s = 1; s <= S; ++s) f[s] = f[s ^ lowbit(s)] * cnt[std::__lg(lowbit(s)) + 1];
		std::swap(now, pre);
		memcpy(dp[now], dp[pre], sizeof(dp[now]));
		for (int s = S; s >= 0; --s) if (f[s] <= k && ++cont[s] <= c) {
			for (int j = 1; j <= c; ++j)
				for (int sta = S ^ s; sta; sta = (sta - 1) & (S ^ s)) smin(dp[now][j][s | sta], dp[pre][j - 1][sta] + b[i].e), assert((s & sta) == 0);
			smin(dp[now][1][s], b[i].e);
		}
	}
	ll ans = INF;
	for (int i = 0; i <= c; ++i) if (dp[now][i][S] != INF) smin(ans, i * dp[now][i][S]);
	if (ans != INF) printf("%I64d\n", ans);
	else puts("-1");
}

inline void init() {
	read(n), read(k);
	for (int i = 1; i <= n; ++i) read(a[i].a), g = std::__gcd(g, a[i].a);
	for (int i = 1; i <= n; ++i) read(a[i].e);
	ll gg = g;
	for (int i = 2, sp = sqrt(g); i <= sp; ++i) if (gg % i == 0) {
		while (gg % i == 0) gg /= i;
		p[++c] = i;
	}
	if (gg > 1) p[++c] = gg;
	assert(c <= 11);
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
posted @ 2019-10-30 17:55  hankeke303  阅读(162)  评论(0编辑  收藏  举报