zoj 3706 Break Standard Weight(dp)

时间:2021-12-01 06:05:05

Break Standard Weight


Time Limit: 2 Seconds                                     Memory Limit: 65536 KB                            

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 6 exactly.

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 4 and 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 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 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 integers x 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

Author: YU, Zhi                                                     Contest: The 10th Zhejiang Provincial Collegiate Programming Contest

题意:两个砝码,将其中一个拆分后,得到三个数字,问这三个数字通过加减能组合成多少个数字
思路:1、首先是得到这三个数,怎么得到呢?假设两个砝码为x,y,可以分别把x、y进行分解,分解成两部分,可以x/2,y/2来枚举,并且要从小到大排序 然后标记,以免重复计算
2、然后是进行计算个数。dp[i][j]表示在第i个数时,j是否可以通过三个数字组合而成,dp[i][j]=1表示可以组合成j这个数字
3、然后枚举sum,for(int j=sum;j>=1;j--)进行判断,如果j>=当前的数字tmp,那么if(dp[i-1][j-tmp]==1)(j-tmp表示可以组成),能组成j-tmp的话,那么加上tmp也是可以的,所以dp[i][j]=1;
如果dp[i-1][j]==1,即能组成j的话,那么dp[i][j-tmp]=1,即j-w也是可以组成的
如果j<tmp,那么当 dp[i-1][j]=1 ,即j能组成的话,dp[i-1][tmp-j]=1 即j-w也能组成
然后统计所以得情况即可
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define N 106
int vis[N][N][N];
int m[];
int ans;
int dp[][N<<];
int sum;
bool cmp(int a,int b)
{
return a>b;
}
void solve()
{
memset(dp,,sizeof(dp));
dp[][]=;
for(int i=;i<=;i++)
{
int tmp=m[i];
memcpy(dp[i],dp[i-],sizeof(dp[]));
for(int j=sum;j>=;j--)
{
if(j>=tmp)
{
if(dp[i-][j-tmp])
dp[i][j]=;
if(dp[i-][j])
dp[i][j-tmp]=;
}
else
{
if(dp[i-][j])
dp[i-][tmp-j]=;
}
}
}
int cnt=;
for(int i=;i<=sum;i++)
if(dp[][i]==)
cnt++;
if(ans<cnt)
ans=cnt;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int x,y;
sum=;
scanf("%d%d",&x,&y);
sum=x+y;
ans=;
memset(vis,,sizeof(vis));
m[]=;
for(int i=;i<=x/;i++)
{
m[]=i;
m[]=x-i;
m[]=y;
sort(m,m++,cmp);
if(!vis[m[]][m[]][m[]])
{
vis[m[]][m[]][m[]]=;
solve();
} }
for(int i=;i<=y/;i++)
{
m[]=i;
m[]=y-i;
m[]=x;
sort(m,m++,cmp);
if(!vis[m[]][m[]][m[]])
{
vis[m[]][m[]][m[]]=;
solve();
}
}
printf("%d\n",ans);
}
return ;
}