poj 1020

Anniversary Cake
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14764 Accepted: 4768

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
搜索,len记录每个人的边长。a记录边长为i的有多少个。DFS已用的块数,当块数等于n时结束。每次都从最小的块开始累加。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1

#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 10100 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
const int MOD = (int)1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[20];
int s , n;
int len[50];
bool dfs(int cur)
{
    if(cur == n)return true;
    int minlen = 1000 , minx;
    FOR(i ,0 , s)
    {
        if(len[i] < minlen)
        {
            minlen = len[i];
            minx = i;
        }
    }
    FORR(i , 1 ,10)
    {
        if(a[i] > 0 && len[minx] + i <= s && minx + i <= s)
        {
            int ok = 1;
            FOR(j , minx, minx + i)
            {
                if(len[j] != len[minx])
                {
                    ok = 0;
                    break;
                }
            }
            if(ok)
            {
                a[i]--;
                FOR(j , minx , minx + i)
                {
                    len[j] += i;
                }
                if(dfs(cur + 1))
                {
                    return true;
                }
                FOR(j , minx , minx + i)
                {
                    len[j] -= i;
                }
                a[i] ++;
            }
        }
    }
    return false;
}
int main()
{
//    freopen("out.text" , "w" , stdout);
    int  t;
    scanf("%d" , &t);
    while(t--)
    {
        clr(a , 0) , clr(len , 0);
        int area = 0;
        scanf("%d%d" , &s , &n);
        FOR(i , 0 , n)
        {
            int num;
            scanf("%d" , &num);
            a[num] ++;
            area += num * num;
        }
        if(area == s * s && dfs(0))puts("KHOOOOB!");
        else puts("HUTUTU!");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值