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. virtualbox+ubuntu

    https://jingyan.baidu.com/article/7f766daff541cd4101e1d0cd.html ubuntu 安装 这台计算机似乎没有安装操作系统 待解决 注意ubun ...

  2. MFC:定时器

    1. 定时器设置 API:CWnd::SetTimer()     函数原型:UINT SetTimer(UINT nIDEvent, UINT nElapse, void (CALLBACK EXP ...

  3. 史上最全PMP备考考点全攻略(上篇-五大过程组,附赠资料)

    一.这可能是一篇史上最全的PMP备考考点全梳理文章 写在前面,这可能是史上最全的PMBOK考点全书考点梳理,由PMP备考自律营呕心沥血整理,内容较长,分为上下篇,绝对值得所有正在备考PMP的学员收藏! ...

  4. python类的两种创建方式

    参考: https://blog.csdn.net/likunkun__/article/details/81949479

  5. DML、DDL、DCL的分别是什么

    DML.DDL.DCL的分别是什么 一直以来,分不清这三者的简称代表什么,甚至在面试中遇到可能会张冠李戴.今天特意记录一下. 一.DML(data manipulation language) 数据操 ...

  6. Apache Hadoop 2.9.2 的Federation架构设计

    Apache Hadoop 2.9.2 的Federation架构设计 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文件,说明你对NameNode的工作原理想必已经了如 ...

  7. unicorn与nginx通讯--[ruby unix socket]

    [龍昌博客] http://www.xefan.com/archives/84146.html unicorn是如何与nginx通讯的——介绍ruby中的unix socket Ruby 应用服务典型 ...

  8. docker常用命令总结

    1.docker ps  查看当前正在运行的容器 2.docker ps -a 查看所有容器的状态 3.docker start/stop id/name     启动/停止某个容器 4.docker ...

  9. java构造方法的重载

    package test; public class Person { String name; int age; public Person() { System.out.println(" ...

  10. matlab 图像设置

    Matlab画图设置线宽和字号 既然这么多人来这里看过,我就多做点注释,方便大家参考. 下边这段代码不需要特别设置,只需要在plot语句之后插入即可. %plot your figure before ...