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 <= 2^31 – 1
X : 2<= X <= 2^31– 1
Y : 2<= Y <= 2^31 – 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.

 

输入样例

2 1 1 
3 2 3 

输出样例

6
196

本题关键是写系数矩阵,将A(N)替换后与A(N-1)*A(N-2)挂钩
附ac代码
#include<bits/stdc++.h>
using namespace std;
const int N=4,mod=10007;
typedef unsigned long long ull;
ull x,y;
struct matrix{
    ull ma[N][N];
};
matrix muti(matrix a,matrix b)
{
    matrix c;
    for(int i=0;i<N;++i)
      for(int j=0;j<N;++j)
      c.ma[i][j]=0;
    for(int i=0;i<N;++i)
      for(int j=0;j<N;++j)
       for(int z=0;z<N;++z)
    {
        c.ma[i][j]+=(a.ma[i][z]%mod*(b.ma[z][j]%mod))%mod;
        c.ma[i][j]%=mod;
    }
    return c;
}
matrix pow_ma(matrix a,int k)
{
    if(k==1) return a;
    matrix s;
    s=pow_ma(muti(a,a),k/2);
    if(k%2) s=muti(s,a);
    return s;
}
int main()
{
    int n;
    int t[N]={2,1,1,1};
    while(scanf("%d%d%d",&n,&x,&y)==3)
    {
        //unit每次输入都要重新定义,不能定义成全局
        ull unit[N][N]={{1,x*x,2*x*y,y*y},{0,x*x,2*x*y,y*y},{0,x,y,0},{0,1,0,0}};
        //x*x直接在数组内就爆了int需要改变其数据类型
        matrix ans;
        for(int i=0;i<N;++i)
          for(int j=0;j<N;++j)
            ans.ma[i][j]=unit[i][j];
        ans=pow_ma(ans,n-1);
        ull sum=0;
        for(int i=0;i<N;++i)
        {sum+=(ans.ma[0][i]%mod*(t[i]%mod))%mod;
        sum%=mod;
        }
        printf("%llu\n",sum%mod);
    }
}

 

 posted on 2023-01-15 16:28  ruoye123456  阅读(32)  评论(0)    收藏  举报