Skip to content

Instantly share code, notes, and snippets.

@koher
Created February 21, 2021 15:29
Show Gist options
  • Save koher/9db4ca29246a58e60755e8d04faa85a8 to your computer and use it in GitHub Desktop.
Save koher/9db4ca29246a58e60755e8d04faa85a8 to your computer and use it in GitHub Desktop.
https://atcoder.jp/contests/arc113/tasks/arc113_b が「7進法」だった場合の解
func readInt3(line: Int = #line, file: String = #file) -> (Int, Int, Int) {
guard let string = readLine() else {
preconditionFailure("No input (at line \(line) in \(file))")
}
let values: [Int] = string.split(separator: " ").map { part in
guard let value = Int(part) else {
preconditionFailure("Illegal input value: \(string) (at line \(line) in \(file))")
}
return value
}
precondition(values.count == 3, "Illegal number of input values: count = \(values.count), values = \(values) (at line \(line) in \(file))")
return (values[0], values[1], values[2])
}
func pow<Integer>(_ a: Integer, _ b: Integer, modulus: Integer?) -> Integer where Integer: BinaryInteger {
var result: Integer = 1
var a = a
var b = b
if let modulus = modulus {
while true {
if b & 0x1 != 0 {
result = (result * a) % modulus
}
b >>= 1
guard b > 0 else { break }
a = (a * a) % modulus
}
} else {
while true {
if b & 0x1 != 0 {
result *= a
}
b >>= 1
guard b > 0 else { break }
a *= a
}
}
return result
}
let (a, b, c) = readInt3()
var k = pow(b, c, modulus: 6)
if k == 0 { k = 6 }
print(pow(a, k, modulus: 7))
//for a in 1 ..< 10 {
// for b in 1 ..< 3 {
// for c in 1 ..< 5 {
// let ans1 = pow(a, pow(b, c, modulus: nil), modulus: nil)
// var k = pow(b, c, modulus: 6)
// if k == 0 { k = 6 }
// let ans2 = pow(a, k, modulus: 7)
// if ans1 % 7 != ans2 {
// print(a, b, c)
// print(ans1, ans2)
// }
// }
// }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment