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:

x1 * 4+x2 * 3+x3 * 2+x4=x5+x6 * 2+x7 * 3+x8 * 4y1 * 4+y2 * 3+y3 * 2+y4=y5+y6 * 2+y7 * 3+y8 * 4

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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 蹬完单车膝盖疼怎么办 夏天骑摩托太热怎么办 半框眼镜线断了怎么办 镜片从镜框脱了怎么办 干活累的肩膀痛怎么办 干活累的肩膀疼怎么办 骑山地车手腕和脖子疼怎么办? 布艺沙发坐软了怎么办 篮球护臂太大了怎么办 豪爵摩托车没电怎么办 西装裤裤脚太宽怎么办 全棉衣服有异味怎么办 羊剪绒毛领褪色怎么办 篮球鞋买大2码怎么办 新鞋穿着脚累怎么办 买了国产乔丹怎么办 跑完1000米吐了怎么办 翻毛鞋脚染色了怎么办 翻毛皮被染色了怎么办 防鹿皮绒鞋染色怎么办 亚瑟士跑鞋挤脚怎么办 亚瑟士跑鞋很紧怎么办 飞线鞋面破了怎么办 新袜子穿了很滑怎么办 鞋底硬脚底板疼怎么办 新鞋前面太硬怎么办 新鞋子鞋底太硬怎么办 不到一米八的身高想扣篮怎么办 鞋胶粘在鞋面上怎么办 鞋子上沾了胶怎么办 休闲鞋号码大了半码怎么办 高跟鞋大了一码怎么办 浅口单鞋买大了怎么办 新鞋子磨大脚趾怎么办 鞋前面磨大脚趾怎么办 白鞋子蹭黑了怎么办 夏天穿皮鞋捂脚怎么办 耐克赤足掉漆怎么办 鞋子买回来小了怎么办 布鞋大了一码怎么办 鞋子买小了一码怎么办