This question has already been answered:
There is such a task:
Given integers 1 ≤ n ≤ 10 18 and 2 ≤ m ≤ 10 5 , it is necessary to find the remainder after dividing the n-th Fibonacci number by m.
My decision:
def fib_mod(n, m): # put your code here if n == 0: return 0 number_before = 0 number_current = 1 for i in range(n - 1): temp = number_current number_current += number_before number_current %= m number_before = temp return number_current def main(): n, m = map(int, input().split()) print(fib_mod(n, m)) if __name__ == "__main__": main() The solution seems to be correct, but this algorithm does not pass time testing. I can not understand how else it can be accelerated ...