zoukankan      html  css  js  c++  java
  • 【蓝桥杯】入门训练 Fibonacci数列

      入门训练 Fibonacci数列  
    时间限制:1.0s   内存限制:256.0MB
          
    问题描述

    Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1。

    当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少。

    输入格式
    输入包含一个整数n。
    输出格式
    输出一行,包含一个整数,表示Fn除以10007的余数。

    说明:在本题中,答案是要求Fn除以10007的余数,因此我们只要能算出这个余数即可,而不需要先计算出Fn的准确值,再将计算的结果除以10007取余数,直接计算余数往往比先算出原数再取余简单。

    样例输入
    10
    样例输出
    55
    样例输入
    22
    样例输出
    7704
    数据规模与约定
    1 <= n <= 1,000,000。
    用了两种方法,普通的
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 const int maxn=1000000+10;
     6 int f[maxn];
     7 int main()
     8 {
     9     int n;
    10     scanf("%d",&n);
    11     f[1]=1;
    12     f[2]=1;
    13     for(int i=2;i<=n;i++)
    14         f[i]=(f[i-1]+f[i-2])%10007;
    15     printf("%d
    ",f[n]);
    16 
    17 }

    用了矩阵快速幂的

     1 /*
     2 ID: sdj22251
     3 PROG: subset
     4 LANG: C++
     5 */
     6 #include <iostream>
     7 #include <vector>
     8 #include <list>
     9 #include <map>
    10 #include <set>
    11 #include <deque>
    12 #include <queue>
    13 #include <stack>
    14 #include <bitset>
    15 #include <algorithm>
    16 #include <functional>
    17 #include <numeric>
    18 #include <utility>
    19 #include <sstream>
    20 #include <iomanip>
    21 #include <cstdio>
    22 #include <cmath>
    23 #include <cstdlib>
    24 #include <cctype>
    25 #include <string>
    26 #include <cstring>
    27 #include <cmath>
    28 #include <ctime>
    29 #define MAXN 305
    30 #define INF 100000000
    31 #define eps 1e-7
    32 #define PI 3.1415926535898
    33 using namespace std;
    34 int n = 2, m;
    35 int tt[2][2]={{1, 1}, {1, 0}};
    36 struct wwj
    37 {
    38     int r, c;
    39     int mat[3][3];
    40 } need, ready;
    41 void init()
    42 {
    43     memset(need.mat, 0, sizeof(need.mat));
    44     need.r = n;
    45     need.c = n;
    46     for(int i = 1; i <= n; i++)
    47     {
    48         need.mat[i][i] = 1;
    49     }
    50     ready.c = n;
    51     ready.r = n;
    52     for(int i = 1; i <= n; i++)
    53     {
    54         for(int j = 1; j <= n; j++)
    55         ready.mat[i][j] = tt[i - 1][j - 1];
    56     }
    57 }
    58 wwj multi(wwj x, wwj y)
    59 {
    60     wwj t;
    61     int i, j, k;
    62     memset(t.mat, 0, sizeof(t.mat));
    63     t.r = x.r;
    64     t.c = y.c;
    65     for(i = 1; i <= x.r; i++)
    66     {
    67         for(k = 1; k <= x.c; k++)
    68             if(x.mat[i][k])
    69             {
    70                 for(j = 1; j <= y.c; j++)
    71                 {
    72                     t.mat[i][j] += (x.mat[i][k] * y.mat[k][j]) % 10007;
    73                     t.mat[i][j] %= 10007;
    74                 }
    75             }
    76     }
    77     return t;
    78 }
    79 int main()
    80 {
    81     int m;
    82     scanf("%d", &m);
    83         init();
    84         while(m)
    85         {
    86             if(m & 1)
    87             {
    88                 need = multi(ready, need);
    89             }
    90             ready = multi(ready, ready);
    91             m = m >> 1;
    92         }
    93         printf("%d
    ", need.mat[1][2] % 10007);
    94     return 0;
    95 }

    郁闷的是评测的结果是这样的:

  • 相关阅读:
    完美世界笔试(动态规划,背包问题)
    腾讯笔试3
    腾讯笔试2
    腾讯笔试1
    阿里笔试1
    Merge Sorted Array
    Partition List
    Reverse Linked List II
    Remove Duplicates from Sorted List II
    白菜刷LeetCode记-704. Binary Search
  • 原文地址:https://www.cnblogs.com/wpnan/p/4095655.html
Copyright © 2011-2022 走看看