程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Blue Bridge Cup exercise system Fibonacci series (Python)

編輯:Python

Problem description

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 .

Input format

Enter an integer n.

Output format

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 .

Reference code

# 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))

reflection

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 ?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved