POJ

来源:互联网 发布:守望先锋鼠标宏编程 编辑:程序博客网 时间:2024/06/14 01:36

Balancing the Scale
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1673 Accepted: 645

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

Source

Shanghai 2006


题意:

给你16个数,具体满足那个公示。


POINT:

!!16个数保证不相同,被坑了好久。

先搜索出所以C(16,4)的组合,然后A(4,4)排序。得到A(16,4)的和的种类数。可知并不大。

对每个组合得到一个sum=x1*4+x2*3+x3*2+x4*1。对每一个sum存下他的状态压缩后的数字。


然后对每一个sum(0-10240)(随便估计,比真实范围大就行)。 对其中链表存下来的状压&,非0则代表数字重复。

for(i=0;i<=(1<<16)-1;i++)来计算答案就好。


#include <iostream>#include <stdio.h>#include <math.h>#include <algorithm>#include <string.h>using namespace std;typedef long long  LL;const int maxn = 50000+55;const int maxnn= 1<<16;int head[maxn],nxt[maxn],big[maxn];int sz;int a[17];int b[5];int num=0;void get(int x,int now,int flag){    if(x==4){        int c[5];        for(int i=0;i<4;i++) c[i]=b[i];        sort(c,c+4);        do        {            num++;            int sum=0;            for(int i=0;i<4;i++) sum+=c[i]*(4-i);            big[sz]=flag;            nxt[sz]=head[sum];            head[sum]=sz++;        }        while(next_permutation(c, c+4));    }    for(int i=now+1;i<=15;i++){        b[x]=a[i];        get(x+1,i,flag|(1<<i));    }}int main(){    int p=0;    while(~scanf("%d",&a[0])&&a[0]){        for(int i=1;i<=15;i++) scanf("%d",&a[i]);        sz=0;        int num[maxnn];        memset(head,-1,sizeof(head));        memset(num,0,sizeof num);        get(0,-1,0);                for(int i=1;i<=10240;i++){            for(int j=head[i];j!=-1;j=nxt[j]){                for(int k=nxt[j];k!=-1;k=nxt[k]){                    if((big[j]&big[k])!=0) continue;                    num[big[j]|big[k]]++;                }            }        }        LL ans=0;        for(int i=1;i<=(1<<16)-1;i++){            ans+=1LL*num[i]*(num[((1<<16)-1)^i]);        }        printf("Case %d: ",++p);        printf("%lld\n",ans/2);    }        return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 性格懒散不工作怎么办 脚受凉脚后跟疼怎么办 月子里脚着凉怎么办 腿着凉了疼怎么办 没有土地的农民怎么办 手机屁股坏了怎么办 包颈手术后睡中会勃起怎么办 武警义务兵训练不合格怎么办 空姐体检心率不齐怎么办 空姐体检差身高怎么办 锻炼过猛肌肉疼怎么办 中考体检有问题怎么办 想参军视力不够怎么办 公司体检查出乙肝怎么办 乙肝携带者福利体检怎么办 小孩没考好我该怎么办 客户考虑我该怎么办 明天中考 我该怎么办 考试考差了怎么办 没考上理想高中怎么办 我是差学生中考怎么办 衬衫后背鼓起来怎么办 高考体检表没了怎么办 高考体检视力不合格怎么办 高考体检转氨酶高怎么办 高考体检肝功能异常怎么办 高考体检有乙肝怎么办 高考体检有纹身怎么办 学校体检血压高怎么办 高考体检不属实怎么办 艺考身上有花臂怎么办 义务兵因病致残怎么办 新兵练成绩优秀怎么办 阿提拉部队得了瘟疫怎么办 公安体能测评胖子怎么办 社区工作者笔试不及格怎么办 警察考核体能差怎么办 胖子跑步跑不动怎么办 电脑跑不动了怎么办 在部队体能不好怎么办 自考考试没过怎么办