Zoj 3706 break standard weight

Source: Internet
Author: User

Break standard weight

Time limit:2 seconds
Memory limit:65536 KB

TheBalanceWas the first mass measuring instrument specified Ted. in its traditional form, it consists of a specified Ted horizontal lever of equal length arms, called the beam, with a weighing pan, also called scale, suincluded from each arm (which
Is the origin of the originally plural term "scales" for a weighing instrument ). the unknown mass is placed in one pan, and standard masses are added to this or the other pan until the beam is as close to equilibrium as possible. the standard weights used
With balances are usually labeled in mass units, which are positive integers.

with some standard weights, we can measure several special masses object exactly, whose weight are also positive integers in mass units. for example, with two standard weights
1 and 5 , we can measure the object with mass
1
, 4 , 5 or 6 exactly.

In the beginning of this problem, there are 2 standard weights, which masses are
XAndY. You have to choose a standard weight to break it into 2 parts, whose weights are also positive integers in mass units. we assume that there is no mass lost. for example, the origin standard weights are
4And9, If you break the second one
4
And5, You cocould Measure7Special masses, which are 1, 3, 4, 5, 8, 9, 13. While if you break the first one
1And3, You cocould Measure13Special masses, which are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13! Your task is to find out the maximum number of possible special masses.

Input

There are multiple test cases. The first line of input is an integerT<500 indicating the number of test cases. Each test case contains 2 integers
XAndY. 2 ≤X,Y≤ 100

Output

For each test case, output the maximum number of possible special masses.

Sample Input
 
24 910 10
Sample output
 
139
Author: Yu, Zhi

Contest:The 10th Zhejiang Provincial Collegiate Programming Contest

Question. Give you two weights. You can split one weight into two. How much weight can you combine with the split weight?


# Include <stdio. h> int C; // number of record combinations int s [20]; int ABS (int x) // returns the absolute value {If (x <0) x =-X; return X;} void add (int * s, int X) // Save the combined weight to the S array. {Int I; If (x = 0) // 0 does not meet the condition return; for (I = 0; I <C; I ++) if (s [I] = x) // if the combined weight already exists, return directly; s [I] = X; // no solution set C ++;} int solve (int A, int B) // split B to find the maximum number of combinations {int CC, BB, I, MA =-1; for (I = 1; I <= B/2; I ++) // The Splitting Method of enumeration B {c = 0; CC = B-I; BB = I; // there may only be a combination of add (s, A); add (S, BB); add (S, CC); add (S, A + BB); add (s, A + CC); add (S, BB + CC); add (s, A + BB + CC); add (S, ABS (a-bb); add (S, ABS (a-CC); add (S, ABS (bb-CC); add (S, ABS (a + BB-CC )); Add (S, ABS (a + CC-bb); add (S, ABS (BB + CC-A); If (C> Ma) // Ma records a maximum of MA = C;} return Ma;} int main () {int t, a, B, M1, M2; scanf ("% d ", & T); While (t --) {scanf ("% d", & A, & B); M1 = solve (A, B ); m2 = solve (B, A); printf ("% d \ n", M1> m2? M1: m2);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.