hdu 5950 Recursive sequence

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;
}

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

时间: 2024-10-07 21:32:39

hdu 5950 Recursive sequence的相关文章

HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 249    Accepted Submission(s): 140 Problem Description Farmer John likes to play mathematics games with his N cows. Recently, t

HDu 5950 Recursive sequence(矩阵快速幂)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1323    Accepted Submission(s): 589 Problem Description Farmer John likes to play mathematics games with his N cows. Recently,

HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 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

HDU 5950 Recursive sequence 矩阵快速幂

http://acm.hdu.edu.cn/showproblem.php?pid=5950 一开始以为i^4不能矩阵快速幂,但是结论是可以得,那么要怎么递推呢? 矩阵快速幂的思路都是一样的,matrix_a * matrix_b ^ n 其中,想要维护什么,就在matrix_a写,比如现在是F[n - 1], F[n - 2],我想要递推到下一项,那么就 会变成F[n], F[n - 1],这个时候,你就要寻找一下F[n]和F[n - 1]有什么关系. i^4也一样,想要从i^4 递推到 (i

5950 Recursive sequence (矩阵快速幂)

题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无法进行运算的.好像有的思路,最后也没想出来,还是参考的大牛的博客 http://blog.csdn.net/spring371327/article/details/52973534 那是讲的很详细了,就不多说了,注意这个取模不是1e9+7,一开始忘了.. 代码如下: #pragma comment

Recursive sequence HDU - 5950

Recursive sequence HDU - 5950 题意:求 f(n) = f(n?1)+2*f(n?2)+n4,其中 f(1)=a,f(2)=b 利用矩阵加速~ 比较坑的是mod=2147493647....不是2147483647,用int WA了多次=_=|| 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const ll mod=2147493647; 5 const int

HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)

题解见X姐的论文 矩阵乘法递推的优化,只是mark一下.. HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

hdu 4441 Queue Sequence(splay)

题目链接:hdu 4441 Queue Sequence 这题看了题解写的,题解传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 #define ls l,m,rt<<1 4 #define rs m+1,r,rt<<1|1 5 using namespace std; 6 typedef long long ll; 7 8 const int N=1e6+7; 9 i