Recursive sequence

来源:互联网 发布:手链饰品店淘宝 编辑:程序博客网 时间:2024/05/22 04:36

Recursive sequence

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


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, andi4. 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 < 231 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
23 1 24 1 10
 

Sample Output
85369
Hint
In the first case, the third number is 85 = 2*1十2十3^4. In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.
 

Source
2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
 

Recommend
jiangzijing2015   |   We have carefully selected several similar problems for you:  6216 6215 6214 6213 6212 
 


公式为:a[i+1]=2a[i-1]+a[i]+(i+1)^4,典型的矩阵快速幂,首先将(i+1)^4 展出来  i^4+4*n^3+6*n^2+4*n+1;

即:a[i+1]=2a[i-1]+a[i]+ i^4+4*n^3+6*n^2+4*n+1;然后矩阵快速幂







代码如下:
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;typedef long long ll;const ll mod=2147493647;struct Matrix{    ll matrix[7][7];};int n;//阶数void init(Matrix &res){    memset(res.matrix,0,sizeof(res.matrix));    for(int i=0;i<n;i++)        res.matrix[i][i]=1;}Matrix multiplicative(Matrix a,Matrix b){    Matrix res;    memset(res.matrix,0,sizeof(res.matrix));    for(int i = 0 ; i < n ; i++){        for(int j = 0 ; j < n ; j++){            for(int k = 0 ; k < n ; k++){                res.matrix[i][j] = (res.matrix[i][j]+(a.matrix[i][k]%mod)*(b.matrix[k][j]%mod))%mod;            }        }    }    return res;}Matrix pow(Matrix mx,int m){    Matrix res,base=mx;    init(res);    while(m)    {        if(m&1)            res=multiplicative(res,base);        base=multiplicative(base,base);        m>>=1;    }    return res;}int main(){    ll t,m,a,b;    n=7;    Matrix mx={            0,1,0,0,0,0,0,            2,1,1,4,6,4,1,            0,0,1,4,6,4,1,            0,0,0,1,3,3,1,            0,0,0,0,1,2,1,            0,0,0,0,0,1,1,            0,0,0,0,0,0,1};    scanf("%d",&t);    while(t--)    {        scanf("%lld%lld%lld",&m,&a,&b);        if(m==1)            printf("%lld\n",a);        else if(m==2)            printf("%lld\n",b);        else        {            Matrix res=pow(mx,m-2);            ll ans=0;            ans=(ans+a*res.matrix[1][0])%mod;            ans=(ans+b*res.matrix[1][1])%mod;            ans=(ans+16*res.matrix[1][2])%mod;            ans=(ans+8*res.matrix[1][3])%mod;            ans=(ans+4*res.matrix[1][4])%mod;            ans=(ans+2*res.matrix[1][5])%mod;            ans=(ans+res.matrix[1][6])%mod;            printf("%lld\n",ans);        }    }    return 0;}