首页 > 其他 > 详细

Another kind of Fibonacci(矩阵)

时间:2015-03-15 00:25:17      阅读:331      评论:0      收藏:0      [点我收藏+]

Another kind of Fibonacci

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


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
 

题意:很明确了。。。不多说。

思路:

技术分享

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3306 

转载请注明出处:寻找&星空の孩子

 

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define LL __int64
#define mod 10007

struct matrix
{
    LL mat[4][4];
};

matrix multiply(matrix a,matrix b)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(a.mat[i][j]==0)continue;
            for(int k=0;k<4;k++)
            {
                if(b.mat[j][k]==0)continue;
                c.mat[i][k]=(c.mat[i][k]+a.mat[i][j]*b.mat[j][k])%mod;
            }
        }
    }
    return c;
}

matrix quickmod(matrix a,LL n)
{
    matrix res;
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            res.mat[i][j]=(i==j);
    while(n)
    {
        if(n&1) res=multiply(res,a);
        n>>=1;
        a=multiply(a,a);
    }
    return res;
}

int main()
{
    LL n,x,y;

    while(scanf("%I64d%I64d%I64d",&n,&x,&y)!=EOF)
    {
        if(n==0||n==1)
        {
            printf("%I64d\n",n+1);
            continue;
        }
        matrix ans;
        ans.mat[0][0]=ans.mat[1][2]=1;
        ans.mat[0][1]=ans.mat[0][2]=ans.mat[0][3]=0;
        ans.mat[2][2]=ans.mat[2][3]=ans.mat[3][2]=0;
        ans.mat[1][0]=ans.mat[1][1]=x*x%mod;
        ans.mat[2][0]=ans.mat[2][1]=y*y%mod;
        ans.mat[3][0]=ans.mat[3][1]=2*x*y%mod;
        ans.mat[1][3]=x%mod;
        ans.mat[3][3]=y%mod;

        ans=quickmod(ans,n-1);
        printf("%I64d\n",(2*ans.mat[0][0]+ans.mat[1][0]+ans.mat[2][0]+ans.mat[3][0])%mod);

    }
    return 0;
}

 

 

加油!少年!

Another kind of Fibonacci(矩阵)

原文:http://www.cnblogs.com/yuyixingkong/p/4338510.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!