UVA 10090-marbles expands Euclid

Source: Internet
Author: User


I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The
Boxes is of 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 is lled to their capacity and also to minimize the total cost of
Buying them. Since I? nd it di?cult for me to? Gure off how to distribute my marbles among the
boxes, I seek your help. I want your program to be e?cient also.
Input
The input Le 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 is all positive integers have the values smaller than 2,000,000,000.
A test case containing a zero for n in the-rst line terminates the input.
Output
For each test case in the input print a line containing the minimum cost solution (the nonnegative
Integers m1 and m2, where mi = number of T Ypei boxes required) If one exists, print ' failed ' otherwise.
If A solution exists, you could assume that it is unique.
Sample Input
43
1 3
2 4
40
5 9
5 12
0
Sample Output
13 1
Failed





Test Instructions :

 give you n a ball, give you two kinds of boxes the first box C1 dollars per box, you can just fit N1 ball, the second box C2 yuan per box, you can just fit n2 a ball. Find a way to load the N balls into a box, each filled with a minimum of money.



Puzzle: http://blog.csdn.net/lyhvoyage/article/details/37932481



Suppose the first box buys M1, the second box buys M2, then n1*m1 + n2*m2 = N. By extending Euclidean ax+by=gcd (A, b) = g, if n%g!=0, then the equation has no solution.



The two equations can be solved m1=nx/g, m2=ny/g, so the general solution is m1=nx/g + bk/g, m2=ny/g-ak/g,



Also because M1 and M2 cannot be negative, so m1>=0, m2>=0, so the range of K is-nx/b <= K <= ny/a, and K must be an integer.



Assume



K1=ceil (-nx/b)



K2=floor (ny/b)



If K1>K2, then K does not have a workable solution, so it is also a case of no solution.



Cost = c1*m1 + c2*m2,



Put M1 and M2 expressions in



Cost=c1* (-xn/g+bk/g) +c2* (yn/g-ak/g) = ((b*c1-a*c2)/g) *k+ (c1*x*n+c2*y*n)/g



This is a function of K, and monotonicity is determined by B*C1-A*C2.



If the B*C1-A*C2 >= 0,k takes the minimum (K1), it is least expensive, otherwise, K takes the maximum (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\n"); continue;
        }
        ll k1 = ceil(-n * x * 1.0/ n2);
        ll k2 = floor(n * y * 1.0/ n1); if(k1>k2) {
            printf("failed\n"); 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\n", l , r);
    } return 0;
}





UVA 10090-marbles expands Euclid


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.