Baby Ming and Weight lifting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1439 Accepted Submission(s): 525
Problem Description
Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which can be ignored) and two different kinds of barbell disks(the weight of which are respectively
a
and
b
), the amount of each one being infinite.
Baby Ming prepare to use this two kinds of barbell disks to make up a new one weighted C
(the barbell must be balanced), he want to know how to do it.
Baby Ming prepare to use this two kinds of barbell disks to make up a new one weighted C
![](https://i-blog.csdnimg.cn/blog_migrate/b4b84567b13ee4486778964422b316b1.jpeg)
Input
In the first line contains a single positive integer
T
, indicating number of test case.
For each test case:
There are three positive integer a,b
, and
C
.
1≤T≤1000,0<a,b,C≤1000,a≠b
For each test case:
There are three positive integer a,b
1≤T≤1000,0<a,b,C≤1000,a≠b
Output
For each test case, if the barbell weighted
C
can’t be made up, print Impossible.
Otherwise, print two numbers to indicating the numbers of a
and
b
barbell disks are needed. (If there are more than one answer, print the answer with minimum
a+b
)
Otherwise, print two numbers to indicating the numbers of a
Sample Input
2 1 2 6 1 4 5
Sample Output
2 2 Impossible
Source
Recommend
把每种情况都枚举一遍。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int t,m,n,i,j,k,l,sum,minn,maxx,num,flag,a,ans1,ans2;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&m,&n,&sum);
if(sum%2)
{
printf("Impossible\n");
continue;
}
else
{
minn=min(n,m);
maxx=max(n,m);
}
if(maxx*2>sum)//不能用大的
{
if(sum%minn)
printf("Impossible\n");
else
{
if(sum/minn%2)
printf("Impossible\n");
else
{
num=sum/minn;
if(minn==m)
printf("%d 0\n",num);
else
printf("0 %d\n",num);
}
}
}
else
{
flag=0;
l=sum/maxx;
for(i=l;i>=0;i--)
{
if(flag)
break;
if((sum-i*maxx)%minn==0)
{
if(flag)
break;
a=(sum-i*maxx)/minn;
for(j=i;j>=0;j--)
{
if(flag)
break;
for(k=0;k<=a;k++)
{
if(k*minn+j*maxx==sum/2)//如果能构成一半就可以,比较机智的一个地方
{
ans1=i;
ans2=a;
flag=1;
}
}
}
}
}
if(flag==0)
printf("Impossible\n");
if(flag==1)
{
if(minn==m)
printf("%d %d\n",ans2,ans1);
else
printf("%d %d\n",ans1,ans2);
}
}
}
return 0;
}