CF1103D Professional layer 状压DP
首先对于所有数求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的更多相关文章
- CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP
题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
- 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP
[BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...
随机推荐
- Android开发学习之RecyclerView
1.在app/build.gradle中添加RecyclerView依赖 implementation 'com.android.support:recyclerview-v7:28.0.0' 注意依 ...
- ES搜索引擎集群模式搭建【Kibana可视化】
一.简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎(与Solr类似),基于RESTful web接口.Elasticsearch是用Ja ...
- Linux中DNS的设置
1.查看本机的域名 hostname 2.修改DNS 临时修改: hostname desktop0.example.com 永久修改: hostnamectl set-hostname deskto ...
- A problem has been detected and windows has been shut down to prevent damage to your computer.他么啥意思?看这里!【蓝屏】
A problem has been detected and windows has been shut down to prevent damage to your computer. 检测到问 ...
- CSS杂谈(2)
opacity 属性设置元素的不透明级别. 语法 opacity: value|inherit; 值 描述 value 规定不透明度.从 0.0 (完全透明)到 1.0(完全不透明). i ...
- jdk8新特性表达式1
Jdk8中有好多新的特性,比如引入Lambda,简化代码的书写等等 我们先看一个关于Lambda的使用 /*** 输出list */@Testpublic void test() { String[] ...
- php快速定位当前调用的类的位置
php快速定位当前调用的类的位置 $func = new ReflectionMethod('类名', '方法名'); $start = $func->getStartLine() - 1; $ ...
- JavaScript对象原型写法详解
体现对象原型分步式写法 //原型分步式写法 //构造函数 function Person(){} //对象原型 Person.prototype.name = 'Avensatr'; Pers ...
- YYModel HandyJson
数学基础: 以类型集合空间为基础,多阶向量结构间的同构映射: 若两个数学结构之间存在同构映射,那么这两个结构叫做是同构的. 基于集合空间的映射: 原理: 1.根据类型名称获得类型的元信息: 2.根据映 ...
- [Python] Python 100例
题目1:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. #程序源 ...