hdu3306--Another kind of Fibonacci(矩阵快速幂)

Another kind of Fibonacci
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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.
 

Sample Input

     
     
2 1 1 3 2 3
 

Sample Output

     
     
6 196

Sn = S(n-1) + An^2 =  S(n-1) + X^2A*(n-1)^2 + 2*X*Y*A(n-1)*A(n-2) + Y^2*A(n-2)^2

也就得到Sn由S(n-1) , A(n-1) , A(n-2) , A(n-1)*A(n-2)得到,由此可以转化为矩阵相乘




#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
#define MOD 10007
struct node
{
    LL a[5][5] ;
    int n ;
};
node mul(node p,node q)
{
    int i , j , k ;
    node s ;
    for(i = 0 , s.n = p.n ; i < p.n ; i++)
        for(j = 0 ; j < p.n ; j++)
        {
            s.a[i][j] = 0 ;
            for(k = 0 ; k < p.n ; k++)
                s.a[i][j] = ( s.a[i][j] + p.a[i][k] * q.a[k][j] ) % MOD ;
        }
    return s ;
}
node pow(node p,int k)
{
    if( k == 1 )
        return p ;
    node s = pow( p,k/2 ) ;
    s = mul(s,s) ;
    if( k%2 )
        s = mul(s,p) ;
    return s ;
}
int main()
{
    LL n , x , y , i , j , ans ;
    node p , s ;
    while( scanf("%I64d %I64d %I64d", &n, &x, &y) != EOF )
    {
        p.n = 4 ;
        memset(p.a,0,sizeof(p.a)) ;
        p.a[0][0] = 1 ;
        p.a[1][0] = p.a[1][1] = (x*x) % MOD ;
        p.a[2][0] = p.a[2][1] = (2*x*y) % MOD ;
        p.a[3][0] = p.a[3][1] = (y*y) % MOD ;
        p.a[1][2] = x % MOD ;
        p.a[2][2] = y % MOD ;
        p.a[1][3] = 1 ;
        s = pow(p,n-1) ;
        ans = s.a[0][0] * 2 ;
        for(i = 1 ; i < 4 ; i++)
            ans = ( ans + s.a[i][0] ) % MOD ;
        printf("%I64d\n", ans) ;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值