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 of Type 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

AC代码:

#include<iostream>
#include<cstdio>
using namespace std; long long Extended_Euclid(long long a,long long b,long long &x,long long &y)
{
long long t,d;
if(!b)
{
x=;y=;
return a;
}
d=Extended_Euclid(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return d;
} int main()
{
long long n,c1,n1,c2,n2,x,y,d,x1,y1,min,tx,ty;
while(scanf("%lld",&n),n)
{
scanf("%lld %lld %lld %lld",&c1,&n1,&c2,&n2);
d=Extended_Euclid(n1,n2,x,y);
if(n%d==)
{
n1/=d;
n2/=d;
n/=d;
x=x*n;
y=y*n;
x1=(x%n2+n2)%n2;
y1=(n-x1*n1)/n2;
tx=x1;
ty=y1;
min=-;
if (y1 >= )
min = (x1*c1 + y1*c2) ;
y1=(y%n1+n1)%n1;
x1=(n-n2*y1)/n1;
if ((min > x1*c1 + y1*c2 || min == -) && x1 >= )
{
tx = x1 ;
ty = y1 ;
}
if(min!=-)
printf("%lld %lld\n",tx,ty);
else
printf("failed\n");
}
else
printf("failed\n");
}
return ;
}

uva 10090 二元一次不定方程的更多相关文章

  1. poj 2115 二元一次不定方程

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14765   Accepted: 3719 Descr ...

  2. poj 1061 青蛙的约会(二元一次不定方程)

      Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...

  3. P5656 【模板】二元一次不定方程(exgcd)

    还不会 exgcd 的请移步窝的学习笔记,这里只讲怎么搞出烦人的答案. 在 \(a,b\) 两者互质的情况下,二元一次不定方程的通解:\(a(x+db)+b(y+da)=c\). 所以要先将 \(a, ...

  4. exgcd、二元一次不定方程学习笔记

    (不会LATEX,只好用Word) ( QwQ数论好难) 再补充一点,单次询问a,b求逆元的题可以直接化简然后套用exgcd求解. 例题:https://www.luogu.org/problemne ...

  5. UVA 10090 Marbles 扩展欧几里得

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

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

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

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

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

  8. uva 10090 Marbles

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

  9. 奇异值分解(SVD)和最小二乘解在解齐次线性超定方程中的应用

    奇异值分解,是在A不为方阵时的对特征值分解的一种拓展.奇异值和特征值的重要意义相似,都是为了提取出矩阵的主要特征. 对于齐次线性方程 A*X =0;当A的秩大于列数时,就需要求解最小二乘解,在||X| ...

随机推荐

  1. 双击窗体是模拟键盘上的Tab键

    实现效果: 知识运用: SendKeys类的Send方法 //向活动应用程序发送击键 public static void Send (string keys) 实现代码: private void ...

  2. CAD交互绘制批注(网页版)

    js中实现代码说明: 动态拖放时的绘制事件: function DynWorldDrawComment( pCustomEntity, pWorldDraw, curPt) { // 得到绘制参数. ...

  3. 简单的 创建AJax的方法

    // 简单的ajax对象 var myAjax = { // XMLHttpRequest IE7+, Firefox, Chrome, Opera, Safari : ActiveXObject I ...

  4. C++函数调用过程深入分析<转>

    转自http://blog.csdn.net/dongtingzhizi/article/details/6680050 C++函数调用过程深入分析 作者:靠谱哥 微博:洞庭之子-Bing 0. 引言 ...

  5. Day5 集合的深浅copy

    集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. 关系测试, ...

  6. C语言中函数参数传递

    C语言中函数参数传递的三种方式 (1)值传递,就是把你的变量的值传递给函数的形式参数,实际就是用变量的值来新生成一个形式参数,因而在函数里对形参的改变不会影响到函数外的变量的值.(2)地址传递,就是把 ...

  7. shelll脚本,根据软链接,找到真实路径

    [root@localhost tmp]# ls -l total lrwxrwxrwx root root Sep : abc -> /etc/passwd lrwxrwxrwx root r ...

  8. ios 自定义RadioButton

    1 前言 众所周知在IOS中没有单选按钮这一控件,今天我们来学习一下简单的单选控件.类似与Web中的radio表单元素. 2 详述 本控件单纯的利用按钮控件和NSObject的respondsToSe ...

  9. lsof指令使用简介

    lsof替代了netstat和ps的全部工作.它可以带来那些工具所能带来的一切,而且要比那些工具多得多 最重要的是,当你给它传递选项时,默认行为是对结果进行“或”运算.因此,如果是用-i来拉出一个端口 ...

  10. perl:split函数用法

    本文和大家重点讨论一下Perl split函数的用法,Perl中的一个非常有用的函数是Perl split函数-把字符串进行分割并把分割后的结果放入数组中.这个Perl split函数使用规则表达式( ...