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;
}
CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP的更多相关文章
- Codeforces Round #585 (Div. 2) E. Marbles(状压dp)
题意:给你一个长度为n的序列 问你需要多少次两两交换 可以让相同的数字在一个区间段 思路:我们可以预处理一个数组cnt[i][j]表示把i放到j前面需要交换多少次 然后二进制枚举后 每次选择一个为1的 ...
- Educational Codeforces Round 13 E. Another Sith Tournament 状压dp
E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...
- CF1103D Professional layer 状压DP
传送门 首先对于所有数求gcd并求出这个gcd含有的质因子,那么在所有数中,只有这一些质因子会对答案产生影响,而且对于所有的数,每一个质因子只会在一个数中被删去. 质因子数量不会超过\(11\),所以 ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp
题目链接: http://codeforces.com/contest/378/problem/E 题意: dota选英雄,现在有n个英雄,m个回合,两支队伍: 每一回合两个选择: b 1,队伍一ba ...
- 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 ...
- 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 ...
- [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)
[多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...
- Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...
随机推荐
- xsens melodic ros driver
sudo apt-get update sudo apt-get install ros-melodic-xsens-driver 设置数据输出: // 输出四元数,加速度.角速度.地磁 python ...
- MyRocks安装部署
参考:https://www.cnblogs.com/WonderHow/p/5621591.html CentOS 7.3 gflags:git clone https://github.com/g ...
- DeepFaceLab报错,integer division or modulo by zero
DeepFaceLab的集成环境在众多换脸软件中是做的最好的.但是使用过程也会出现一些错误,主要的错误有两个,一个是你配置太低OOM了,主要体现显存太低.第二个是版本不对应.比如你原先用的cuda9. ...
- 分布式消息队列 Celery 的最佳实践
目录 目录 不使用数据库作为 Broker 不要过分关注任务结果 实现优先级任务 应用 Worker 并发池的动态扩展 应用任务预取数 保持任务的幂等性 应用任务超时限制 善用任务工作流 合理应用 a ...
- 阶段3 1.Mybatis_09.Mybatis的多表操作_6 分析mybatis多对多的步骤并搭建环境
示例:用户和角色 一个用户可以有多个角色 一个角色可以赋予多个用户 步骤: 1.建立两张表:用户表,角色表 ...
- js监听当前页面再次加载
document.addEventListener("visibilitychange", function () { if (!document.hidden) { //处于当前 ...
- Delphi加密解密算法
// 加密方法一(通过密钥加密解密)function EncryptString(Source, Key: string): string;function UnEncryptString(Sourc ...
- zabbix添加主机后无法显示解决
第一次添加主机后显示正常,后来删除了主机,重新添加了一下主机再也无法显示主机,很苦恼,原来需要点击重设,
- itchat初步解读登录(转)
原文:https://blog.csdn.net/coder_pig/article/details/81357810 itchat的登录采取的是通过itchat.auto_login()这个函数来完 ...
- 【Linux开发】全面的framebuffer详解
全面的framebuffer详解 一.FrameBuffer的原理 FrameBuffer 是出现在 2.2.xx 内核当中的一种驱动程序接口. Linux是工作在保护模式下,所以用户态进程是无法象D ...