Fibonacci The recurrence formula of sequence is :Fn=Fn-1+Fn-2, among F1=F2=1.
When n The larger the ,Fn It's also very large. , Now we want to know ,Fn Divide 10007 What is the remainder of .
Enter an integer n.
Output one line , Contains an integer , Express Fn Divide 10007 The remainder of .
explain : In the subject , The answer is demand Fn Divide 10007 The remainder of , So we just need to work out the remainder , You don't have to work out Fn The exact value of , Then divide the result of the calculation by 10007 Take the remainder , It is often easier to calculate the remainder directly than to calculate the original number first and then take the remainder .
# Method 1 :
n = int(input())
f1, f2, f3 = 1, 1, 1
for i in range(n-2):
f3 = (f1 + f2) % 10007
f1, f2 = f2, f3
print(f3)
# Method 2 :
n=int(input())
list=[1,1]
for i in range(0,n-2):
s=list[0]+list[1]
list[0]=list[1]
list[1]=s%10007
print(list[1])
# Method 3 :
# Run timeout
n = int(input())
def f(n):
if n == 1 or n == 2:
return 1
else:
return (f(n-2) + f(n-1)) % 10007
print(f(n))
hold Fn-1+Fn-2 Yes 10007 The remainder will be given to Fn Do the next operation , This is not a change Fibonacci The recurrence formula of the sequence ?