zoukankan      html  css  js  c++  java
  • 矩阵连乘(3)

    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
     
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    const long long mod=10007;
    
    typedef struct
    {
      long long m[4][4];
    }mat;
    
    mat I={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
    
    mat calc(mat a,mat b)
    {
        int i,j,k;
        mat c;
        for(i=0;i<4;i++)
        for(j=0;j<4;j++)
        {
            c.m[i][j]=0;
           for(k=0;k<4;k++)
          {
            c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
          }
          c.m[i][j]=c.m[i][j]%mod;
        }
        return c;
    }
    
    mat matirx(mat P,long long n)
    {
        mat m=P,b=I;
        while(n>=1)
        {
            if(n&1) b=calc(b,m);
            n>>=1;
            m=calc(m,m);
        }
        return b;
    }
    
    int main()
    {
        long long n,x,y;
        while(scanf("%lld%lld%lld",&n,&x,&y)!=EOF)
        {
            long long sum=0;
            x=x%mod;
            y=y%mod;   //2*x*y可能会溢出
            mat P={x*x,2*x*y,y*y,0,x,y,0,0,1,0,0,0,1,0,0,1};
            mat a;
            a=matirx(P,n);
            sum=(a.m[3][0]+a.m[3][1]+a.m[3][2]+a.m[3][3])%mod;
            printf("%lld
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    LSMW TIPS
    Schedule agreement and Delfor
    Running VL10 in the background 13 Oct
    analyse idoc by creation date
    New Journey Prepare
    EDI error
    CBSN NEWS
    Listen and Write 18th Feb 2019
    Microsoft iSCSI Software Target 快照管理
    通过 Microsoft iSCSI Software Target 提供存储服务
  • 原文地址:https://www.cnblogs.com/chen9510/p/4735370.html
Copyright © 2011-2022 走看看