Blue Bridge Cup-introductory training Fibonacci series

Source: Internet
Author: User

Introductory training Fibonacci Sequence time limit: 1.0s memory limit: 256.0MBProblem description

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

When n is large, fn is also very large, and now we want to know what theremainder of f n divided by 10007 is.

Input format input contains an integer n. The output format outputs a line that contains an integer representing the F The remainder of n divided by 10007.

Note: In the subject, the answer is to ask Fn divided by 10007 of the remainder, so as long as we can calculate the remainder can be, and do not need to calculate theexact value of f N, and then divide the result of the calculation by 10007 to take the remainder, the direct calculation of the remainder is often more simple than the original number of the first.

Sample input 10 Sample Output 55 Sample input 22 sample output 7704 data scale and Convention 1 <= N <= 1,000,000.
#include <iostream>#include<cstdio>using namespacestd;intMain () {intf1,f2,n,f;  while(~SCANF ("%d",&N)) {intm=10007; if(n==1|| n==2) {printf ("1\n"); Continue; } N=n-2; F1=f2=1;  while(n--) {f= (F1+F2)%m; F1=f2%m; F2=f%m; } printf ("%d\n", f%m); }    return 0;}

Blue Bridge Cup-introductory training Fibonacci series

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.