Blue Bridge Cup Test Questions-Introductory Training Fibonacci Sequence

Resource limit time limit: 1.0s memory limit: 256.0MB

Problem description The recursive formula of Fibonacci sequence is: Fn=Fn-1+Fn-2, where F1=F2=1.
When n is relatively large, Fn is also very large. Now we want to know what is the remainder of Fn divided by 10007.

Input format The
input contains an integer n.
Output format
Output one line, containing an integer, representing the remainder of Fn divided by 10007.

Explanation: In this question, the answer is to require the remainder of Fn divided by 10007, so we only need to calculate the remainder, without first calculating the exact value of Fn, and then dividing the calculated result by 10007 to take the remainder and calculate directly The remainder is often simpler than calculating the original number first and then taking the remainder.

Sample input
10
Sample output
55

Sample input
22
Sample output
7704

Data size and convention
1 <= n <= 1,000,000.

public class Main {
    
    
public static void main(String[] args) {
    
    
	int n=5;
	System.out.println(feibo(n+1)%10007);
}
public static int feibo(int n) {
    
    
	if(n==0) {
    
    
		return 0;
	}
	if(n==1) {
    
    
		return 1;
	}
	return feibo(n-1)+feibo(n-2);
}
}

Guess you like

Origin blog.csdn.net/TroyeSivanlp/article/details/108652384