大意: 给定$n$元素序列$a$, 现在想要让$gcd(a_1,a_2,...,a_n)=1$. 对于每个$a_i$可以除以一个不超过$k$的因子, 代价为$e_i$, 假设一共选择了$x$个元素去除, 代价和为$y$, 求$xy$的最小值.

设$g=gcd(a_1,a_2,...,a_n)=p_1^{\alpha_1}p_2^{\alpha_2}\cdots p_r^{\alpha_r}$, 有$1\le r \le 11$

我们考虑最优解的结构, 对于一个数$a_i$, 它的某个素因子$p_k$想要对答案产生贡献则必须将$p_k$全部除去, 所以每个数都可以用一个二进制状态表示, 并且显然最优解至多选择$r$个$a_i$. 这样的话就可以得到暴力代码, 转移复杂度是$O(nr2^{2r})$, 显然过不去

//dp[i][j][k] 表示前i个数取j个数状态为k时的最小值
memset(dp,0x3f,sizeof dp);
dp[0][0][0]=0;
REP(i,1,n) {
//预处理出每个状态需要除的数num
REP(j,1,S) if (num[j]<=k) {
REP(x,0,S) {
REP(t,1,r) {
chkmin(dp[i][t][x|j],dp[i-1][t-1][x]+a[i].e);
}
}
}
}
ll ans = INF;
REP(i,1,r) if (dp[n][i][S]!=INF) ans=min(ans, dp[n][i][S]*i);

下面考虑优化

  • 对于每个$a_i$, 对于它不在$g$内的素因子直接除去不考虑, 打表发现这样不同的$a_i$不超过M=12000
  • 对于每个不同的$a_i$只取$e$前$r$小的, 并且每个状态只至多转移$r$次
  • 枚举补集的子集, 可将$2^{2r}$优化为$3^r$

这样转移的复杂度就为$O(Mr2^r+r^23^r)$

最后要注意最大素因子是$O(max{a_i})$的, 要开long long 存!!

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10; int n, w[N];
ll k, g;
struct _ {
ll a;
int e;
bool operator < (const _ &rhs) const {
return e<rhs.e;
}
} a[N];
vector<ll> A;
map<ll,int> vis;
ll dp[2][12][1<<11], num[1<<11]; void factor(ll g) {
int mx = sqrt(g+0.5);
REP(i,2,mx) if (g%i==0) {
A.pb(i);
while (g%i==0) g/=i;
}
if (g>1) A.pb(g);
} void chkmin(ll &x, ll y) {x=min(x,y);} int main() {
scanf("%d%lld", &n, &k);
REP(i,1,n) scanf("%lld", &a[i].a),g=gcd(g,a[i].a);
if (g==1) return puts("0"),0;
REP(i,1,n) scanf("%d", &a[i].e);
sort(a+1,a+1+n);
factor(g);
int r = A.size(), S = (1<<r)-1, cur = 0;
memset(dp[0],0x3f,sizeof dp[0]);
dp[0][0][0]=0;
REP(i,1,n) {
ll b = 1;
REP(j,0,r-1) if (a[i].a%A[j]==0) {
ll t = 1;
while (a[i].a%A[j]==0) a[i].a/=A[j],t*=A[j];
num[1<<j] = t;
b *= t;
}
if (++vis[b]>r) continue;
num[0] = 1;
cur ^= 1;
memcpy(dp[cur],dp[cur^1],sizeof dp[cur]);
REP(j,1,S) {
num[j] = num[j^j&-j]*num[j&-j];
if (num[j]>k) continue;
if (++vis1[j]>r) continue;
ll x = ~j&S;
PER(t,0,r-1) {
for (int y=x; y; y=(y-1)&x) if (dp[cur^1][t][y]!=INF) {
chkmin(dp[cur][t+1][y^j],dp[cur^1][t][y]+a[i].e);
}
chkmin(dp[cur][t+1][j],dp[cur^1][t][0]+a[i].e);
}
}
}
ll ans = INF;
REP(i,1,r) if (dp[cur][i][S]!=INF) {
ans = min(ans, dp[cur][i][S]*i);
}
printf("%lld\n", ans==INF?-1:ans);
}

