uva 10090 Marbles
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, 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 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的更多相关文章
- UVA 10090 - Marbles 拓展欧几里得
I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The ...
- UVA 10090 Marbles(扩展欧几里得)
Marbles Input: standard input Output: standard output I have some (say, n) marbles (small glass ball ...
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- uva 10090 二元一次不定方程
Marbles Input: standard input Output: standard output I have some (say, n) marbles (small glass ball ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- UVA 11125 Arrange Some Marbles
dp[i][j][m][n][s]表示最初选择j个i号颜色大理石.当前选择n个m号颜色大理石.剩余大理石状态(8进制数状压表示)最开始没看出状压..sad #include <map> # ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
随机推荐
- 使用gradle创建java程序
创建一个Java项目 我们可以使用Java插件来创建一个Java项目,为了做到这点,我们需要把下面这段语句加入到build.gradle文件中: 1 apply plugin: 'java' 就是这样 ...
- ACM常用算法及练习(1)
ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...
- WOW: 宏
1.常用的宏命令 1.1常用的宏命令 1.释放技能命令 /cast 释放一个或多个技能,可以加入一些条件判断,是最常用的命令 /castsequence 依次释放释放数个技能,同样可以加入一些条件判断 ...
- .net 反编译工具
遇到一个需求,做一个专门访问自己网站的浏览器给用户使用,这个浏览器提供登录功能.此时是一个安装在客户端的exe程序,做登录验证要用到webservice,不能将验证逻辑写入exe中,否则客户端可以利用 ...
- Struts2 标签库讲解
要使用Struts2的标签,只需要在JSP页面添加如下一行定义即可: <%@ taglib prefix="s" uri="/struts-tags"%& ...
- android 内存不足的问题
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring project ' ...
- PTPX Power Analysis Flow
PrimeTime PX工具是PrimeTime工具内的一个feature. PTPX的功耗分析,可以报告出chip,block,cell的各个level的功耗. 使用PTPX可以分析的功耗的方式: ...
- 收缩TempDB的办法(转载)
有时候在数据库上运行一个数据量很大的查询语句,会导致TempDB数据量剧增,具体查看下面链接文章: SqlServer 一个查询语句导致tempdb增大55G 找到TempDB剧增的问题后,接下来的问 ...
- ckeditor添加插入flv视频的插件
首发:个人博客,更新&纠错&回复 昨天写在网页中播放flv的博文的时候,想在博文中插入视频,但是发现无法实现.因为用的编辑器是ckeditor,决定自己写个插件插入视频.官方的教程在这 ...
- PHPCMS V9 学习总结
在实现PHPCMS网站过程中,根据业务需求,我们遇到很多问题,特此总结如下,以便大家参考学习. [1]PHPCMS V9系统目录简析 在研究所有问题之前,请先了解一下系统的文件目录结构,具体如下图所示 ...