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. Python实现1-9数组形成的结果为100的所有运算式

    问题: 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性.例如:1 + 2 + 34–5 + 67–8 + 9 = 100. f ...

  2. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  3. 升级nodejs 与短小的n模块

    要用指令升级nodejs到新版本要先安装n模块 window用不了n模块  可以用 nvm-windows : https://github.com/coreybutler/nvm-windows n ...

  4. 【数位dp】bzoj3209: 花神的数论题

    Description 背景众所周知,花神多年来凭借无边的神力狂虐各大 OJ.OI.CF.TC …… 当然也包括 CH 啦.描述话说花神这天又来讲课了.课后照例有超级难的神题啦…… 我等蒟蒻又遭殃了. ...

  5. [LUOGU] P4767 [IOI2000]邮局

    https://www.luogu.org/problemnew/show/P4767 四边形不等式好题! 可以设f[i][j]表示前i个村庄,建了j个邮局的最小代价. 转移:f[i][j]=min{ ...

  6. Web框架之Django_04 模板层了解(过滤器、标签、自定义过滤器、标签、inclusion_tag、模板的继承与导入)

    摘要: 模版层(模板语法) 模板语法 过滤器 标签 自定义过滤器.标签 inclusion_tag 模板的继承 模板的导入 一.模板语法: 常用语法:{{     }}    变量相关{% %}    ...

  7. day20-python之装饰器

    1.装饰器 #!/usr/bin/env python # -*- coding:utf-8 -*- import time def cal(l): start_time=time.time() re ...

  8. JavaScript脚本在页面中放置的位置

    JavaScript脚本通常放置在三个位置: 1.head部分JavaScript脚本. 2.body部分JavaScript脚本. 3.单独以.js结尾的文件中的JavaScript脚本. 客户端会 ...

  9. RHEL6.5上升级OpenSSH7.4p1

    由于升级OpenSSH涉及到安全性问题,为保险起见,在升级前最好安装telnet服务作为备用方案,然后在升级成功后再停止telnet即可. 一.OpenSSH升级相关源码包下载地址 zlib  htt ...

  10. vscode and python

    http://robotkang.cc/2017/04/VS-Code-%E9%85%8D%E7%BD%AEPython/