zoukankan      html  css  js  c++  java
  • ZOJ3706Break Standard Weight

    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
    
    ----------------------------------------------------------------------------------
    这题的意思就是给你两个整数(砝码的质量),现对其中一个数进行拆分,问怎样拆分才能尽可能多地称量出不同质量的物体。
    例如:有重4g和9g的两个砝码,现将4g拆分为1g和3g,则1,3,9三个砝码最多能称重量为1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
    11, 12, 13的物体,一共有13种。
    我的思路是分别对两个数进行拆分,每一次拆分后使用母函数求出所有可能的重量,然后将这些不同重量的数量存入数组中,
    最后从数组找到最大值就为此题答案了。这题也可以直接枚举所有的情况,同样也将所有情况存入数组,再从数组中找到最大值。
    #include<stdio.h>
    int ans[10005],p;
    void muhanshu(int a,int b,int c)
    {
    	int x[6],c1[10005]={0},c2[10005]={0},sum,count=0;
    	int i,j,k;
    	x[0]=a;x[1]=b;x[2]=c;
    	x[3]=-a;x[4]=-b;x[5]=-c;
    	sum=x[0];
    	for(i=0;i<=sum;i+=x[0])
    	{
    		c1[i]=1;
    		c2[i]=0;
    	}
    	for(i=1;i<6;i++)
    	{
    		for(j=0;j<=sum;j++)
    		{
    			if(x[i]>0)
    			{
    				for(k=0;k<=x[i];k+=x[i])
    				{
    					if(j+k<0)continue;
    					c2[j+k]+=c1[j];
    				}
    			}
    			else
    			{
    				for(k=x[i];k<=0;k-=x[i])
    				{
    					if(j+k<0)continue;
    					c2[j+k]+=c1[j];
    				}
    			}
    		}
    		if(x[i]>0)sum+=x[i];
    		for(j=0;j<=sum;j++)
    		{
    			c1[j]=c2[j];
    			c2[j]=0;
    		}
    	}
    	for(i=1;i<=sum;i++)
    		if(c1[i]>0)
    			count++;
    	ans[p++]=count;
    }
    int main()
    {
    	int a,b,t,i,max;
    	scanf("%d",&t);
    	while(t--)
    	{
    		p=0;
    		scanf("%d%d",&a,&b);
    		for(i=1;i<=a/2;i++)
    			muhanshu(i,a-i,b);
    		for(i=1;i<=b/2;i++)
    			muhanshu(i,b-i,a);
    		max=ans[0];
    		for(i=1;i<p;i++)
    			if(max<ans[i])
    				max=ans[i];
    		printf("%d\n",max);
    	}
    	return 0;
    }
  • 相关阅读:
    eclipse 注释模板
    解决win7访问不了局域网共享文件
    java 执行command
    解决Unable to reach a settlement: [diffie-hellman-group1-sha1, diffie-hellman-group-exchange-sha1] and [curve25519-sha256@li
    解决java.lang.UnsupportedClassVersionError
    hadoop命令备忘
    intellij 提交代码到git
    java用代理访问
    解决 jersey javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
    解决Unable to locate Kerberos realm
  • 原文地址:https://www.cnblogs.com/tengtao93/p/3087662.html
Copyright © 2011-2022 走看看