Problem F

Marbles

Input: standard input

Output: standard output

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:

Type 1: each box costs c1 Taka and can hold exactly n1 marbles

Type 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, c1c2n1 and nare 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 ofType i 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

___________________________________________________________________

Rezaul Alam Chowdhury

“The easiest way to count cows in a grazing field is to count how many hooves are there and then divide it by four!”

题意很简单:ax+by=c; 求c1x+c2y的最小值。

首先要说一下两个函数的区别。

    floor(1.00001) = 1; floor(1.99999) = 1;

    ceil(1.00001) = 2; ceil(1.99999) =2;

    其实是对函数的取整的问题。

思路:当然,首先要判断是否有解,这个过程。。  g=gcd(a,b);

由于 x = x*c/g + k*(b/g);

y = y*c/g  - k*(a/g);  x>=0 && y>=0 ,因为不能能买负数个东西。

==> x*c/b <=k <=c*y/a;

   ok,这个就是k的取值范围。

这里就要用到一个问题,k是整数,如果取值才是合理的呢?

ceil(x*c/b)<=k<=floor(c*y/a);

这里不解释,1.24<=k<=4.25  ==> 2<=k<=4;?? enen .

现在k的范围求出来了,那么现在就是求对应的x,和y的值了。

有式子  c1x+c2y = c1*x+c2*(c-a*x)/b = c1*x - c2*a/b*x + c2*a/b;

就是化简成只有x的情况进行讨论。

我们只需要看c1*x - c2*a/b*x这一部分, x*(  c1-c2*a/b  )

当c1-c2*a/b<0的时候,x应该越到越好,这就可以根据已经求出的k来做了。

当c1-c2*a/b>0的时候,x应该越小越好。同理。

当c1-c2*a/b=0的时候,当然,就随意在前面一种情况里都是一样的。

code:

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long LL; LL Ex_GCD(LL a,LL b,LL &x,LL& y)
{
if(b==)
{
x=;
y=;
return a;
}
LL g=Ex_GCD(b,a%b,x,y);
LL hxl=x-(a/b)*y;
x=y;
y=hxl;
return g;
}
int main()
{
LL n,c1,n1,c2,n2;
LL c,a,b,x,y,g;
while(scanf("%lld",&n)>)
{
if(n==)break;
scanf("%lld%lld",&c1,&n1);
scanf("%lld%lld",&c2,&n2);
a=n1;
b=n2;
c=n;
g=Ex_GCD(a,b,x,y);
if(c%g!=)
{
printf("failed\n");
continue;
}
LL lowx =ceil ( -1.0*x*c/(double)b);
LL upx = floor( c*y*1.0/(double)a );
if(upx<lowx)
{
printf("failed\n");
continue;
}
if(c1*b<=a*c2)/** x越大越好,就取上限值 */
{
x=x*(c/g)+upx*(b/g);
y=y*(c/g)-upx*(a/g);
}
else
{
x=x*(c/g)+lowx*(b/g);
y=y*(c/g)-lowx*(a/g);
}
printf("%lld %lld\n",x,y);
}
return ;
}

uva 10090 Marbles的更多相关文章

  1. UVA 10090 - Marbles 拓展欧几里得

    I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The ...

  2. UVA 10090 Marbles(扩展欧几里得)

    Marbles Input: standard input Output: standard output I have some (say, n) marbles (small glass ball ...

  3. UVA 10090 Marbles 扩展欧几里得

    来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...

  4. uva 10090 二元一次不定方程

    Marbles Input: standard input Output: standard output I have some (say, n) marbles (small glass ball ...

  5. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  6. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  7. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  8. UVA 11125 Arrange Some Marbles

    dp[i][j][m][n][s]表示最初选择j个i号颜色大理石.当前选择n个m号颜色大理石.剩余大理石状态(8进制数状压表示)最开始没看出状压..sad #include <map> # ...

  9. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

随机推荐

  1. [转]MongoDB for Java】Java操作MongoDB

    原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...

  2. C++新手之详细介绍MFC

     MFC (Microsoft Foundation Class Library)中的各种类结合起来构成了一个应用程序框架,它的目的就是让程序员在此基础上来建立Windows下的应用程序,这是一种相对 ...

  3. Android Handler简单使用

    package com.example.myhandlertest3; import android.os.Bundle; import android.os.Handler; import andr ...

  4. java 项目打包流程速记

    1.与资源库同步 2.[解决冲突] --可能没有这一步 3.合并标记 4.清除一下项目-- clean 5.打包: run As -->Maven install 6.去服务备份原包,下载服务 ...

  5. JQuery获取和设置Select选项常用方法总结

    1.获取select 选中的 text: $("#cusChildTypeId").find("option:selected").text(); $(&quo ...

  6. struts拦截器

    struts中的拦截器相当于过滤器的作用 一在struts.xml中配置拦截器或拦截器栈 <interceptors>!--全部的拦截器 <interceptor name=&quo ...

  7. 夺命雷公狗---微信开发53----网页授权(oauth2.0)获取用户基本信息接口(3)实现世界留言版

    前面两节课我们讲的是base型的授权了,那么现在我们开始Userinfo型授权, 先来看下我们的原理图 我们这节课来做一个 世界留言版 系统 1..首先我还是在微信测试平台那里设置好回调页面的域名 2 ...

  8. android studio1.0 for Mac环境搭建与demo运行(手动下载gradle,科学上google) 转载

    http://blog.csdn.net/allenffl/article/details/41957907 官网下载 http://developer.android.com/sdk/install ...

  9. 「Ruby && Sqlite3」How to install sqlite3 for ruby? (solve: sqlite-ruby no such file...)

    error message:           no such file .... 安装 gem install sqlite3-ruby -- --with-sqlite3-dir=/usr/lo ...

  10. 创建数据库和表的SQL语句【转】

    创建数据库和表的SQL语句 转至http://www.cnblogs.com/philanthr/archive/2011/08/09/2132398.html 创建数据库的SQL语句: 1 crea ...