zoukankan      html  css  js  c++  java
  • Another kind of Fibonacce(矩阵快速幂,HDU3306)

    Another kind of Fibonacci

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 2470    Accepted Submission(s): 987


    Problem Description
    As we all known , the Fibonacci series : F(0) = 1, F(1) = 1, F(N) = F(N - 1) + F(N - 2) (N >= 2).Now we define another kind of Fibonacci : A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).And we want to Calculate S(N) , S(N) = A(0)2 +A(1)2+……+A(n)2.

     
    Input
    There are several test cases.
    Each test case will contain three integers , N, X , Y .
    N : 2<= N <= 231 – 1
    X : 2<= X <= 231– 1
    Y : 2<= Y <= 231 – 1
     
    Output
    For each test case , output the answer of S(n).If the answer is too big , divide it by 10007 and give me the reminder.
     
    Sample Input
    2 1 1 3 2 3
     
    Sample Output
    6 196
     
    Author
    wyb
     
    Source
     
    Recommend
    wxl   |   We have carefully selected several similar problems for you:  3308 3307 3309 3314 3310 
     
    注意:用cin>>输入会时间超限,改用scanf即可。

    #include <iostream>
    #include <cstdio>

    using namespace std;

    typedef long long ll;

    #define mod 10007

    ll n;
    ll x;
    ll y;

    struct matrix
    {
     ll v[4][4];
    };

    matrix mul(matrix m1, matrix m2)
    {
     matrix c;
     for (int i = 0; i<4; i++)
      for (int j = 0; j<4; j++)
      {
       c.v[i][j] = 0;
       for (int k = 0; k<4; k++)
        c.v[i][j] += (m1.v[i][k] * m2.v[k][j]) % mod;
       c.v[i][j] %= mod;
      }
     return c;
    }

    matrix qupow(matrix A)
    {
     n--;
     matrix res = A, m = A;
     while (n)
     {
      if (n & 1)
       res = mul(res, m);
      n >>= 1;
      m = mul(m, m);
     }
     return res;
    }

    void solve(matrix A)
    {
     matrix temp = qupow(A);
     ll ans = 0;
     for (int i = 0; i<4; i++)
      ans += temp.v[i][0]%mod;
        ans%=mod;
        printf("%lld ",ans);
     //cout << ans << endl;
    }

    int main()
    {
     matrix A;
     ll a, b, c;
     while (~scanf("%lld%lld%lld",&n,&x,&y))
     {
            a = x*x%mod;
      b = y*y%mod;
      c = x*y * 2%mod;
      //matrix A = {  1,0,0,0 ,1,a,1,x,0,b,0,0 0,c,0,y };
      A.v[0][0]=1;
      for(int i=1;i<4;i++)
                A.v[0][i]=0;
            A.v[1][0]=1;
            A.v[1][1]=a%mod;
            A.v[1][2]=1;
            A.v[1][3]=x%mod;
            A.v[2][0]=0;
            A.v[2][1]=b%mod;
            A.v[2][2]=0;
            A.v[2][3]=0;
            A.v[3][0]=0;
            A.v[3][1]=c%mod;
            A.v[3][2]=0;
            A.v[3][3]=y%mod;
      /*for(int i=0;i<4;i++)
                for(int j=0;j<4;j++)
                cin>>A.v[i][j];*/
      solve(A);
     }
     return 0;
    }

     
     
  • 相关阅读:
    一些常看的网站 工具
    JavaScript 学习
    我的周记15——“5年后,你想成为怎样的人”
    一点分享:从日课到晨记
    跟着高淇学Python——第一到第三章总结
    在新的电脑上的Git本地库 与远程库关联前的一些设置
    搭建环境
    查询XML树
    Linq to XML的基本操作
    LINQ to XML概述
  • 原文地址:https://www.cnblogs.com/onlyli/p/6560291.html
Copyright © 2011-2022 走看看