The Minimum Number of Variables

我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况。

然后用sosdp处理出,状态为state的a, 能组成的数字, 然后转移就好啦。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n;
int a[N]; int cnt[ << ];
int sos[ << ];
bool dp[][ << ]; int main() {
scanf("%d", &n);
for(int i = ; i < ( << n); i++)
cnt[i] = cnt[i - (i & - i)] + ;
for(int i = ; i < n; i++) scanf("%d", &a[i]);
for(int i = ; i < n; i++) {
for(int j = i; j < n; j++) {
int ret = a[i] + a[j];
for(int k = ; k < n; k++) {
if(ret == a[k])
sos[( << i) | ( << j)] |= << k;
}
}
}
for(int i = ; i < n; i++)
for(int mask = ; mask < ( << n); mask++)
if(mask >> i & ) sos[mask] |= sos[mask ^ ( << i)]; dp[][] = ;
for(int i = ; i < n - ; i++) {
for(int mask = ; mask < ( << n); mask++) {
if(!dp[i][mask]) continue;
if(sos[mask] >> (i + ) & ) {
dp[i + ][mask | ( << (i + ))] = true;
for(int j = ; j <= i; j++) {
if(mask >> j & ) {
dp[i + ][(mask ^ ( << j)) | ( << (i + ))] = true;
}
}
}
}
}
int ans = inf;
for(int i = ; i < ( << n); i++)
if(dp[n - ][i]) chkmin(ans, cnt[i]);
if(ans == inf) ans = -;
printf("%d\n", ans);
return ;
} /*
*/

Codeforces 279D The Minimum Number of Variables 状压dp的更多相关文章

  1. CodeForces 279D The Minimum Number of Variables 题解

    题目大意: 有一组n个不相同的数字组成数串:a1,a2,a3-an. 1.一个数组b. 2.第一个操作我们将b0的值赋为a1.之后我们有n-1个操作,第k次操作我们将by=bi+bj(y,i,j可能相 ...

  2. 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 ...

  3. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  4. Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...

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

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

  6. codeforces#580 D. Kefa and Dishes(状压dp)

    题意:有n个菜,每个菜有个兴奋值,并且如果吃饭第i个菜立即吃第j个菜,那么兴奋值加ma[i][j],求吃m个菜的最大兴奋值,(n<=18) 分析:定义dp[status][last],statu ...

  7. Codeforces Round #585 (Div. 2) E. Marbles(状压dp)

    题意:给你一个长度为n的序列 问你需要多少次两两交换 可以让相同的数字在一个区间段 思路:我们可以预处理一个数组cnt[i][j]表示把i放到j前面需要交换多少次 然后二进制枚举后 每次选择一个为1的 ...

  8. 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 ...

  9. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

随机推荐

  1. Java基础 -- 深入理解迭代器

    在Java基础 -- 持有对象(容器)已经详细介绍到,集合(Collection)的种类有很多种,比如ArrayList.LinkedList.HashSet.... 由于集合的内部结构不同,很多时候 ...

  2. ubuntu安装nginx pagespeed

    一.自动安装 使用最新稳定版本的ngx_pagespeed自动安装依赖项并构建最新的主线版nginx,请运行: $ sudo bash <(curl -f -L -sS https://ngxp ...

  3. crm 一级菜单排序,二级菜单选中并且展开,非菜单权限的归属,权限粒度控制到按钮级别

    排序 /rbac/templatetags/rbac.py from django import template from django.conf import settings import re ...

  4. 2018-2019-2 《Java程序设计》第9周学习总结

    20175319 2018-2019-2 <Java程序设计>第9周学习总结 教材学习内容总结 本周学习任务: 下载安装MySQL数据库管理系统. 教材介绍在官网下载安装Mysql服务,启 ...

  5. Python——安居客租房信息爬取(以南昌为例)

    前言: 提前安装好所需要的库. 本代码的输入仅需要某个城市的租房地址首页即可,其他自会生成. 使用前请创建所需的目录,或者为代码添加os.makedir() 支持断点重爬,重行运行即可. header ...

  6. shell 批量远程主机执行命令

    [yunwei@Y24-209 ~]$cat ls.sh #!/bin/bash ip55=`cat ip1` for i in $ip55;do ping -c 1 $i if [ $? -eq 0 ...

  7. mysql并发控制之MVCC

    1.MVCC(Multiversion concurrency control) :多版本并发控制,当我们并发访问数据库(读或写)时,对事物内正在处理的数据做多版本控制,用以防止写操作的阻塞影响读操作 ...

  8. python面试题一个字符串是否由重复的子字符串组成

    一,给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 输入: "abab" 输出: True 解释: 可由 ...

  9. VS发布网站时,报错提示:“未能将文件xxx复制到xxx,未能找到文件xx”三种解决方案!

    发布网站时候大家可能会遇到这样的情况,就是报错提示说:“未能将文件xxx复制到xxx,未能找到文件xx”,这个问题一般来说有三种解决方案,个人倾向第三种,如图: 解决方案如下: 方案一.把系统提示缺失 ...

  10. web前端效率提升之浏览器与本地文件的映射-遁地龙卷风

    1.chrome浏览器,机制是拦截url, 1.在浏览器Element中调节的css样式可以直接同步到本地文件,反之亦然,浏览器会重新加载css,省去刷新 2.在source面板下对js的编辑可以同步 ...