Hdu5143 brute force Enumeration
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5143
Problem descriptionpth is learning progress sion ssion 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)
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?
InputThe 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 ≤0 = 109) .
OutputFor each test case, print "Yes" (without quotes) if the numbers can be divided properly, otherwise print "No" (without quotes ).
Sample Input
31 2 2 11 0 0 03 0 0 0
Sample Output
YesNoYesHintIn 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 violent enumeration questions: given numbers 1, 2, 3, 4. each number can be used only once to form multiple or one arithmetic difference series (length greater than or equal to 3). How can this problem be solved) it can be found that there is only a constant number of the arithmetic difference series (123,234,123 4 and length> = 3). If the number of the extraordinary series (123,234,123 4) is greater than or equal to 3, it can be changed to three or four constant columns, for example, (123,123,123) to (111,222,333 ). Therefore, select the number of extraordinary columns from the 0-2 enumeration, and then determine whether to overwrite the remaining values with regular columns (if the number length is exactly 0 or less than or equal to 3 ). */# Include
# Include
# Include
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 ;}