• 矩阵连乘(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;
    }
  • 相关阅读:
    用ssh从ubuntu系统向ubuntu系统服务器发送文件
    python import caffe失败的可能原因
    segnet caffe upsample top index 0 out of range
    出了问题检查下你的caffe 搭建步骤
    由于不能随便改路径,所以写在这里
    关于DatePicker设置MinDate和MaxDate的几个坑
    Socket I/O multiplexing
    STL_迭代器
    基本套接字
    STL_空间配置器(allocators)
  • 原文地址:https://www.cnblogs.com/chen9510/p/4735370.html
Copyright © 2020-2023  润新知