20251102 baekjoon

This commit is contained in:
songyc macbook 2025-11-02 18:53:56 +09:00
parent 60de4d4536
commit dab2d539de
3 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,25 @@
MOD = 1000000007
def power(x,n) :
if n==0 :
return 1
if n==1 :
return x%MOD
half = power(x,n//2)
result = (half*half)%MOD
if n%2==1 :
result = (result*x)%MOD
return result
inverse_element_MOD = lambda x: power(x, MOD-2)
expect_value = lambda a, b : (a*inverse_element_MOD(b))%MOD
result = 0
for _ in range(int(input())) :
S, N = map(int, input().split())
result = (result + expect_value(N,S))%MOD
print(result)

View File

@ -0,0 +1 @@
print('\n'.join((["285", "syc725"])))

View File

@ -0,0 +1,35 @@
let Mod: Int64 = 1000000007
func matrix_multiply_2by2(_ A: [[Int64]], _ B: [[Int64]]) -> [[Int64]] {
var result = Array(repeating: Array(repeating: Int64(0), count: 4), count: 4)
result[0][0] = (A[0][0]*B[0][0] + A[0][1]*B[1][0])%Mod
result[0][1] = (A[0][0]*B[0][1] + A[0][1]*B[1][1])%Mod
result[1][0] = (A[1][0]*B[0][0] + A[1][1]*B[1][0])%Mod
result[1][1] = (A[1][0]*B[0][1] + A[1][1]*B[1][1])%Mod
return result
}
func power_matrix_2by2(_ A: [[Int64]], exponetioal n: Int64) -> [[Int64]] {
if n == 0 {
return [[1,0],[0,1]]
}
if n == 1 {
return A
}
let half = power_matrix_2by2(A, exponetioal: n/2)
let result = matrix_multiply_2by2(half, half)
if n%2 == 1 {
return matrix_multiply_2by2(result, A)
}
return result
}
if let n = Int64(readLine() ?? "") {
print(power_matrix_2by2([[1,1],[1,0]], exponetioal: n)[1][0])
}