Equation

HDU - 5937

Little Ruins is a studious boy, recently he learned addition operation! He was rewarded some number bricks of 11 to 99 and infinity bricks of addition mark '+' and equal mark '='

Now little Ruins is puzzled by those bricks because he wants to put those bricks into as many different addition equations form x+y=zx+y=z as possible. Each brick can be used at most once and x, y, z are one digit integer. 

As Ruins is a beginer of addition operation, xx, yy and zz will be single digit number. 

Two addition equations are different if any number of xx, yy and zz is different. 

Please help little Ruins to calculate the maximum number of different addition equations.

Input

First line contains an integer TT, which indicates the number of test cases. 

Every test case contains one line with nine integers, the ithith integer indicates the number of bricks of ii. 

Limits 
1≤T≤301≤T≤30 
0≤bricks number of each type≤1000≤bricks number of each type≤100

Output

For every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the result.

Sample Input

3
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
0 3 3 0 3 0 0 0 0

Sample Output

Case #1: 2
Case #2: 6
Case #3: 2

首先,总共只有36种等式,每种等式可以选或不选,这样判断是否合法复杂度为2的36次方,会超时,可以发现其中有4个等式是1+1=2这种,其他32个等式是3+4=7,4+3=7这样的,x和y对称,花费的卡片数一样,可以将它们看成同一种等式,这种等式可以取0个,1个或2个,复杂度变为3的16次方乘上2的4次方,再进行一些剪枝边可

 

#include

#include

#include

#include

#include

using namespace std;

int mov[20][3] = {{1,2,3},{1,3,4},{1,4,5},{1,5,6},{1,6,7},{1,7,8},{1,8,9},{2,3,5},{2,4,6},{2,5,7},{2,6,8},{2,7,9},{3,4,7},{3,5,8},{3,6,9},{4,5,9},{1,1,2},{2,2,4},{3,3,6},{4,4,8}};

int num[20] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1};

int t,a[10],ans,sum[20];

void dfs(int i,int cnt,int tot)

{

ans = max(ans,cnt);

if(i == -1)

return;

if(sum[i] + cnt <= ans || tot / 3 + cnt <= ans)//剪枝,如果将还没判断的所有等式都加上也比ans小,或者将所有

//剩下的卡片都用上也比ans小,这样不可能再得到更大的ans,直接返回

return;

--a[mov[i][0]],--a[mov[i][1]],--a[mov[i][2]];

if(a[mov[i][0]] >= 0 && a[mov[i][1]] >= 0 && a[mov[i][2]] >= 0)

dfs(i-1,cnt+1,tot-3);

if(num[i] == 2)

{

--a[mov[i][0]],--a[mov[i][1]],--a[mov[i][2]];

if(a[mov[i][0]] >= 0 && a[mov[i][1]] >= 0 && a[mov[i][2]] >= 0)

dfs(i-1,cnt+2,tot-6);

++a[mov[i][0]],++a[mov[i][1]],++a[mov[i][2]];

}

++a[mov[i][0]],++a[mov[i][1]],++a[mov[i][2]];

dfs(i-1,cnt,tot);

}

int main()

{

int tot;

sum[0] = 2;

for(int i = 1;i < 20;++i)

sum[i] = sum[i-1] + num[i];

scanf("%d",&t);

for(int step = 1;step <= t;++step)

{

tot = 0;

for(int i = 1;i < 10;++i)

{

scanf("%d",&a[i]);

if(a[i] > 17-i)

a[i] = 17-i;

tot += a[i];

}

ans = 0;

dfs(19,0,tot);

printf("Case #%d: %d\n",step,ans);

}

return 0;////

}

你可能感兴趣的:(搜索)