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

    http://poj.org/problem?id=3070


    题意:

    已知  

    Fibonacci 数列 可以这样求 ,求 fn 的最后四位 

    .
     题解:

      矩阵的快速幂

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<vector>
    #include<string>
    #define Min(a,b) a<b?a:b
    #define Max(a,b) a>b?a:b
    #define CL(a,num) memset(a,num,sizeof(a));
    #define maxn  40
    #define eps  1e-6
    #define inf 9999999
    #define mx 1<<60
    #define mod 10000
    using namespace std;
    struct martrix
    {
        int m[3][3];
    };
    martrix mtmul(martrix a,martrix b)
    {
        martrix c;
        int i,j,k;
        for(i = 0; i < 2; i++)
        {
            for(j = 0; j < 2;j++)
            {
                c.m[i][j] = 0;
                for(k = 0 ; k < 2;k++)
                {
                    c.m[i][j] += a.m[i][k] * b.m[k][j];
                    c.m[i][j] %=mod;
                }
            }
        }


        return c;
    }
    martrix mtpow(martrix d,int k)
    {   martrix a;
        if(k == 1return d;
        int mid = k / 2;
        a = mtpow(d,k/2);
        a = mtmul(a,a);
        if(k & 1)
        {
            a = mtmul(a,d);
        }
        return a;


    }
    int main()
    {
        __int64 n;
        martrix a,b;
        a.m[0][0] = 1;
        a.m[0][1] = 1;
        a.m[1][0] = 1;
        a.m[1][1] = 0 ;
        while(scanf("%I64d",&n),n >= 0)
        {
            if(n == 0)printf("0\n");
            else{

              b = mtpow(a,n);
              printf("%d\n",b.m[1][0] % mod);

            }
        }
    }
  • 相关阅读:
    poj 3422 Kaka's Matrix Travels
    poj 1815 Friendship
    poj 1966 Cable TV Network
    黑暗
    【bzoj2741】[FOTILE模拟赛] L
    整数拆分
    LCIS
    原题的旅行
    【codeforces gym】Increasing Costs
    【noip模拟】D(==)
  • 原文地址:https://www.cnblogs.com/acSzz/p/2647194.html
Copyright © 2011-2022 走看看