HDU 3306解题报告

Another kind of Fibonacci

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


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.
 

Sample Input
  
  
2 1 1 3 2 3
 

Sample Output
  
  
6 196
 

Author
wyb
 

Source
 

Recommend
wxl   |   We have carefully selected several similar problems for you:   3308  3307  3309  3314  3310 

        这道题是比较水的矩阵快速幂题目。可以从A(n)上构造矩阵,然后利用矩阵的等比之和求出结果,如果从A(n)上构造矩阵为3阶矩阵。而如果从S(n)上构造矩阵则为4阶矩阵。这样速度要更快一些。S(n+1)=S(n)+x^2*(A(n))^2+2*x*y*A(n)A(n-1)+y^2*(A(n-1))^2。而A(n+1)A(n)=x*(A(n))^2+y*A(n)A(n-1)。直接构造即可。

       参考代码

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;

const int mod=10007;

struct mat
{
    ll d[4][4];
}A,B,E;
ll n,x,y;

mat multi(mat a,mat b)
{
    mat ans;
    rep(i,0,4)
    {
        rep(j,0,4)
        {
            ans.d[i][j]=0;
            rep(k,0,4)
            {
                if(a.d[i][k]&&b.d[k][j])
                    ans.d[i][j]+=a.d[i][k]*b.d[k][j];
            }
            ans.d[i][j]%=mod;
        }
    }
    return ans;
}

mat quickmulti(mat a,int n)
{
    mat ans=E;
    while(n)
    {
        if(n&1)
        {
            n--;
            ans=multi(ans,a);
        }
        else
        {
            n>>=1;
            a=multi(a,a);
        }
    }
    return ans;
}

int main()
{
   CLR(E.d);CLR(B.d);
   rep(i,0,4)
      E.d[i][i]=1;
   while(~scanf("%d%d%d",&n,&x,&y))
   {
       A.d[0][0]=1,A.d[0][1]=(x*(x%mod))%mod,A.d[0][2]=(2*((x*(y%mod))%mod))%mod,A.d[0][3]=(y*(y%mod))%mod;
       A.d[1][0]=0,A.d[1][1]=A.d[0][1],A.d[1][2]=A.d[0][2],A.d[1][3]=A.d[0][3];
       A.d[2][0]=0,A.d[2][1]=x%mod,A.d[2][2]=y%mod,A.d[2][3]=0;
       A.d[3][0]=0,A.d[3][1]=1,A.d[3][2]=A.d[3][3]=0;
       B.d[0][0]=2,B.d[1][0]=B.d[2][0]=B.d[3][0]=1;
       mat ans=quickmulti(A,n-1);
       ans=multi(ans,B);
       printf("%I64d\n",ans.d[0][0]);
   }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值