uva 1381 - Balancing the Scale(枚举+位运算)
来源:互联网 发布:物流数据交换技术 编辑:程序博客网 时间:2024/06/03 20:31
题目链接:uva 1381 - Balancing the Scale
题目大意:给出16个数,要求找出满足题目中两个算式的种数,每个数字1次。
解题思路:每次枚举4个数,然后枚举出这四个数排列的全部情况,对于每个情况有一个和(题目中的算式),判断可否用剩余的12个数字组成相同大小的值,可以的话,给8个数字的情况就要+1。最后枚举8个数字和其他8个数字可行解的乘积和,即是答案的2倍。
#include <stdio.h>#include <string.h>#include <vector>#include <algorithm>using namespace std;const int N = 1<<16;const int M = 20;int num[M], v[M], s[N+5];vector<int> g[N+5];bool init () {memset(s, 0, sizeof(s));for (int i = 0; i < N; i++) g[i].clear();scanf("%d", &num[0]);if (num[0] == 0) return false;for (int i = 1; i < 16; i++) scanf("%d", &num[i]);sort(num, num + 16);return true;}bool judge (int k) {int cnt = 0;for (int i = 0; i < 16; i++) if (k & (1<<i)) {v[cnt++] = num[i];}return cnt == 4;}int solve () {for (int i = 0; i < N; i++) if (judge(i)) {do {int cur = v[0] * 4 + v[1] * 3 + v[2] * 2 + v[3];for (int j = 0; j < g[cur].size(); j++) if ((i & g[cur][j]) == 0) {s[i|g[cur][j]]++;}g[cur].push_back(i);} while (next_permutation(v, v + 4));}int ans = 0;for (int i = 0; i < N; i++) {ans += s[i] * s[i ^ (N-1)];}return ans/2;}int main () {int cas = 1;while (init()) {printf("Case %d: %d\n", cas++, solve());}return 0;}
1 0
- uva 1381 - Balancing the Scale(枚举+位运算)
- 1381 - Balancing the Scale (技巧枚举+位运算)
- POJ 3139 / LA 3693 Balancing the Scale (枚举,状态压缩)
- UVALive - 3693 Balancing the Scale 枚举 + 状态压缩
- Balancing the Scale
- uva - Magic Numbers(枚举、位运算)
- UVALive - 3693 Balancing the Scale
- POJ 3139 Balancing the Scale
- POJ 3139 Balancing the Scale 笔记
- UVa 11218 KTV (枚举&位运算)
- POJ1753(枚举+位运算)
- POJ2965(枚举+位运算)
- UVA 11825Hackers' Crackdown dp+位运算状态压缩(子集枚举)
- UVA 687 - Lattice Practices(暴力枚举+位运算)
- UVA 1326 - Jurassic Remains(技巧枚举+位运算)
- hdoj 3006 The Number of set(位运算+枚举)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+位运算)
- The Fortified Forest(poj1873位运算枚举+凸包)
- 九度oj 题目1367:二叉搜索树的后序遍历序列
- uva - 10487 - Closest Sums(二分查找)
- 消息和事件的区别 VC++解惑
- C++ 的THUNK技术
- Android SQLite 打开、操作分析
- uva 1381 - Balancing the Scale(枚举+位运算)
- 已知二叉树的前序/后序遍历和中序遍历,求后序/前序遍历
- POJ 2503 二分 还有一些基本字符串的处理
- appfuse 解决乱码
- Java程序员面试技巧
- 2013校赛未A Min Number
- Android编程之LocalBroadcastManager源码详解
- VS2010 快捷键 (空格显示 绿点, Tab 显示箭头)
- python菜鸟学习第一课