20251006 baekjoon
This commit is contained in:
parent
11fe2f4f7e
commit
9ab329938b
57
code_study/Baekjoon/swift/13172.swift
Normal file
57
code_study/Baekjoon/swift/13172.swift
Normal file
@ -0,0 +1,57 @@
|
||||
let Mod = 1000000007
|
||||
|
||||
func power(base a: Int, exponent b: Int) -> Int {
|
||||
if b == 0 {
|
||||
return 1
|
||||
}
|
||||
|
||||
if b == 1 {
|
||||
return a
|
||||
}
|
||||
|
||||
var half = power(base: a, exponent: b/2)
|
||||
half = (half * half) % Mod
|
||||
|
||||
if b%2 == 1 {
|
||||
return (a * half) % Mod
|
||||
}
|
||||
else {
|
||||
return half
|
||||
}
|
||||
}
|
||||
|
||||
func gcd(_ x: Int, _ y: Int) -> Int {
|
||||
var a = max(x,y)
|
||||
var b = min(x,y)
|
||||
|
||||
while b != 0 {
|
||||
let r = a%b
|
||||
a = b
|
||||
b = r
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
if let M = Int(readLine() ?? "") {
|
||||
var result = 0
|
||||
|
||||
for _ in 0..<M {
|
||||
if let input = readLine(),
|
||||
let info = input.split(separator: " ").compactMap({Int($0)}) as? [Int],
|
||||
info.count == 2
|
||||
{
|
||||
var N = info[0]
|
||||
var S = info[1]
|
||||
|
||||
let GCD = gcd(N, S)
|
||||
N /= GCD
|
||||
S /= GCD
|
||||
|
||||
result += S*(power(base: N, exponent: Mod-2))%Mod
|
||||
result %= Mod
|
||||
}
|
||||
}
|
||||
|
||||
print(result)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user