Blue Bridge Cup induction training

Link to the original question: http://lx.lanqiao.cn/problem.page?gpid=T4

Started training: Fibonacci series Description

Fibonacci recursion formulas for the number of columns: Fn = Fn-1 + Fn -2, where F1 = F2 = 1. When n is relatively large, Fn is very great, and now we want to know, Fn is divided by the number 10007 is. Input Input Description:
input contains an integer n.
Sample input:
10
output Description:
an output line, comprising an integer, Fn represents the remainder of the division 10007.
Description: In this problem, the answer is to require Fn is divided by 10007, so we can figure this out as long as the remainder can be, without the need to calculate the exact value of Fn, then the result is calculated by dividing the number 10007 to take over direct calculation the remainder often than first calculate the original number and then take the remainder simple.
Output Sample:
55
Time limit: 1.0s
memory limit: 256.0MB
. 1 <= n-<= 1, 000,000.

Key words:

  1. The remainder can be calculated
  2. No need to calculate the exact value of Fn

Problem-solving ideas:

         加法和对数取余的结果与分开取余的结果相同
         即:(a + b) % c == a % c + b % c
         you can try it:
                  (3 + 4) % 2 = 1
                  3 % 2 = 1   plus   4 % 2 = 0  is 1,   so it's right!

Code Display

#include <stdio.h>
int main(void)
{
    int i, index, i_num;
    int F_num1 = 1, F_num2 = 1; 
    scanf("%d", &i_num);//n表示要求的第n项 
    if(i_num == 1 || i_num == 2)
        printf("%d\n", F_num1);
    else if(i_num >= 3)
    {
          for(i = 3; i <= i_num; i ++)
          {
                 index = F_num2;
                 F_num2 = (F_num1 + F_num2) % 10007;//此处是重点哦
                 F_num1 = index;
          }
   
          printf("%d\n", F_num2);
    }
   
   return  0;

If do not understand, welcome to leave a message below

Released two original articles · won praise 1 · views 110

Guess you like

Origin blog.csdn.net/mango660/article/details/104089687