UVA10090 Marbles(扩展欧几里得)

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个珠子和两个盒子以及他们的容量和价钱求花费最少的价钱把所有珠子都放进盒子里的情况

思路:扩展欧几里德
n1x+n2y=n。(ax+by=gcd(a,b)=g,如果n%g!=0,则方程无解)
设x1,y1是扩展欧几里得求出的解
可以得出x=nx1/g, y=ny1/g
所以通解为x=nx1/g+bk/g, y=ny1/g-ak/g,
x>=0, y>=0,所以k: -nx/b<=k<=ny/a(k是整数)

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define LL long long
void extend_Eulid(LL a, LL b, LL &x, LL &y, LL &d)
{
    if (!b) {d = a, x = 1, y = 0;}
    else{
        extend_Eulid(b, a % b, y, x, d);
        y -= x * (a / b);
    }
}
int main()
{
    LL n,c1,n1,c2,n2,d;

    while (~scanf("%lld", &n) && n)
    {
        scanf("%lld%lld%lld%lld",&c1,&n1,&c2,&n2);
        LL x,y;
        extend_Eulid(n1,n2,x,y,d);
        LL l=ceil(1.0*-n*x/n2);//向上取整
        LL r=floor(1.0*n*y/n1);//向下取整
        if(n%d||l>r)
        {
            printf("failed\n");
            continue;
        }
        if(c1*n2<c2*n1)
        {
            x=n*x/d+n2/d*r;
            y=n*y/d-n1/d*r;
        }
        else
        {
             x=n*x/d+n2/d*l;
            y=n*y/d-n1/d*l;
        }
        printf("%lld %lld\n",x,y);
    }



}

还有一个和它一样得题:

On one of the Caribbean Islands there are N tourists and you want to move them from this island to another one.

There are only two boats on this island, the first one can hold n1 tourists and cost c1 to move exactly n1 tourists from one Island to another, and the second one can hold n2 and cost c2.

The boat will not sail unless it is fully booked. Moreover, you want to minimize the total cost of moving all tourists from one island to another. You can use any boat several times.

Input
The input may contain multiple test cases. Each test case begins with a line containing an integer N (1 ≤ N ≤ 2 * 109).

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 * 109.

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 non-negative integers m1 and m2, where m1 is the number of times to use the first boat, and m2 is the number of times to use the second boat) if one exists.

Print “failed” otherwise. If a solution exists, you may assume that it is unique.

Examples
Input
43
1 3
2 4
40
5 9
5 12
0
Output
13 1
failed

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值