-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10090.go
61 lines (55 loc) · 998 Bytes
/
10090.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// UVa 10090 - Marbles
package main
import (
"fmt"
"io"
"os"
)
func extendedEuclidean(n1, n2 int) (int, int, int) {
if n2 == 0 {
return 1, 0, n1
}
x, y, d := extendedEuclidean(n2, n1%n2)
x, y = y, x-n1/n2*y
return x, y, d
}
func solve(out io.Writer, n, c1, n1, c2, n2 int) {
x, y, d := extendedEuclidean(n1, n2)
if n%d != 0 {
fmt.Fprintln(out, "failed")
return
}
x *= n / d
y *= n / d
l := -x * d / n2
if x*d%n2 != 0 && l >= 0 {
l++
}
h := y * d / n1
if y*d%n2 != 0 && h <= 0 {
h--
}
if l > h {
fmt.Fprintln(out, "failed")
return
}
if c1*(x+n2/d*l)+c2*(y-n1/d*l) > c1*(x+n2/d*h)+c2*(y-n1/d*h) {
l = h
}
fmt.Fprintln(out, x+n2/d*l, y-n1/d*l)
}
func main() {
in, _ := os.Open("10090.in")
defer in.Close()
out, _ := os.Create("10090.out")
defer out.Close()
var n, c1, n1, c2, n2 int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
fmt.Fscanf(in, "%d%d", &c1, &n1)
fmt.Fscanf(in, "%d%d", &c2, &n2)
solve(out, n, c1, n1, c2, n2)
}
}