uva 10090 二元一次不定方程
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 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 二元一次不定方程的更多相关文章
- poj 2115 二元一次不定方程
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14765 Accepted: 3719 Descr ...
- poj 1061 青蛙的约会(二元一次不定方程)
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...
- P5656 【模板】二元一次不定方程(exgcd)
还不会 exgcd 的请移步窝的学习笔记,这里只讲怎么搞出烦人的答案. 在 \(a,b\) 两者互质的情况下,二元一次不定方程的通解:\(a(x+db)+b(y+da)=c\). 所以要先将 \(a, ...
- exgcd、二元一次不定方程学习笔记
(不会LATEX,只好用Word) ( QwQ数论好难) 再补充一点,单次询问a,b求逆元的题可以直接化简然后套用exgcd求解. 例题:https://www.luogu.org/problemne ...
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- 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
Problem F Marbles Input: standard input Output: standard output I have some (say, n) marbles (small ...
- 奇异值分解(SVD)和最小二乘解在解齐次线性超定方程中的应用
奇异值分解,是在A不为方阵时的对特征值分解的一种拓展.奇异值和特征值的重要意义相似,都是为了提取出矩阵的主要特征. 对于齐次线性方程 A*X =0;当A的秩大于列数时,就需要求解最小二乘解,在||X| ...
随机推荐
- UVA10917 A walk trough the Forest (最短路,dp)
求出家到其他点的最短路径,题目的条件变成了u->v不是回头路等价于d[u]>d[v]. 然后根据这个条件建DAG图,跑dp统计方案数,dp[u] = sum(dp[v]). #includ ...
- ucosii(2.89)mutex 应用要点
mutex 的创建在于共享资源打交道是可以可以保证满足互斥条件:1,必须保证继承优先级要高于可能与相应共享资源打交道的任务中优先级最高的优先级.2,不要将占有Mutex的任务挂起,也不要让占有mute ...
- python_108_格式化字符串format函数
#通过关键字映射 print('I am {name},age {age}'.format(name='qiqi齐',age=18))#I am qiqi齐,age 18 dictory={'name ...
- sql server 处理分母为空
SP 前面加下面设置,会忽略错误结果 直接返回null 不会导致SP 失败 SET ANSI_WARNINGS OFFSET ARITHABORT OFFSET ARITHIGNORE ON
- jQuery绑定动态元素的点击事件无效
之前就一直受这个问题的困扰,在写ajax加载数据的时候发现,后面追加进来的demo节点元素,失去了之前的点击事件.为什么点击事件失效,我们该怎么去解决呢?那么,我们通过下面的示例简单说明. 示例如下: ...
- iOS使用技巧---高效使用你的xcode
推荐一遍好文章:绝对可以学到关于xcode的很多哟 转载自cocoachina: http://www.cocoachina.com/ios/20140731/9284.html
- 利用React遍历数组,并且用数组的元素生成<li>arrItem</li>标签组
var numbers = [1,2,3,4,5,6,7,8,9]; ReactDom.render({ <ul> { numbers.map(function(item){ return ...
- TortoiseSVN文件夹及文件图标不显示解决方法---20150515
由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能正常的使用,但是就是文件夹或文件夹的左下角就是不显示图标,这个问题前一段时间就遇到了(那个时候没找到合适的答案) ...
- 数据库事务ACID和事务的隔离级别
借鉴:https://blog.csdn.net/zh521zh/article/details/69400053和https://blog.csdn.net/May_3/article/detail ...
- django第三天(路由基础和路由分配)
路由基础 url(正则路径,视图函数地址,默认关键字参数,路由别名) 路由由上而下匹配, ""可以匹配任意路由 "^$"来匹配"/" url ...