BOJ/๐Ÿ…

โ˜…Divide & Conquer Expert(Easy) I - 1 Solvedโ˜…

metamong 2024. 6. 30.

โ˜… 11440 ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์˜ ์ œ๊ณฑ์˜ ํ•ฉ โ˜…

n=int(input())
BIG=1_000_000_007

def get_power(a,b,c):
    if b <= 1:
        return [[a[0][0]%BIG,a[0][1]%BIG],[a[1][0]%BIG,a[1][1]%BIG]]
        
    x = get_power(a,b//2,c)

    if b % 2 == 0:
        return prod(x,x)
    else:
        return prod(prod(x,x),a)

def prod(A,B):
    a,b,c,d=A[0][0],A[0][1],A[1][0],A[1][1]
    e,f,g,h=B[0][0],B[0][1],B[1][0],B[1][1]
    return [[(a*e+b*g)%BIG,(a*f+b*h)%BIG],[(c*e+d*g)%BIG,(c*f+d*h)%BIG]]

core = [[1,1],[1,0]]

matrix_1 = get_power(core,n-1,BIG)
matrix_2 = get_power(core,n,BIG)
print(((matrix_1[0][0]%BIG)*(matrix_2[0][0]%BIG))%BIG)

 

๐Ÿ™‰ F1๋ถ€ํ„ฐ Fn๊นŒ์ง€์˜ ๋ชจ๋“  ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์˜ ์ œ๊ณฑ์˜ ํ•ฉ์€ ์•„๋ž˜์™€ ๊ฐ™์ด Fn*Fn+1 ๊ฒฐ๊ณผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ™‰ ์ด ๋•Œ Fn์—์„œ์˜ n์ด ๋งค์šฐ ํฐ ์ˆ˜์ด๋ฏ€๋กœ Fn์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋ถ„ํ• ์ •๋ณต์„ ์ด์šฉํ•œ ํ–‰๋ ฌ ๊ฑฐ๋“ญ์ œ๊ณฑ์œผ๋กœ O(logn)๋งŒ์— ๊ตฌํ•ด์•ผ ํ•œ๋‹ค. Fn๊ณผ Fn+1์„ ๊ฐ๊ฐ O(logn)๋งŒ์— ๊ตฌํ•œ ๋’ค, modulo multiplication property์— ์˜ํ•ด ((Fn%BIG) * (Fn+1%BIG))%BIG๋ฅผ ์ตœ์ข… ๋‹ต์œผ๋กœ ์ถœ๋ ฅ!


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'BOJ > ๐Ÿ…' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

โ˜…DP Expert(Easy) I - 4 Solvedโ˜…  (0) 2024.01.07
โ˜…์ •๋ ฌ ์ตœ์ƒ๊ธ‰1 - 1๋ฌธ์ œ()โ˜…  (0) 2023.02.07

๋Œ“๊ธ€