题目传送门

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;
}

CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP的更多相关文章

  1. Codeforces Round #585 (Div. 2) E. Marbles(状压dp)

    题意:给你一个长度为n的序列 问你需要多少次两两交换 可以让相同的数字在一个区间段 思路:我们可以预处理一个数组cnt[i][j]表示把i放到j前面需要交换多少次 然后二进制枚举后 每次选择一个为1的 ...

  2. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  3. CF1103D Professional layer 状压DP

    传送门 首先对于所有数求gcd并求出这个gcd含有的质因子,那么在所有数中,只有这一些质因子会对答案产生影响,而且对于所有的数,每一个质因子只会在一个数中被删去. 质因子数量不会超过\(11\),所以 ...

  4. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  5. Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp

    题目链接: http://codeforces.com/contest/378/problem/E 题意: dota选英雄,现在有n个英雄,m个回合,两支队伍: 每一回合两个选择: b 1,队伍一ba ...

  6. Codeforces Beta Round #8 C. Looking for Order 状压dp

    题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...

  7. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  8. [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)

    [多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...

  9. Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...

随机推荐

  1. LinkedList Stack

  2. saml2协议sp-initial登录过程

    登录过程如下所示: 一次完整的saml认证过程,包括一次samlrequest和samlresponse, 首先用户如果想访问一个sp,sp会先检验用户是否登录,如果用户已经登录,即可以正常访问sp的 ...

  3. 使用 Dom4j 对XML操作!!!

    转自:http://blog.csdn.net/redarmy_chen/article/details/12969219 dom4j是一个Java的XML API,类似于jdom,用来读写XML文件 ...

  4. k8s架构

    master节点 k8s的集群由master和node组成,节点上运行着若干k8s服务. master节点之上运行着的后台服务有kube-apiserver .kube-scheduler.kube- ...

  5. es为什么要取消type? 或者为什么一个index下多个type会有问题

    同一个index下的不同的type下的相同的filed,在同一个index下其实会被认为是同一个filed. 否则,不同type中的相同字段名称就会在处理中出现冲突的情况,导致Lucene处理效率下降

  6. 【ABAP系列】SAP ABAP BDC_OKCODE 解释

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP BDC_OKC ...

  7. word2vec (CBOW、分层softmax、负采样)

    本文介绍 wordvec的概念 语言模型训练的两种模型CBOW+skip gram word2vec 优化的两种方法:层次softmax+负采样 gensim word2vec默认用的模型和方法 未经 ...

  8. dbvisualizer安装

    1. 下载DbVisualizer安装包. 2.解压. 无论是哪个版本的dbvisualizer破解版, 都可以找到安装程序(例dbvis_windows-x64_10_0_10.exe), dbvi ...

  9. jenkins自动化测试Email Extension邮件模板 及可用参数TEST_COUNTS ,FAILED_TESTS详细说明

    先列出模板内容: <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <t ...

  10. eclipse maven 项目突然所有的JS方法都失效了

    原因:JS 或者 jQuery 有严重的语法错误