传送门


首先对于所有数求gcd并求出这个gcd含有的质因子,那么在所有数中,只有这一些质因子会对答案产生影响,而且对于所有的数,每一个质因子只会在一个数中被删去。

质因子数量不会超过\(11\),所以考虑暴力的状压DP:设\(f_{i,j,k}\)表示前\(i\)个数中选出\(j\)个数并删去了集合为\(k\)的质因子的最小代价,转移枚举子集并计算转移是否合法(即第\(i\)个数中当前集合质因子的乘积是否超过\(K\)),复杂度\(O(nm3^m)\)

考虑优化:对每一个数将不能对答案造成影响的因子全部丢掉。对于剩下的数相同的一些数,最多只会选按权值从小到大排序之后前质因子个数个产生贡献。

再加上一些奇怪的优化就可以快很多了。时间复杂度\(O(?m3^m)\)

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<map>
//This code is written by Itst
using namespace std; #define int long long
inline int read(){
int a = 0;
char c = getchar();
while(!isdigit(c))
c = getchar();
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return a;
} inline int gcd(int a , int b){
int r = a % b;
while(r){a = b; b = r; r = a % b;}
return b;
} #define st first
#define nd second
const int MAXN = 1e6 + 7;
struct zt{
int num , w;
bool operator <(const zt a)const{return w < a.w;}
}now[MAXN];
vector < int > in;
int N , K , allGcd , dp[12][1 << 11] , calc[1 << 11];
map < int , vector < int > > lsh; signed main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("out","w",stdout);
#endif
N = read(); K = read();
for(int i = 1 ; i <= N ; ++i){
now[i].num = read();
allGcd = gcd(allGcd , now[i].num);
}
for(int i = 1 ; i <= N ; ++i) now[i].w = read();
if(allGcd == 1) return puts("0") , 0;
for(int i = 2 ; i <= 1e6 && 1ll * i * i <= allGcd ; ++i)
if(allGcd % i == 0){
in.push_back(i);
while(allGcd % i == 0) allGcd /= i;
}
if(allGcd - 1) in.push_back(allGcd);
int M = in.size();
for(int i = 1 ; i <= N ; ++i){
int z = 1;
for(auto j : in)
while(now[i].num % j == 0){
now[i].num /= j;
z *= j;
}
lsh[z].push_back(now[i].w);
}
memset(dp , 2 , sizeof(dp));
dp[0][0] = 0;
for(auto t : lsh){
int x = t.st;
sort(t.nd.begin() , t.nd.end());
if(t.nd.size() > M) t.nd.resize(M);
vector < int > num;
for(int i = 0 ; i < 1 << M ; ++i){
int times = 1 , p = x;
for(int j = 0 ; j < M ; ++j)
if(i & (1 << j))
while(p % in[j] == 0){
p /= in[j];
times *= in[j];
}
num.push_back(times);
}
for(auto p : t.nd){
bool flg = 0;
for(int i = M - 1 ; i >= 0 ; --i)
for(int j = 0 ; j < 1 << M ; ++j)
if(dp[i][j] <= 1e12){
int all = (1 << M) - 1 - j;
for(int k = all ; k ; k = (k - 1) & all)
if(num[k] <= K)
if(dp[i + 1][j | k] > dp[i][j] + p){
dp[i + 1][j | k] = dp[i][j] + p;
flg = 1;
}
}
if(!flg) break;
}
}
int minN = 1e18;
for(int i = 1 ; i <= M ; ++i)
minN = min(minN , dp[i][(1 << M) - 1] * i);
if(minN > 1e12) cout << -1;
else cout << minN;
return 0;
}

CF1103D Professional layer 状压DP的更多相关文章

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

    题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...

  2. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  3. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  4. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  5. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  6. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  7. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  8. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  9. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

随机推荐

  1. Oracle笔记----oracle数字类型number自增

    创建序列 create sequence seq_student start increment maxvalue nominvalue nocycle nocache; 创建触发器 create o ...

  2. mysql----SELECT names/zh

    < SELECT names   Language: English  • 中文 name continent Afghanistan Asia Albania Europe Algeria A ...

  3. 排序算法----快速排序java

    快速排序是对冒泡排序的一种改进,平均时间复杂度是O(nlogn) import java.util.Arrays; import java.util.Scanner; public class tes ...

  4. [20181105]再论12c set feedback only.txt

    [20181105]再论12c set feedback only.txt --//前一阵子的测试,链接:http://blog.itpub.net/267265/viewspace-2216290/ ...

  5. iis 限制动态IP地址访问次数

    An IP Address Blocking HttpModule for ASP.NET in 9 minutes namespace YourModuleNameHere 10 { 11 publ ...

  6. django安装与使用

    django安装与使用 --更新中 安装 我这里采用pip安装 pip install django 创建django工程 创建好的工程,会在当前目录.下 django-admin startproj ...

  7. C# -- 正则表达式匹配字符之含义

    C#正则表达式匹配字符之含义 1.正则表达式的作用:用来描述字符串的特征. 2.各个匹配字符的含义: .   :表示除\n以外的单个字符 [ ]  :表示在字符数组[]中罗列出来的字符任意取单个 | ...

  8. nginx基本配置与参数说明

    user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes  1;   #全局错误日志及PID文件 #error_log  logs/error.log; # ...

  9. 解决IE8不支持html5标签最好解决办法?

    完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法:HTML5的语义化标签以及属性,可以让开发者非常方便地实现清晰的web页面布局,加上CSS3的效果渲染,快速建立丰富灵活的web页面显 ...

  10. ES5-ES6-ES7_async函数

    async 函数概述 async/await应该是目前最简单的异步方案,ES7 中新增了 async/await 两个关键词. async 可以声明一个异步函数,此函数需要返回一个 Promise 对 ...