-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path113.go
52 lines (46 loc) · 773 Bytes
/
113.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
// UVa 113 - Power of Cryptography
// k^n = p
// ln(k) = ln(p) / n
// k = e^(ln(p)/n)
package main
import (
"fmt"
"math/big"
"os"
)
const max = 1000000000
func main() {
in, _ := os.Open("113.in")
defer in.Close()
out, _ := os.Create("113.out")
defer out.Close()
var ps string
var p, tmp big.Int
var ns int64
for {
if _, err := fmt.Fscanf(in, "%d\n%s", &ns, &ps); err != nil {
break
}
p.SetString(ps, 10)
n := big.NewInt(ns)
if p.Cmp(n) == 0 {
fmt.Fprintln(out, 1)
} else {
var i1, i2 int64 = 0, max
for {
var i = (i1 + i2 + 1) / 2
k := big.NewInt(i)
tmp.Exp(k, n, nil)
if tmp.Cmp(&p) == 0 {
fmt.Fprintln(out, i)
break
}
if tmp.Cmp(&p) > 0 {
i2 = i
} else {
i1 = i
}
}
}
}
}