Description
The balance was the first mass measuring instrument invented. In itstraditional form, it consists of a pivoted horizontal lever of equal lengtharms, called the beam, with a weighing pan, also called scale, suspended fromeach arm (which is the origin of the originally plural term "scales"for a weighing instrument). The unknown mass is placed in one pan, and standardmasses are added to this or the other pan until the beam is as close toequilibrium as possible. The standard weights used with balances are usuallylabeled in mass units, which are positive integers.
Withsome standard weights, we can measure several special masses object exactly,whose weight are also positive integers in mass units. For example, with twostandard weights 1 and 5, we can measure the object with mass 1, 4, 5 or 6 exactly.
In thebeginning of this problem, there are 2 standard weights, which masses are x and y. Youhave to choose a standard weight to break it into 2 parts, whose weights arealso positive integers in mass units. We assume that there is no mass lost. Forexample, the origin standard weights are 4 and 9, if you break thesecond one into 4 and 5, you could measure 7 specialmasses, which are 1, 3, 4, 5, 8, 9, 13. While if you break the first one into 1 and 3, youcould measure 13 special 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 ofpossible special masses.
Input
Thereare multiple test cases. The first line of input is an integer T < 500 indicatingthe number of test cases. Each test case contains 2 integers x and y. 2 ≤ x, y ≤ 100
Output
For eachtest case, output the maximum number of possible special masses.
Sample Input
2
4 9
10 10
Sample Output
13
9
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
int result[500],cnt=0;
int GUI[27][3]={{-1,-1,-1},{-1,-1,0},{-1,-1,1},{-1,0,-1},{-1,0,0},{-1,0,1},{-1,1,-1},{-1,1,0},{-1,1,1},
{0,-1,-1},{0,-1,0},{0,-1,1},{0,0,-1},{0,0,0},{0,0,1},{0,1,-1},{0,1,0},{0,1,1},
{1,-1,-1},{1,-1,0},{1,-1,1},{1,0,-1},{1,0,0},{1,0,1},{1,1,-1},{1,1,0},{1,1,1},};
void iscun(int n)
{
if(n==0)
return;
n=fabs(n);
intflag=1;
for(inti=0;i<cnt;i++)
if(result[i]==n)
{
flag=0;
break;
}
if(flag==1)
result[cnt++]=n;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int x,y,maxs=2;
cin>>x>>y;
for(int k=1;k<=y/2;k++)
{
int y1=k,y2=y-k;
memset(result,0,sizeof(result));
cnt=0;
for(int i=0;i<27;i++)
{
int sum=x*GUI[i][0]+y1*GUI[i][1]+y2*GUI[i][2];
iscun(sum);
}
if(cnt>maxs)
maxs=cnt;
}
for(int k=1;k<=x/2;k++)
{
int x1=k,x2=x-k;
memset(result,0,sizeof(result));
cnt=0;
for(int i=0;i<27;i++)
{
intsum=x1*GUI[i][0]+x2*GUI[i][1]+y*GUI[i][2];
iscun(sum);
}
if(cnt>maxs)
maxs=cnt;
}
cout<<maxs<<endl;
}
return 0;
}