题目传送门

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. Unity Mathf And Transform Compent(一)

    Mathf类部分变量 辐射到度的转化函数,能够将弧度转化成度. Abs 能够求出绝对值 Atan 求出正切值x/y的弧度 Transform 组件中带有local 以父物体为坐标原点 global以世 ...

  2. 操作系统汇编语言之AT&T指令

    转载时格式有问题,大家看原版吧! 作者:EwenWanW  来源:CSDN  原文:https://blog.csdn.net/xiaoxiaowenqiang/article/details/805 ...

  3. 【Qt开发】V4L2 API详解 背景知识 打开设备设置参数

    www.linuxtv.org下,有篇文档详细讲解了V4L2相关知识和体系结构.是V4L2方面最全面的文档.可以通过它学习V4L2的一些思路和想法. http://www.linuxtv.org/do ...

  4. 国家授时中心的NTP服务器地址 210.72.145.44

    国家授时中心的NTP服务器地址 210.72.145.44

  5. TS学习笔记----(一)基础类型

    布尔值: boolean let isDone: boolean = false; 数字: number 和JavaScript一样,TS里的所有数字都是浮点数. 支持十进制和十六进制字面量,TS还支 ...

  6. 刘铁猛-深入浅出WPF-系列资源汇总

    首先奉上原作者刘铁猛博客地址:http://www.cnblogs.com/prism/ 作者讲的很不错,没有之一,另外作者出了一本书,希望大家支持. 送上全套高清晰视频教程(我注册了3个51cto的 ...

  7. vue-devtools安装以后,勾选了“允许访问文件网址”之后还是无法使用

    勾选了“允许访问文件网址”,还是无法使用: Vue.js is detected on this page. Devtools inspection is not available because ...

  8. 005 gcc 的简单使用

    0. 前言 本文主要讲关于 gcc 的几种编译方式 不妨设文件名为 test.c 1. 方法一 $ gcc test.c (Windows OS)编译成功的话,没有回馈,在 test.c 所在的文件夹 ...

  9. 搜索专题: HDU1258Sum It Up

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  10. HTTPS和HTTP的区别,http协议的特征

    http协议传输的数据都是没有经过加密的,也就是明文,所以http用于传输数据并不安全.而https是是使用了ssl(secure socket layer)协议+http协议构成的可加密传输,身份认 ...