SCU - 1269 UVA - 10090 Marbles (拓展欧几里得算法的解空间结构)
2016-08-19 21:19
501 查看
题目:
Description
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 c2and 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
这个题目和另外一个题目差不多,我就不解释了。点击打开我的博客
代码:
函数 f 里面加了n1==n2的特判,因为我自己写的这个gcd函数只能计算2个不同的数的gcd
Description
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 c2and 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
这个题目和另外一个题目差不多,我就不解释了。点击打开我的博客
代码:
#include<iostream> #include<stdio.h> using namespace std; long long x, y; long long gcd(long long a, long long b) { if (a == 0 || b == 0) { x = (b == 0); y = (a == 0); return a + b; } long long r; if (a < 0) { r = gcd(-a, b); x *= -1; return r; } if (b < 0) { r = gcd(a, -b); y *= -1; return r; } if (a >= b)r = gcd(a%b, b); else r = gcd(a, b%a); y -= a / b*x; x -= b / a*y; return r; } void f(long long n, long long c1, long long n1, long long c2, long long n2) { if (c1*n2 < c2*n1) { f(n, c2, n2, c1, n1); long long t = x; x = y; y = t; return; } if (n1 == n2) { x = -1; if (n%n2 == 0) { x = 0; y = n / n2; } return; } long long g = gcd(n1, n2); if (n % g) { x = -1; return; } x *= n / g; y *= n / g; long long s = x / (n2 / g); x -= n2 / g*s; y += n1 / g*s; if (x < 0) { x += n2 / g; y -= n1 / g; } } int main() { long long n, c1, n1, c2, n2; while (cin >> n) { if (n == 0)break; cin >> c1 >> n1 >> c2 >> n2; f(n, c1, n1, c2, n2); if (x < 0 || y < 0)cout << "failed" << endl; else cout << x << " " << y << endl; } return 0; }
函数 f 里面加了n1==n2的特判,因为我自己写的这个gcd函数只能计算2个不同的数的gcd
相关文章推荐
- UVA 10090 - Marbles 拓展欧几里得
- HDU - 1356 The Balance(拓展欧几里得算法的解空间结构)
- uva 10090 - Marbles 扩展欧几里得
- UVA 10090 Marbles 扩展欧几里得
- UVa 10090 - Marbles
- UVA 10090 Marbles(扩展欧几里得)
- UVA, 10090 Marbles
- 习题6-8 空间结构 UVa806
- uva806 spatial structures 空间结构
- UVa Problem Solution: 10090 - Marbles
- uva 10090 - Marbles(欧几里得+通解)
- 拓展欧几里得详解 及其题目 POJ 1061 2115 2142 UVA 10673 10090
- UVa806 习题6-8 空间结构(Spatial Structures,ACM/ICPC World Finals 1998)
- UVA 10090 Marbles(扩展欧几里得)
- UVA 10090 - Marbles (数论)
- Uva 10090 Marbles 扩展欧几里得 费用最小
- oracle之逻辑数据库结构概述(表空间、段、数据拓展和数据块)
- uva 10090 marbles
- UVa Problem 10090 Marbles (弹珠)
- uva 10090 Marbles