POJ 1020 Anniversary Cake

Anniversary Cake
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10380 Accepted: 3238

Description

Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square-shaped chocolate cake with known size. She asks each invited person to determine the size of the piece of cake that he/she wants (which should also be square-shaped). She knows that Mr. Kavoosi would not bear any wasting of the cake. She wants to know whether she can make a square cake with that size that serves everybody exactly with the requested size, and without any waste.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case. Each test case consist of a single line containing an integer s, the side of the cake, followed by an integer n (1 ≤ n ≤ 16), the number of cake pieces, followed by n integers (in the range 1..10) specifying the side of each piece.

Output

There should be one output line per test case containing one of the words KHOOOOB! or HUTUTU! depending on whether the cake can be cut into pieces of specified size without any waste or not.

Sample Input

2
4 8 1 1 1 1 1 3 1 1
5 6 3 3 2 1 1 1

Sample Output

KHOOOOB!
HUTUTU!

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
惭愧惭愧,这道题实在搞不清楚搜的策略,还是参考别人的代码写的

 

 

#include<stdio.h>

#include<string.h>

int c[20],n,d[50],s;

bool ok;

void dfs(int depth)

{

    int p,min;

    if(ok) return ;//两个剪枝

    if(depth>=n) { ok=true; return; }

    min=1<<31-1;

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

    if( d[i]<min ) { min=d[i]; p=i; }//这个极其重要,保证了每列覆盖的连续性

    for(int i=10;i>=1;i--)//数据很小,最大是到10

        if(c[i]>0 && min+i-1<=s && p+i-1<=s)//条件

        {

            bool f=true;

            for(int j=p;j<=p+i-1;j++)

            if(d[j]>min)   { f=false;break;}

            if(f)

            {

                for(int j=p;j<=p+i-1;j++) d[j]+=i;

                c[i]--;

                dfs(depth+1);

                c[i]++;

                for(int j=p;j<=p+i-1;j++) d[j]-=i;

            }

        }

}

int main()

{

    int T;

    scanf("%d",&T);

    while(T--)

    {

        memset(c,0,sizeof(c));

        memset(d,0,sizeof(d));

        scanf("%d ",&s);

        scanf("%d ",&n);

        int max=0,sum=0;

        bool flag=true;

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

        {

           int x;

           scanf("%d",&x);

           if(x+max>s) flag=false;

           if(x>max)  max=x;

           sum+=x*x;

           c[x]++;

        }

        if(sum!=s*s)  flag=false;

        ok=false;

        if(flag)  dfs(0);

        if(ok)   puts("KHOOOOB!");

        else puts("HUTUTU!");

    }

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值