HDU 5077 NAND

题意:

输入真值表的最后一列  问  最少使用多少个“与非”式使真值表成立

思路:

纱布题…  现场赛出了就是看前面谁做得快了 - -b  这题就是暴力打表  然后提交那个表…

我的打表巨暴力  能把ans=10的答案打出  全部表打完估计要跑30min才行  其中104这个点打不出来  于是我们可以用人工枚举的办法 - -b  就是从11开始枚举  不断提交…  WA了就改一下…

其实这题的暴搜可以写的很美  但是我很懒…… QAQ

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<bitset>
using namespace std;
#define M 260

int T, top;
int ans[M] = { 1, 5, 6, 3, 6, 3, 7, 4, 7, 8, 4, 5, 4, 5, 4, 1, 6, 3, 7, 4, 7, 4,
		9, 7, 8, 8, 7, 5, 7, 5, 7, 4, 7, 8, 4, 5, 8, 8, 7, 5, 8, 9, 5, 6, 8, 8,
		5, 5, 4, 5, 4, 1, 7, 5, 7, 4, 8, 8, 5, 5, 5, 7, 6, 4, 7, 8, 8, 8, 4, 5,
		7, 5, 8, 9, 8, 8, 5, 6, 5, 5, 4, 5, 7, 5, 4, 1, 7, 4, 8, 8, 5, 7, 5, 5,
		6, 4, 8, 9, 8, 8, 8, 8, 5, 7, 11, 9, 8, 9, 8, 9, 8, 8, 5, 6, 5, 5, 5, 5,
		6, 4, 8, 9, 8, 8, 8, 8, 8, 7, 8, 9, 9, 9, 9, 9, 10, 9, 5, 7, 6, 6, 6, 6,
		7, 6, 9, 9, 10, 9, 10, 9, 10, 10, 7, 6, 7, 7, 7, 7, 9, 7, 5, 7, 6, 6, 7,
		6, 7, 7, 5, 6, 2, 3, 6, 6, 4, 3, 6, 6, 7, 6, 7, 7, 9, 7, 6, 6, 4, 3, 7,
		7, 7, 6, 5, 7, 7, 6, 6, 6, 7, 7, 5, 6, 6, 6, 2, 3, 4, 3, 6, 6, 7, 7, 7,
		6, 9, 7, 6, 6, 7, 7, 4, 3, 7, 6, 5, 6, 6, 6, 6, 6, 7, 7, 8, 9, 5, 6, 5,
		6, 2, 5, 2, 3, 4, 3, 4, 3, 7, 6, 5, 6, 2, 5, 2, 5, 4, 1 };
int now[M], vis[M];

bool dfs(int f, int x, int floor) {
	if (vis[x])
		return true;
	if (f == floor)
		return false;
	for (int i = 0; i < top; i++) {
		for (int j = i + 1; j < top; j++) {
			int go = ((~(now[i] & now[j])) & 255);
			if (!vis[go]) {
				vis[go] = 1;
				now[top++] = go;
				if (dfs(f + 1, x, floor))
					return true;
				vis[go] = 0;
				top--;
			}
		}
	}
	return false;
}

void init() {
	top = 5;
	memset(vis, 0, sizeof(vis));
	vis[0] = vis[255] = vis[15] = vis[51] = vis[85] = 1;
}

void pre() {
	int i, j, k;
	now[0] = 0;
	now[1] = 255;
	now[2] = 15;
	now[3] = 51;
	now[4] = 85;
	for (i = 105; i < 256; i++) {
		for (k = 0, j = 0; !k; j++) {
			init();
			if (dfs(0, i, j))
				k = j + 1;
		}
		ans[i] = k;
		cout << i << " " << ans[i] << endl;
	}
}

void maketable() {
	int i, j, k = 256;
	while (k--) {
		scanf("%d %d", &i, &j);
		ans[i] = j;
	}
	printf("{");
	for (i = 0; i < 256; i++)
		printf("%d,", ans[i]);
	printf("}");
}

int main() {
	int k, i;
	char f[10];
	//pre();
	//maketable();
	scanf("%d", &T);
	while (T--) {
		scanf("%s", f);
		k = 0;
		for (i = 0; i < 8; i++) {
			k <<= 1;
			if (f[i] == '1')
				k |= 1;
		}
		printf("%d\n", ans[k]);
	}
	return 0;
}
时间: 2024-10-28 22:00:31

HDU 5077 NAND的相关文章

hdu 5077 NAND(暴力打表)

题目链接:hdu 5077 NAND 题目大意:Xiaoqiang要写一个编码程序,然后根据x1,x2,x3的值构造出8个字符,现在给定要求生成的8个字符,问 说Xiaoqiang最少要写多少行代码.代码内容只能为NAND操作和return操作,操作的变量可以是常数. 解题思路:输入总共就256中情况,所以暴力剪枝打表,打表的代码手贱给删了...所以就将一下思路,开一个s数组 表示变量,然后对应每一层每次两个变量进行NAND操作. 大致三个剪枝,dfs时候,变量出现相同就跳过:8个字符可以直接根

hdu 5077 NAND(打表)2014 Asia regional 鞍山站 H题

题目链接:点击打开链接 题意:就是一个按位运算的一个函数,问最少经过多少步运算可以得到给定数: 思路:不是我投机取巧想打表,是特么这题只能打表...打表思想用可以得到的数的集合表示状态bfs:最后有一个需要11步的需要打将近1h,除去这一个十分钟就够了. cpp: #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <map> using

2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1008(hdu 5929)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5929 Problem Description Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack: ? PUSH x: put x on the top of the stack, x must be 0 or 1.? POP

hdu 5078 Osu! (2014 acm 亚洲区域赛鞍山 I)

题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5078 Osu! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 180    Accepted Submission(s): 114 Special Judge Problem Description Osu! is a very p

hdu 5071 Chat-----2014acm亚洲区域赛鞍山 B题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 Chat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 476    Accepted Submission(s): 109 Problem Description As everyone knows, DRD has no g

hdu 5073 Galaxy(2014acm亚洲赛区鞍山 D)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 768    Accepted Submission(s): 179 Special Judge Problem Description Good news for u

hdu 5073 Galaxy(2014acm亚洲赛区鞍山 C)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 768    Accepted Submission(s): 179 Special Judge Problem Description Good news for u

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include