Professional layer CodeForces - 1103D (状压,gcd)的更多相关文章

  1. Vladik and cards CodeForces - 743E (状压)

    大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[1,8]的数字, 在子序列中的出现次数差不超过1, 且子序列中相同数字连续. 正解是状压dp, 先二分转为判断[1,8]出现次数>=x ...

  2. Keyboard Purchase CodeForces - 1238E (状压)

    大意: 给定串$s$, 字符集为字母表前$m$个字符, 求一个$m$排列$pos$, 使得$\sum\limits_{i=2}^n|{pos}_{s_{i-1}}-{pos}_{s_{i}}|$最小. ...

  3. CodeForces 11D(状压DP 求图中环的个数)

    Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...

  4. Clear The Matrix CodeForces - 903F (状压)

    大意: 给定4行的棋盘以及4种大小的正方形方块, 每种各有一定花费, 每次可以选一种方块放在棋盘上, 棋盘对应格子全变为'.', 求最少花费使得棋盘全部变成'.' 状压基本操作练习, 状态取12位, ...

  5. Pollywog CodeForces - 917C (状压)

    链接 大意: 一共n个格子, 初始$x$只蝌蚪在前$x$个格子, 每次最左侧的蝌蚪向前跳, 跳跃距离在范围[1,k], 并且每只蝌蚪跳跃都有一定花费, 有$q$个格子上有石头, 若有蝌蚪跳到某块石头上 ...

  6. Codeforces 678E 状压DP

    题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...

  7. Codeforces 8C 状压DP

    题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...

  8. Codeforces 1215E 状压DP

    题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...

  9. Crisp String CodeForces - 1117F (状压)

    #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> ...

随机推荐

  1. 好用的firefox浏览器、geckodriver驱动的版本组合(55 和 0.19.1)

    试过很多的firefox浏览器版本和geckodriver的组合,有时候好用,有时候不好用,现在确定了一个好用的版本组合,记录一下: firefox:版本55,而且此版本可以用firebug geck ...

  2. Linux服务器配置---ftp限制带宽

    限制带宽 ftp服务器可以设置每个用户的带宽,这样根据实际需求来分配,更加充分的利用系统资源.带宽通过参数“anon_max_rate“和”local_max_rate“来设置,这两个参数在配置文件中 ...

  3. 利用构造函数对canvas里面矩形与扇形的绘制进行一个封装

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Linux系统安装telnet以及xinetd服务

    Linux系统安装telnet以及xinetd服务 一.安装telnet 1.检测telnet-server的rpm包是否安装 # rpm -qa telnet-server 若无输入内容,则表示没有 ...

  5. SIFT在OpenCV中的调用和具体实现(HELU版)

    前面我们对sift算法的流程进行简要研究,那么在OpenCV中,sift是如何被调用的?又是如何被实现出来的了? 特别是到了3.0以后,OpenCV对特征点提取这个方面进行了系统重构,那么整个代码结构 ...

  6. 20145322何志威 Exp7 网络欺诈技术防范

    20145322何志威 Exp7 网络欺诈技术防范 一 实践过程记录 简单应用SET工具建立冒名网站 1 确保kali和靶机能ping通: 2 为了使得apache开启后,靶机通过ip地址可以直接访问 ...

  7. [LeetCode] 701. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  8. Spring Boot + thymeleaf 实现文件上传下载

    参考博客:https://juejin.im/post/5a326dcaf265da431048685e 技术选型:spring boot 2.1.1+thymeleaf+maven 码云地址:htt ...

  9. luogu P3387 【模板】缩点

    题目 好久没法博客了 这次就水个板子题目吧 tarjan缩点之后重新建图 而且边权应该都是正的(要不我怎么能这么轻松水过去) 在新图上记忆化一下就好了 f[i] 表示 开头选i这个点 的 路径最大值 ...

  10. 启动Sql server的服务CMD命令

    启动:net start mssqlserver 停止:net stop mssqlserver