結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー cozy_saunacozy_sauna
提出日時 2022-03-17 00:24:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 3,754 bytes
コンパイル時間 357 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 66,048 KB
最終ジャッジ日時 2024-09-25 03:16:57
合計ジャッジ時間 2,823 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
65,920 KB
testcase_01 AC 65 ms
66,048 KB
testcase_02 AC 62 ms
65,792 KB
testcase_03 AC 66 ms
65,920 KB
testcase_04 AC 60 ms
65,920 KB
testcase_05 AC 60 ms
65,920 KB
testcase_06 AC 60 ms
65,792 KB
testcase_07 AC 59 ms
65,988 KB
testcase_08 AC 60 ms
66,048 KB
testcase_09 AC 59 ms
65,920 KB
testcase_10 AC 59 ms
65,280 KB
testcase_11 AC 59 ms
65,024 KB
testcase_12 AC 60 ms
65,408 KB
testcase_13 AC 58 ms
65,408 KB
testcase_14 AC 59 ms
65,408 KB
testcase_15 AC 60 ms
65,408 KB
testcase_16 AC 59 ms
65,792 KB
testcase_17 AC 59 ms
65,408 KB
testcase_18 AC 59 ms
65,408 KB
testcase_19 AC 59 ms
65,408 KB
testcase_20 AC 58 ms
65,152 KB
testcase_21 AC 58 ms
65,792 KB
testcase_22 AC 58 ms
65,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit, stdin
from collections import defaultdict, deque
from itertools import permutations, combinations, product
from functools import lru_cache
from random import sample, choice, randint, random
from bisect import bisect_left, bisect_right
from heapq import heappush, heappop
from math import factorial, exp
from copy import copy, deepcopy
from time import time

setrecursionlimit(1 << 20)
readline = stdin.readline
# @lru_cache(maxsize = None)
INF = 10 ** 18
# MOD = 998244353
MOD = 1000000007
DYDX = [(-1, 0), (1, 0), (0, -1), (0, 1)]
ALP = 26
''' Input '''
def I(): return int(readline())
def ST(): return readline()[:-1]
def LI(): return list(map(int, readline().split()))
def LII(): return list(map(lambda x: int(x) - 1, readline().split()))
def SPI(): return map(int, readline().split())
def SPII(): return map(lambda x: int(x) - 1, readline().split())
def FIE(x): return [readline()[:-1] for _ in [0] * x]
def NODE(N, edge):
    node = [[] for _ in [0] * N]
    for _ in [0] * edge:
        a, b = map(int, input().split())
        node[a - 1].append(b - 1)
        node[b - 1].append(a - 1)
    return node 
def NODE_W(N, edge):
    node = [[] for _ in [0] * N]
    for _ in [0] * edge:
        a, b, c = map(int, input().split())
        node[a - 1].append((b - 1, c))
        node[b - 1].append((a - 1, c))
    return node

''' Array '''
def cmin(dp, i, x):
    if x < dp[i]: dp[i] = x
def cmax(dp, i, x):
    if x > dp[i]: dp[i] = x

''' Sorted Array '''
# x <= A[i] (minimum i meeting the requirement)
def binary(sortedA, x): return bisect_left(sortedA, x)
# x < A[i] (minimum i meeting the requirement)
def binary2(sortedA, x): return bisect_right(sortedA, x + 1)
# number of x in A
def quantity(sortedA, x): return bisect_right(sortedA, x) - bisect_left(sortedA, x)

''' Alphabet '''
def id_a(s): return ord(s) - ord('a')
def id_A(s): return ord(s) - ord('A')
def st_a(i): return chr(ord('a') + i)
def st_A(i): return chr(ord('A') + i)

''' Other'''
def ranges(*args): return [(i, j) for i in range(args[0]) for j in range(args[-1])]
def nynx(y, x, ly = INF, lx = INF): return [(y + dy, x + dx) for dy, dx in DYDX if 0 <= y + dy < ly and 0 <= x + dx < lx]
def gen(x, *args):
    ret = [x] * args[-1]
    for e in args[:-1][::-1]: ret = [deepcopy(ret) for _ in [0] * e]
    return ret
def is_prime(x):
    if x == 1 or x % 2 == 0: return x == 2
    f = 3
    while f * f <= x:
        if x % f == 0: return False 
        f += 2
    return True 
def gcd(x, y):
    if x == 0: return y
    return gcd(y % x, x)
def lcm(a, b):
    return a // gcd(a,b) * b

''' Output '''
def puts(E):
    for e in E: print(e)
def pprint(E): 
    print()
    for e in E: print(e)
def yn(x): print("Yes" if x else "No")
def YN(x): print("YES" if x else "NO")
def blank(arr): print(" ".join(map(str, arr)))
def link(arr): print(''.join(map(str, arr)))
def debug(*args):
    if DEBUG:
        for e in args: print(e)
DEBUG = False
###############################################################################################

def mat_pow(A, n):
    if not n: return [[1, 0], [0, 1]]
    m = mat_pow(A, n // 2)
    if n % 2:
        return mat_mul(A, mat_mul(m, m))
    else:
        return mat_mul(m, m)

def mat_mul(A, B):
    y, x = len(A), len(A[0])
    yy, xx = len(B), len(B[0])
    if not yy == x: 
        raise Exception('({}, {}) and ({}, {}) cannot be multipled'.format(y, x, yy, xx))
    mat = [[0] * xx for _ in range(y)]
    for i in range(y):
        for j in range(xx):
            mat[i][j] = sum(A[i][k] * B[k][j] for k in range(x)) % MOD
    return mat 

def fibonacci(x):
    return mat_mul(mat_pow([[1, 1], [1, 0]], x), [[1], [0]])[0][0]

N = I()

FN = fibonacci(N)
FNN = fibonacci(N - 1)
print(FN * FNN % MOD)
0