zoukankan      html  css  js  c++  java
  • Fibonacci(矩阵快速幂)

    Fibonacci
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 24331   Accepted: 16339

    Description

    In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

    An alternative formula for the Fibonacci sequence is

    .

    Given an integer n, your goal is to compute the last 4 digits of Fn.

    Input

    The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.

    Output

    For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

    Sample Input

    0
    9
    999999999
    1000000000
    -1

    Sample Output

    0
    34
    626
    6875

    解题思路:因为n很大所以不能用递推来算;

    a[2]  a[1]  a[0]   ------> a[3]    a[2]     a[1]

      1       1     0       2         1         1               

    构造一个矩阵 1行3列的矩阵*(莫一个矩阵)=1行3列的矩阵  ----->   矩阵大小为3行3列

    递推式=  a[n]=a[n-1]]+a[n-2];                      a[n+1]=a[n]+a[n-1];

    后一个由第一个怎么变换到达  -----> 构造的矩阵为

    1 1 0

    1 0 1

    0 0 0

     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define ll long long
     5 #define mod(x) ((x)%MOD)
     6 using namespace std;
     7 
     8 ll n;
     9 const int maxn=3;
    10 const int MOD=10000;
    11 
    12 struct mat{
    13     int m[maxn][maxn];
    14 }unit;
    15 
    16 mat operator *(mat a,mat b){
    17     mat ret;
    18     ll x;
    19     for(int i=0;i<maxn;i++)
    20     for(int j=0;j<maxn;j++){
    21         x=0;
    22         for(int k=0;k<maxn;k++){
    23             x+=mod(1LL*a.m[i][k]*b.m[k][j]);
    24         }
    25         ret.m[i][j]=mod(x);
    26     }
    27     return ret;
    28 }
    29 
    30 void init(){
    31     for(int i=0;i<maxn;i++)
    32         unit.m[i][i]=1;
    33     return;
    34 }
    35 
    36 mat pow_mat(mat a,ll m){
    37     mat ret=unit;
    38     while(m){
    39         if(m&1) ret=ret*a;
    40         a=a*a;
    41         m=m/2;
    42     }
    43     return ret;
    44 }
    45 
    46 int main(){
    47     ios::sync_with_stdio(false);
    48     init();
    49     while(cin>>n&&n!=-1){
    50         if(n==0) cout << 0 << endl;
    51         else if(n==1) cout << 1 << endl;
    52         else if(n==2) cout << 1 << endl;
    53         else{
    54             mat a,b;
    55             a.m[0][0]=1,a.m[0][1]=1,a.m[0][2]=0;
    56 
    57             b.m[0][0]=1,b.m[0][1]=1,b.m[0][2]=0;
    58             b.m[1][0]=1,b.m[1][1]=0,b.m[1][2]=1;
    59             b.m[2][0]=0,b.m[2][1]=0,b.m[2][2]=0;
    60 
    61             b=pow_mat(b,n-2);
    62             a=a*b;
    63             cout << mod(a.m[0][0]) << endl;
    64         }
    65     }
    66     return 0;
    67 }
    矩阵快速幂

     

     

     

     

     

  • 相关阅读:
    六.初识Mybatis
    python中文资源大全
    阅读《乌云回忆录一》后的一点感慨
    SSH无法连上CentOS7的问题
    [转]sqlmap使用教程
    [转]11种常见sqlmap使用方法详解
    ZVulDrill渗透环境搭建及部分题目writeup
    渗透资源大全-整理
    【洛谷5934】[清华集训2012] 最小生成树(最小割)
    【洛谷3974】[TJOI2015] 组合数学(模拟最大流)
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11749608.html
Copyright © 2011-2022 走看看