ZCMU训练赛-B(dp/暴力)
B - Break Standard Weight
The balance was the first mass measuring instrument invented. In its traditional form, it consists of a pivoted horizontal lever of equal length arms, called the beam, with a weighing pan, also called scale, suspended 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 6exactly.
In the beginning of this problem, there are 2 standard weights, which masses are x and y. 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 4 and 9, if you break the second one into 4and 5, you could measure 7 special masses, which are 1, 3, 4, 5, 8, 9, 13. While if you break the first one into 1 and 3, you could measure 13special 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 integer T < 500 indicating the number of test cases. Each test case contains 2 integersx and y. 2 ≤ x, y ≤ 100
Output
For each test case, output the maximum number of possible special masses.
Sample Input
2 4 9 10 10
Sample Output
13 9
1 #include<iostream> 2 #include<string.h> 3 #include<stdio.h> 4 #include<ctype.h> 5 #include<algorithm> 6 #include<stack> 7 #include<queue> 8 #include<set> 9 #include<map> 10 #include<math.h> 11 #include<vector> 12 #include<deque> 13 #include<list> 14 #define INF 0x7fffffff 15 #define inf 0x3f3f3f3f 16 using namespace std; 17 int f(int x,int y,int z) 18 { 19 int r[200]; 20 set<int> s; 21 r[0]=x; 22 r[1]=y; 23 r[2]=z; 24 r[3]=x+y ; 25 r[4]=x+z; 26 r[5]=y+z; 27 r[6]=x-y; 28 r[7]=y-z; 29 r[8]=x-z; 30 r[9]=y-x; 31 r[10]=z-x; 32 r[11]=z-y; 33 r[12]=x-y-z; 34 r[13]=x-y+z; 35 r[14]=y-x-z; 36 r[15]=y-x+z; 37 r[16]=y-z+x; 38 r[17]=z-x+y; 39 r[18]=z-x-y; 40 r[19]=x+y+z; 41 for(int i=0;i<20;i++) 42 { 43 if(r[i]>0) 44 s.insert(r[i]); 45 } 46 return s.size(); 47 } 48 49 int main() 50 { 51 int t,a,b; 52 int maxn, sum; 53 scanf("%d",&t); 54 while(t--) 55 { 56 maxn=0; 57 scanf("%d%d",&a,&b); 58 for(int i=1; i<a/2+1; i++) 59 { 60 sum=f(i,a-i,b); 61 maxn=max(maxn,sum); 62 } 63 for(int i=1; i<b/2+1; i++) 64 { 65 sum=f(a,i,b-i); 66 maxn=max(maxn,sum); 67 } 68 printf("%d\n",maxn); 69 } 70 return 0 ; 71 }