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 <= 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
 

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]);
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章