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 Studio 之 注释模板
我们看有的项目的文档,会发现它的注释写的很好看,很规范. 而如果你的代码注释还是在用简单的 // 或者 /**/ ,那就学习一下在Android Studio中如何进行注释模板的设置 首先看下面代码 ...
- Android内存优化(一)Dalvik虚拟机和ART虚拟机对比
1.概述 Android4.4以上开始使用ART虚拟机,在此之前我们一直使用的Dalvik虚拟机,那么为什么Google突然换了Android运行的虚拟机呢?答案只有一个:ART虚拟机更优秀. 2.D ...
- Hibernate设置事务的隔离级别
方式有两种: 1)修改配置文件hibernate.cfg.xml实现 <hibernate-configuration> <session-factory> ...... &l ...
- Spring MVC 请求映射 (二)
完整的项目案例: springmvc.zip 目录 实例 项目结构: 一.配置web.xml <?xml version="1.0" encoding="UTF-8 ...
- python3+正则表达式爬取 猫眼电影
'''Request+正则表达式抓取猫眼电影TOP100内容''' import requests from requests.exceptions import RequestException i ...
- CSipIm断网重连崩溃原因分析
断网重连之后的操作流程 拨打电话 ->调用SipService.makeCallOptions(),先重启一遍SipService保证PjSipService的各参数正常,尤其是create参数 ...
- C# -- 正则表达式匹配字符之含义
C#正则表达式匹配字符之含义 1.正则表达式的作用:用来描述字符串的特征. 2.各个匹配字符的含义: . :表示除\n以外的单个字符 [ ] :表示在字符数组[]中罗列出来的字符任意取单个 | ...
- LeetCode算法题-Binary Tree Level Order Traversal II(Java实现)
这是悦乐书的第165次更新,第167篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第24题(顺位题号是107).给定二叉树,返回其节点值的自下而上级别顺序遍历(即从左到右 ...
- March 02nd, 2018 Week 9th Friday
Make hay while the sun shines. 勿失良机. Last night the toothache woke me up and it was very difficult f ...
- #005Python实验楼基本操作学习笔记
Ctrl + D 输入一个 EOF 字符来退出解释器,也可以键入 exit() 来退出解释器. 打开 Xfce 终端,键入 vim helloworld.py 来启动 Vim 并编辑 hellowor ...