POJ 3139 Balancing the Scale
来源:互联网 发布:javascript 控制height 编辑:程序博客网 时间:2024/06/05 18:49
Description
You are given a strange scale (see the figure below), and you are wondering how to balance this scale. After several attempts, you have discovered the way to balance it — you need to put different numbers on different squares while satisfying the following two equations:
How many ways can you balance this strange scale with the given numbers?
Input
There are multiple test cases in the input file. Each test case consists of 16 distinct numbers in the range [1, 1024] on one separate line. You are allowed to use each number only once.
A line with one single integer 0 indicates the end of input and should not be processed by your program.
Output
For each test case, if it is possible to balance the scale in question, output one number, the number of different ways to balance this scale, in the format as indicated in the sample output. Rotations and reversals of the same arrangement should be counted only once.
Sample Input
87 33 98 83 67 97 44 72 91 78 46 49 64 59 85 880
Sample Output
Case 1: 15227223
算有多组答案,先根据数值的范围可以知道,最大值不会超过10240。
先用四个数组成全部的情况,总共有4!*C(16,4)种,用链表分别对应到相应的值上。
然后对于同一个值,计算出对应的8个数的组合,用状压统计到数组里
然后两个互补的8个数组合相乘就是答案。
#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)#define loop(i,j,k) for (int i = j;i != -1; i = k[i])#define lson x << 1, l, mid#define rson x << 1 | 1, mid + 1, r#define fi first#define se second#define mp(i,j) make_pair(i,j)#define pii pair<int,int>using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-4;const int INF = 0x7FFFFFFF;const int mod = 9973;const int N = 1 << 16;int a[16], b[4], c[4], cas = 0;int ft[N], nt[N], u[N], sz;int f[N];void get(int x, int y, int z){if (x == 4){rep(i, 0, 3) c[i] = b[i];do{int C = 0;rep(i, 0, 3) C += a[c[i]] * (4 - i);u[sz] = z; nt[sz] = ft[C]; ft[C] = sz++;} while (next_permutation(c, c + 4));}else rep(i, y, 15){b[x] = i;get(x + 1, i + 1, z | (1 << i));}}int main(){while (scanf("%d", &a[0]) != EOF, a[0]){rep(i, 1, 15) scanf("%d", &a[i]);rep(i, 0, N) ft[i] = -1;sz = 0; get(0, 0, 0);memset(f, 0, sizeof(f));rep(i, 0, N){loop(j, ft[i], nt){loop(k, nt[j], nt){if (u[j] & u[k]) continue;f[u[j] | u[k]]++;}}}LL ans = 0;rep(i, 0, (1 << 16) - 1){ans += 1LL * f[i] * f[((1 << 16) - 1) ^ i];}printf("Case %d: %lld\n", ++cas, ans / 2); }return 0;}
- POJ 3139 Balancing the Scale
- POJ 3139 Balancing the Scale 笔记
- Balancing the Scale
- POJ 3139 / LA 3693 Balancing the Scale (枚举,状态压缩)
- UVALive - 3693 Balancing the Scale
- 1381 - Balancing the Scale (技巧枚举+位运算)
- uva 1381 - Balancing the Scale(枚举+位运算)
- UVALive - 3693 Balancing the Scale 枚举 + 状态压缩
- POJ 1655 Balancing Art
- poj 1655 Balancing Act
- poj 1655 Balancing Act
- poj 1655 Balancing Act
- POJ 1655 Balancing Act
- poj 1655 Balancing Act
- poj 1655 Balancing Act
- POJ 1655 Balancing Act
- poj 1655 Balancing Act
- POJ 1655:Balancing Act
- picasso-强大的Android图片下载缓存库
- python3.x操作redis
- 与技术无关-电视剧《小别离》经典台词
- 代码笔记 | 哥德巴赫猜想的计算机验证
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- POJ 3139 Balancing the Scale
- Activity中心打开动画效果工具类
- 关于Mybatis中动态查询表和表中的列
- python的http代理,对请求的头文件中的json数据进行比对-------(2)
- 代码笔记 | BP神经网络的C语言代码
- log4j
- 红黑树
- iOS开发集成友盟,腾讯QQ登录授权失败
- Android Studio ADB响应失败解决方法