Given a box of coins, Type of coins inside the box & weight of each type of coin & total weight of the box, you have to find the amount inside the box.
For example:
Given a box whose weight is 100 gm while it is empty and 150 gm after we place coins inside the box. If coin types are Rs.2 & Rs.5 with weights 5 gm & 12 gm respectively.
Then, the amount inside the box is = Rs.20
Implementation:
INPUT
First line - (T) Number of Testcases
Second line - (P) Weight of an empty box and (Q) Weight of a coin filled box
Third line - (N) Number of Type of coins
Fourth line & Fifth line - (V) Type of coin and (W) Weight of coin
OUTPUT
The minimum amount. If the weight cannot be reached , print "Impossible."
Sample Output
20
For example:
Given a box whose weight is 100 gm while it is empty and 150 gm after we place coins inside the box. If coin types are Rs.2 & Rs.5 with weights 5 gm & 12 gm respectively.
Then, the amount inside the box is = Rs.20
Implementation:
#include <stdio.h>
int main()
{
int T,TW,Q,P,N,i,V[500],W[500],M,Op,count=0,xcount=0,Resx,Resy,Resz,x,y,z,Op1;
scanf("%d",&T);
while(T>0)
{
scanf("%d%d",&P,&Q);
TW=Q-P;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d%d",&V[i],&W[i]);
}
count=0;
for(i=0;i<N;i++)
{
if(TW%(W[i]+W[i+1]+W[i+2])==0)
{
Resx=TW/W[i];
Resy=TW/W[i+1];
Resz=TW/W[i+2];
for(x=1;x<=Resx;x++)
{
for(y=1;y<=Resy;y++)
{
for(z=1;z<=Resz;z++)
{
if(W[i]*x+W[i+1]*y+W[i+2]==TW)
{
Op=V[i]*x+V[i+1]*y+V[i+2]*z;
xcount++;
}
}
}
}
}
else
if(TW%(W[i]+W[i+1])==0||TW%(W[i]+W[i+2])==0)
{
Resx=TW/W[i];
Resy=TW/W[i+1];
for(x=1;x<=Resx;x++)
{
for(y=1;y<=Resy;y++)
{
if(W[i]*x+W[i+1]*y==TW)
{
Op=V[i]*x+V[i+1]*y;
xcount++;
}
}
}
}
else
if(TW%W[i]==0)
{
M=TW/W[i];
Op1=V[i]*M;
count++;
}
}
T--;
if(xcount>0&&count>0&&Op<Op1)
printf("%d\n",Op);
else if(count>0&&Op1<Op)
printf("%d\n",Op1);
else
printf("Impossible.\n");
}
return 0;
}
INPUT
First line - (T) Number of Testcases
Second line - (P) Weight of an empty box and (Q) Weight of a coin filled box
Third line - (N) Number of Type of coins
Fourth line & Fifth line - (V) Type of coin and (W) Weight of coin
OUTPUT
The minimum amount. If the weight cannot be reached , print "Impossible."
Sample Input
1
100 150
2
2 5
5 12
1
100 150
2
2 5
5 12
Sample Output
20