hdu 3306 Another kind of Fibonacci

Another kind of Fibonacci

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

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 Out

6 196
 
Meaning of the questions: multiple sets of data. Every group of data N, X, Y, A ( 0) = 1, A (1) = 1, A (N) = X * A (N-1) + Y * A (N-2) (N> = 2), find S (N) = A (0) 2 + A (. 1) 2 + ... + A (n-) 2  
 
Solution: using A (N) = X-* A (N-. 1) + the Y * A (N-2), S (N) = S (N-. 1) + A (N) 2 , A (N) 2 = X- 2 * A (. 1-N) 2 + 2XY A * (. 1-N) A (N-2) the Y + 2 * A (N-2) 2 configuration matrix.
 
      
 
 
 
  
 
 
Code:
#include<bits/stdc++.h>
using namespace std;
const int mod=10007;
struct node
{
  long long Martix[4][4];
  node operator *(const node&n) const
  {
    int i,j,k;node sum;
    for(i=0;i<4;i++)
     for(j=0;j<4;j++)
      {
        sum.Martix[i][j]=0;
        for(k=0;k<4;k++) 
          sum.Martix[i][j]=(sum.Martix[i][j]+Martix[i][k]*n.Martix[k][j])%mod;
      }
    return sum;
  }
};
int main()
{
  long long n,x,y;
  node B,ans;
  while(~scanf("%lld%lld%lld",&n,&x,&y))
  {
    fill(B.Martix[0],B.Martix[0]+16,0);
    fill(ans.Martix[0],ans.Martix[0]+16,0);
    for(int i=0;i<4;i++) ans.Martix[i][i]=1;
    B.Martix[0][0]=B.Martix[1][0]=B.Martix[1][2]=1;
    B.Martix[1][1]=(x*x)%mod;
    B.Martix[2][1]=(y*y)%mod;
    B.Martix[3][1]=(2*x*y)%mod;
    B.Martix[1][3]=x%mod;
    B.Martix[3][3]=y%mod;
    while(n)
    {
       if(n&1) ans=ans*B;
       n>>=1;
       B=B*B;
    }
    printf("%lld\n",(ans.Martix[0][0]+ans.Martix[1][0]+ans.Martix[2][0]+ans.Martix[3][0])%mod);
  }
  system("pause");
  return 0;
}

Guess you like

Origin www.cnblogs.com/VividBinGo/p/11323175.html