http://acm.hdu.edu.cn/showproblem.php?pid=5143
Problem descriptionnpy is learning arithmetic progression in his math class. In mathematics, an arithmetic progression (AP) are a sequence of numbers such that the difference between the consecutive T Erms 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, a 1 , a 2 , a 3 , a 4 , which is the numbers of 1,2,3,4 you have. Can divide these numbers into some arithmetic progressions,whose lengths is 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?
Inputthe first line contains a integer t-the number of test cases ( 1≤T≤100000 ).
The next T Lines,each contains 4 integers a 1 , a 2 , a 3 , a 4 (0≤ a 1 , a 2 , a 3 , a 4 ≤ ten 9 ) .
Outputfor each test Case,print "Yes" (without quotes) if the numbers can divided properly,otherwise print "No" (without Q Uotes).
Sample Input
31 2 2 11 0 0 03 0 0 0
Sample Output
YesnoyesHintin the first case,the numbers can is divided into {} and {2,3,4}. The second case,the numbers can ' t be divided properly. In the third case,the numbers can is divided into {1,1,1}.
/*hdu 5143 The topic of violent enumeration is the main idea: given the number of 1,2,3,4. The number of each digit can and can only be used once, composed of multiple or one arithmetic progression (length greater than or equal to 3) asked whether the success of the problem Solving ideas: (Hangzhou Electric Official solution) can be found arithmetic progression only (123,234,1234 and length >=3 constant column), if you select a very large number of columns (123,234,1234) greater than or equal to 3, you can change to three or 4 constant columns, for example (123,123,123) to (111,222,333). So choose the number of very large numbers from the 0-2 enumeration, and then decide if you can overwrite the remainder with a constant number (if the digit length is exactly 0 or ≤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;}
HDU5143 Violence Enumeration