码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5950 Recursive sequence

时间:2019-08-08 19:12:36      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:pos   drag   otto   time   entity   form   this   nbsp   ati   

Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5120    Accepted Submission(s): 2197

Problem Description

Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i^4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right. 
 

Input

The first line of input contains an integer t, the number of test cases. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b <2^31 as described above.
 

Output

For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.
 

Sample Input

2 3 1 2 4 1 10
 

Sample Output

85 369
 
题意:T组数据。每组数据有N,F[1]=a,F[2]=b,F[N]=2*F[N-1]+F[N-2]+n^4,求F[N]。
 
题解:矩阵构造。先把技术图片处理一下,技术图片,完成了由技术图片技术图片的转换。利用F[1],F[2],技术图片技术图片技术图片技术图片技术图片构造矩阵。
技术图片

 

 
 

 

 

 

 

 技术图片

 

 技术图片

 

 

 

 

 

 

 技术图片

 

代码:

#include<bits/stdc++.h>
using namespace std;
const long long mod=2147493647;
struct node
{
  long long Martix[7][7];
  node operator *(const node&n) const
  {
    int i,j,k;node sum;
    for(i=0;i<7;i++)
     for(j=0;j<7;j++)
      {
        sum.Martix[i][j]=0;
        for(k=0;k<7;k++) 
          sum.Martix[i][j]=(sum.Martix[i][j]+Martix[i][k]*n.Martix[k][j])%mod;
      }
    return sum;
  }
};
int main()
{
  long long T,n,a,b;
  node A,B,ans;
  scanf("%ld",&T);
  while(T--)
  {
    scanf("%lld%lld%lld",&n,&a,&b);
    fill(A.Martix[0],A.Martix[0]+49,0);
    fill(B.Martix[0],B.Martix[0]+49,0);
    fill(ans.Martix[0],ans.Martix[0]+49,0);
    for(int i=0;i<7;i++) A.Martix[i][i]=B.Martix[i][i]=1;
    
    ans.Martix[0][0]=2*a%mod;ans.Martix[0][1]=b%mod;
    ans.Martix[0][2]=81;ans.Martix[0][3]=27;
    ans.Martix[0][4]=9;ans.Martix[0][5]=3;ans.Martix[0][6]=1;
    
    B.Martix[0][0]=0;B.Martix[0][1]=1;B.Martix[1][0]=B.Martix[5][4]=2;
    B.Martix[2][1]=B.Martix[6][2]=B.Martix[6][3]=B.Martix[6][4]=B.Martix[6][5]=1;
    B.Martix[3][2]=B.Martix[5][2]=4;
    B.Martix[4][2]=6;
    B.Martix[4][3]=B.Martix[5][3]=3;
  
    n-=2;
    while(n)
    {
       if(n&1) A=A*B;
       n>>=1;
       B=B*B;
    }
    ans=ans*A;
    printf("%lld\n",ans.Martix[0][1]%mod);
  }
  system("pause");
  return 0;
}

hdu 5950 Recursive sequence

标签:pos   drag   otto   time   entity   form   this   nbsp   ati   

原文地址:https://www.cnblogs.com/VividBinGo/p/11322920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!