Waterloo Cup: Fibonacci series "Java algorithm implementation" __c language

Source: Internet
Author: User
Topic description

The recursive formula for the Fibonacci sequence is: Fn=fn-1+fn-2, which is f1=f2=1.

When n is larger, the FN is also very large, and now we want to know how much of the remainder of FN divided by 10007.

Input Format

The input contains an integer n.

output Format

Output line that contains an integer that represents the remainder of the FN divided by 10007.

Description: In this question, the answer is to require FN divided by 10007 of the remainder, so as long as we can work out the remainder can be, and do not need to calculate the exact value of FN, and then divide the calculated result by 10007 to the remainder, the direct calculation of the remainder is often more simple than the first to calculate the

Sample Input

10
Sample Output

Sample Example Input

Sample Output

7704 Data size and Convention

1 <= n <= 1,000,000.

Import Java.util.Scanner;

public class Main {public

	static void Main (string[] args) {
		Scanner cin = new Scanner (system.in);
		int a = 1, b = 1, ans = 1,c=cin.nextint ();
		for (int i = 1; I <=  c-2 i++) {
			ans = (a + b)% 10007;
			A = b;
			b = ans;
		}
		System.out.println (ANS);
	}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.