HDOJ 5143 NPY and arithmetic progression DFS

DFS.....

多余3个的数可以自己成等比数列

和其他数组合成等比数列的有1,2,3,  2,3,4  1,2,3,4 三种情况

NPY and arithmetic progression

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 189    Accepted Submission(s): 61

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}.

Source

BestCoder Round #22

/* ***********************************************
Author        :CKboss
Created Time  :2014年12月13日 星期六 23时09分55秒
File Name     :B_2.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>

using namespace std;

bool flag;

bool ck(int a,int b,int c,int d)
{
	/// check
	if(a==0&&b==0&&c==0&&d==0) return true;
	if( ((a!=0&&a>=3)||(a==0)) && ((b!=0&&b>=3)||(b==0)) && ((c!=0&&c>=3)||(c==0)) && ((d!=0&&d>=3)||(d==0)) ) return true;

	if(a-1>=0&&b-1>=0&&c-1>=0&&d-1>=0)
		if(ck(a-1,b-1,c-1,d-1)) return true;

	if(a-1>=0&&b-1>=0&&c-1>=0)
		if(ck(a-1,b-1,c-1,d)) return true;

	if(b-1>=0&&c-1>=0&&d-1>=0)
		if(ck(a,b-1,c-1,d-1)) return true;

	return false;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		int a,b,c,d;
		scanf("%d%d%d%d",&a,&b,&c,&d);
		if(ck(a,b,c,d)==true) puts("Yes");
		else puts("No");
	}
    return 0;
}
时间: 2024-12-14 08:43:39

HDOJ 5143 NPY and arithmetic progression DFS的相关文章

BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数只使用一次的前提下,分成若干部分,每部分数的长度 >= 3且满足是等差数列.可以的话输出 Yes ,否则 No . 比赛的时候过了pretest,那个开心啊--然后跟 XX 兽讨论了之后,才发现自己理解错了题目= = 要用到深搜,问组合嘛--组合就是有可能是(1, 2, 3, 4).(1, 2, 3

hdoj 2553 N皇后问题 【DFS】

题意:... 典型的深搜,就是处理对角线的时候有些意外. 代码(注释掉的就是深搜,因为我不打表的话 TL): #include<stdio.h> int c[11], n, ans; int res[10] = {1, 0, 0, 2, 10, 4, 40, 92, 352, 724}; /*void dfs(int cur) { if(cur == n) { ++ans; return ; } int i, j, flag; for(i = 0; i < n; i ++){ flag

hdoj 1016 Prime Ring Problem 【DFS】

策略如题 链接 http://acm.hdu.edu.cn/showproblem.php?pid=1016 代码: #include<stdio.h> #include<string.h> int prime[25] = {1, 1}, n, vis[25]; //vis作用:标记是否用过 int a[25]; void f() //找出来前20的素数 判定为0 { for(int i = 2; i <= 24; i ++){ if(prime[i] == 0) for(i

CF 1114 E. Arithmetic Progression

E. Arithmetic Progression 链接 题意: 交互题. 有一个等差序列,现已打乱顺序,最多询问60次来确定首项和公差.每次可以询问是否有严格大于x的数,和查看一个位置的数. 分析: 首先可以二分找到序列的最大值,然后考虑如何求公差. 随机选30个数,然后对任意两个求一遍gcd即可. 正确性证明 代码: #include<cstdio> #include<algorithm> #include<cstring> #include<iostream

hdoj 1428 漫步校园 【BFS+DFS】

题目:hdoj 1428 漫步校园 分析:题意还是有必要说的,他考虑从A区域到B区域仅当存在一条从B到机房的路线比任何一条从A到机房的路线更近,注意这句话,可见先让你求每个点到机房(n,n)点的最短路.当然这里用BFS比较好,注意要用优先队列..接着这句话告诉你,每次选择走的时候可以有多种选择,只要满足上面每一步都比当前这一步更近,即dis[child] < dis[father],然后求到终点有多少条.(注意不是求最短路有多少种走法),这一步可以用记忆话深搜,用dp[i][j]表示从当前点到终

HDOJ 题目1881 毕业bg(DFS)

毕业bg Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 6   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description 每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上

HDOJ 5144 NPY and shot 简单物理

三分角度.... NPY and shot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 236    Accepted Submission(s): 87 Problem Description NPY is going to have a PE test.One of the test subjects is throwing t

HDOJ 5142 NPY and FFT 水

比赛的时候题目意思完全不对.....居然还有人1分钟就AC了...... NPY and FFT Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 132    Accepted Submission(s): 86 Problem Description A boy named NPY is learning FFT algorithm

hdoj - 1258 Sum It Up &amp;&amp; hdoj - 1016 Prime Ring Problem (简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. 1 #include <cstdio> 2 #include <cstring> 3 4 int n,t; 5 int b[15],c[15]; 6 bool flag; 7 void dfs(int k,int sum,int l) 8 { 9 if(sum==t) 10 { 11 for(int i=0;i<l-1;i++) 12 p