Blue Bridge Cup algorithm training Java algorithm Fibonacci sequence

Source: Internet
Author: User

Problem Description

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

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

Input FormatThe input contains an integer n. output FormatThe output line contains an integer that represents the remainder of FN divided by 10007.

Note: In the subject, the answer is to require FN divided by 10007 of the remainder, so as long as we can calculate the remainder can be, and do not need to calculate the exact value of FN, and then divide the calculated results by 10007 to take the remainder, the direct calculation of the remainder is often more than the original number of the first and then take more simple.

Sample InputTenSample Output -Sample Input ASample Output7704data size and conventions1 <= n <= 1,000,000. Note: It seems to be a relatively easy problem, with recursion will come out the answer, but in the system to test, there will be a timeout, only 30 points, the following code for non-recursive processing
1 ImportJava.util.Scanner;2  Public class_1fibonacci Series {3     //The request for this problem is not recursive and will time out.4 //Public static void Main (string[] args) {5 //Scanner Scanner = new Scanner (system.in);6 //int m = Scanner.nextint ();7 //int sum = f (m);8 //System.out.println (sum%10007);9 //   }Ten //public static int f (int n) { One //if (n==1| | n==2) { A //return 1; - //}else - //return F (n-1) +f (n-2); the //   } -      -      -      +      Public Static voidMain (string[] args) { -Scanner Scanner =NewScanner (system.in); +         intn =scanner.nextint (); A         intA =1, b=1; at         intsum = 0, temp; -          for(inti = 1; I <= N; i++) { -sum = a%10007; -temp =b; -b = (a+b)%10007; -A =temp; in         } - System.out.println (sum); to     } +}

But another question, do not know why, do not know the following why there are two test points is wrong, only 80 points

1 ImportJava.util.Scanner;2  Public class_1fibonacci Series {3      Public Static voidMain (string[] args) {4Scanner Scanner =NewScanner (system.in);5         intn =scanner.nextint ();6         intA =1, b=1;7         intsum = 0, temp;8          for(inti = 1; I <= n-2; i++) {9sum = (a+b)%10007;//Note hereTenA =b; Oneb =sum; A          -         } - System.out.println (sum); the     } - } -     

Blue Bridge Cup algorithm training Java algorithm Fibonacci sequence

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.