I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The
boxes are of two types:
T ype 1: each box costs c1 Taka and can hold exactly n1 marbles
T ype 2: each box costs c2 Taka and can hold exactly n2 marbles
I want each of the used boxes to be filled to its capacity and also to minimize the total cost of
buying them. Since I find it difficult for me to figure out how to distribute my marbles among the
boxes, I seek your help. I want your program to be efficient also.
Input
The input file may contain multiple test cases. Each test case begins with a line containing the integer
n (1 ≤ n ≤ 2,000,000,000). The second line contains c1 and n1, and the third line contains c2 and n2.
Here, c1, c2, n1 and n2 are all positive integers having values smaller than 2,000,000,000.
A test case containing a zero for n in the first line terminates the input.
Output
For each test case in the input print a line containing the minimum cost solution (two nonnegative
integers m1 and m2, where mi = number of T ypei boxes required) if one exists, print ‘failed’ otherwise.
If a solution exists, you may assume that it is unique.
Sample Input
43
1 3
2 4
40
5 9
5 12
0
Sample Output
13 1
failed
题意:给你n个球,给你两种盒子第一种盒子每个盒子c1美元,可以恰好装n1个球;第二种盒子每个盒子c2元,可以恰好装n2个球。找出一种方法把这n个球装进盒子,每个盒子都装满,并且花费最少的钱。
题解:http://blog.csdn.net/lyhvoyage/article/details/37932481
假设第一种盒子买m1个,第二种盒子买m2个,则n1*m1 + n2*m2 = n。由扩展欧几里得 ax+by=gcd(a,b)= g,如果n%g!=0,则方程无解。
联立两个方程,可以解出m1=nx/g, m2=ny/g,所以通解为m1=nx/g + bk/g, m2=ny/g - ak/g,
又因为m1和m2不能是负数,所以m1>=0, m2>=0,所以k的范围是 -nx/b <= k <= ny/a,且k必须是整数。
假设
k1=ceil(-nx/b)
k2=floor(ny/b)
如果k1>k2的话则k就没有一个可行的解,于是也是无解的情况。
设花费为cost,则cost = c1*m1 + c2*m2,
把m1和m2的表达式代入得
cost=c1*(-xn/g+bk/g)+c2*(yn/g-ak/g) = ((b*c1-a*c2)/g)*k+(c1*x*n+c2*y*n)/g
这是关于k的一次函数,单调性由b*c1-a*c2决定。
若b*c1-a*c2 >= 0,k取最小值(k1)时花费最少;否则,k取最大值(k2)时花费最少。
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std ; typedef long long ll; const int N=550; /*m1*n1 + m2*n2 = n; a = n1; b = n2; a*x + b*y = g m1 = x*n/g + k*b/g; m2 = y*n/g - k*a/g; cost = m1*c1 + m2*c2; cost = x*n*c1/g + k*b*c1/g + y*n*c2/g - k*a*c2/g ; cost = k*(b*c1 - a*c2)/g + (y*n*c2+x*n*c1)/g; */ ll ExpGcd(ll a,ll b,ll &x,ll &y) { ll temp,p; if(b==0) { x=1; y=0; return a; } p=ExpGcd(b,a%b,x,y); temp=x; x=y; y=temp-(a/b)*y; return p; } ll n,x,y,c1,c2,n1,n2,l,r; int main() { while(scanf("%lld",&n)!=EOF) { if(n == 0) break; scanf("%lld%lld",&c1,&n1); scanf("%lld%lld",&c2,&n2); ll g = ExpGcd(n1,n2,x,y); if(n % g != 0) { printf("failed "); continue; } ll k1 = ceil(-n * x * 1.0/ n2); ll k2 = floor(n * y * 1.0/ n1); if(k1>k2) { printf("failed "); continue; } if((c2*n1 - c1*n2)>0) { l = n2 / g * k2 + n/g * x; r = n / g * y - n1 / g * k2; } else { l = n2 / g * k1 + n / g * x; r = n / g * y - n1 / g * k1; } printf("%lld %lld ", l , r); } return 0; }