hdu5143 暴力枚举

http://acm.hdu.edu.cn/showproblem.php?pid=5143

Problem Description

NPY is learning arithmetic progression in his math class. In mathematics, an arithmetic progression (AP) is a sequence of numbers such that the difference between the consecutive terms is constant.(from wikipedia)

He thinks it‘s easy to understand,and he found a challenging problem from his talented math teacher:

You‘re given four integers, a1,a2,a3,a4,
which are the numbers of 1,2,3,4 you have.Can you divide these numbers into some Arithmetic Progressions,whose lengths are equal to or greater than 3?(i.e.The number of AP can be one)

Attention: You must use every number exactly once.

Can you solve this problem?

Input

The first line contains a integer T — the number of test cases (1≤T≤100000).

The next T lines,each contains 4 integers a1,a2,a3,a4(0≤a1,a2,a3,a4≤109).

Output

For each test case,print "Yes"(without quotes) if the numbers can be divided properly,otherwise print "No"(without quotes).

Sample Input

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

Sample Output

Yes
No
Yes

Hint

In the first case,the numbers can be divided into {1,2,3} and {2,3,4}.
In the second case,the numbers can‘t be divided properly.
In the third case,the numbers can be divided into {1,1,1}.
/*
hdu 5143 暴力枚举
题目大意:
     给定数字1,2,3,4.的个数每个数字能且仅能使用一次,组成多个或一个等差数列(长度大于等于3)问能否成功
解题思路:(杭电官方题解)
     可以发现等差数列只有(123,234,1234和长度>=3的常数列),如果选择非常数列(123,234,1234)数量大于等于3,
可以变为三个或4个常数列,例如(123,123,123)变为(111,222,333)。所以从0-2枚举选择非常数列的数量,再判断能
否用常数列覆盖剩下的(如果数字长度正好为0或≤3就可以)。
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
bool ok(int a[5])
{
    if((a[0]>=3||a[0]==0)&&(a[1]>=3||a[1]==0)&&(a[2]>=3||a[2]==0)&&(a[3]>=3||a[3]==0))
        return true;
    return false;
}
int a[10],b[10];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<4;i++)
        {
            scanf("%d",&a[i]);
        }
        int flag=0;
        if(ok(a))
        {
            flag=1;
        }
        else
        {
            for(int i=0;i<=2;i++)
            {
                for(int j=0;j<=2;j++)
                {
                    for(int k=0;k<=2;k++)
                    {
                        b[0]=a[0]-i-j;
                        b[1]=a[1]-i-j-k;
                        b[2]=a[2]-i-j-k;
                        b[3]=a[3]-i-k;
                        if(ok(b))
                            flag=true;
                    }
                }
            }
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
时间: 2024-10-13 00:06:49

hdu5143 暴力枚举的相关文章

hdu5616 暴力枚举

2017-08-25 20:08:54 writer:pprp 题目简述: ? HDU 5616? n个砝码,可以放在天平左右两侧或不放? m次询问,每次询问是否可以测出给定重量? 1 ≤ n ≤ 20? 1 ≤ m ≤ 100 这道题采用枚举的思路的话实现起来还是有点困难的, 要实现的功能是对每个砝码进行处理,加到左边, 加到右边,或者是不加 看了大神的代码,感觉很巧妙, 设置了两个标记数组 vis1[2005], vis2[2005] 一个vis1用来记录当前已经可以实现的重量 另一个vis

hdu4282A very hard mathematic problem 暴力枚举

//给出k //找x,y,z使得x^z+y^z+x*y*z = k //x,y,z都为正整数x<y,z>1问有多少种方法 //当z = 2时,可以看到左边是一个完全平方 //而当z>=3时,可以暴力枚举x,y //由于k<2^31所以x<2^(31/3)枚举复杂度可以过 #include<cstdio> #include<cstring> #include<iostream> #include<cmath> using name

hdu 5247 找连续数【暴力枚举】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5247 分析:这道题是2015百度之星初赛1的2题,当时没看这道题 是队友看的,比完以后也做了一下,思路大体都是一样的,就是 暴力枚举,因为k<=1000,那么我们可以每一点x为起点跑[x,x+999] 这段区间,把每得到一段连续的子区间[x,?],则num[len]++(len=size([x,?])); 这样就可以了,最后num数组里就是对应的答案 献上代码: #include<stdio.h&

HDU 4770 Lights Against Dudely 暴力枚举+dfs

又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ czy Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360    Accepted Subm

ZOJ3818-Pretty Poem(暴力枚举)

题目链接 题意:求所给字符串是否符合ABABA或者ABABCAB的形式,如果可以的话输出Yes,不可以的话为No. 思路:暴力枚举A和B的长度,再用从长度减去3倍的AB长度,即为C的长度,看组合而成的字符串是否与给定的相等.在这里string中的substr函数是个好东西. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <a

uva10892(暴力枚举)

把n的所有因子求出来,总数不会太多,所以直接O(n2)的暴力枚举所有对行不行. 有几个细节要注意,详见代码. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set> #include<vector> #include<algorit

暴力枚举 + 24点 --- hnu : Cracking the Safe

Cracking the Safe Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 46, Accepted users: 12 Problem 12886 : No special judgement Problem description Secret agent Roger is trying to crack a safe containing evil Syr

HNU 12886 Cracking the Safe(暴力枚举)

题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数,要你判断用 + .- . * ./.四种运算能不能得到一个结果为24的式子,可以用括号. 解释一下测试的第四组样例:应该是6 / (1 - 3 / 4) 暴力枚举三种符号分别是什么,然后枚举这三种符号运算的顺序,然后枚举这四个数字的24种排列方式,时间是4^3 * 6 * 24 然后注意要用double型,

HDU 4930 Fighting the Landlords(暴力枚举+模拟)

HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型,如果先手能一步走完,或者一步让后手无法管上,就赢 思路:先枚举出两个人所有可能的牌型的最大值,然后再去判断即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Player { int rank[15]; } p1, p2; int t, h