HDU5950Recursive sequence(矩阵快速幂)

来源:互联网 发布:淘宝网韩都衣舍 编辑:程序博客网 时间:2024/05/21 12:08

Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 342 Accepted Submission(s): 191

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 i4. 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

2
3 1 2
4 1 10

Sample Output

85
369
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.
题意:f[1]=a,f[2]=b,f[i]=f[i-1]+2*f[i-2]+i^4(I>=3),求f[n]%mod
题解:一眼就可以看出来是矩阵快速幂吧,这里不好用矩阵表示的就是i^4,但是如果队里数论好点的话,其实也很好得出来。
(n+1)^4 = n^4 + 4n^3 + 6n^2 + 4n + 1
则构造两个矩阵就可以了
这里写图片描述
最后套一个裸的矩阵快速幂。
代码:

#include<stdio.h>#include<string.h>#include <iostream>#define ll long long#define bababaa printf("!!!!!\n")using namespace std;ll mod=2147493647;int aa,bb,n;struct Matrix{    ll m[7][7];};Matrix Mul(Matrix a,Matrix b){    Matrix c;    memset(c.m,0,sizeof(c.m));    for(int i=0; i<7; i++)        for(int j=0; j<7; j++)            for(int k=0; k<7; k++)            {                c.m[i][j] =c.m[i][j]+((a.m[i][k]%mod)*(b.m[k][j]%mod)%mod);            }    return c;}Matrix fastm(Matrix a,int n){    Matrix res;    memset(res.m,0,sizeof(res.m));    res.m[0][0]=bb,res.m[1][0]=aa,res.m[2][0]=16,res.m[3][0]=8,res.m[4][0]=4,res.m[5][0]=2,res.m[6][0]=1;    while(n)    {        if(n&1)            res = Mul(a,res);        n>>=1;        a = Mul(a,a);    }    return res;}Matrix init(){    Matrix pp;    memset(pp.m,0,sizeof(pp.m));    pp.m[0][0] = 1, pp.m[0][1] = 2, pp.m[0][2] = 1, pp.m[0][3] = 4, pp.m[0][4] = 6, pp.m[0][5] = 4, pp.m[0][6] = 1;    pp.m[1][0] = 1, pp.m[1][1] = 0, pp.m[1][2] = 0, pp.m[1][3] = 0, pp.m[1][4] = 0, pp.m[1][5] = 0, pp.m[1][6] = 0;    pp.m[2][0] = 0, pp.m[2][1] = 0, pp.m[2][2] = 1, pp.m[2][3] = 4, pp.m[2][4] = 6, pp.m[2][5] = 4, pp.m[2][6] = 1;    pp.m[3][0] = 0, pp.m[3][1] = 0, pp.m[3][2] = 0, pp.m[3][3] = 1, pp.m[3][4] = 3, pp.m[3][5] = 3, pp.m[3][6] = 1;    pp.m[4][0] = 0, pp.m[4][1] = 0, pp.m[4][2] = 0, pp.m[4][3] = 0, pp.m[4][4] = 1, pp.m[4][5] = 2, pp.m[4][6] = 1;    pp.m[5][0] = 0, pp.m[5][1] = 0, pp.m[5][2] = 0, pp.m[5][3] = 0, pp.m[5][4] = 0, pp.m[5][5] = 1, pp.m[5][6] = 1;    pp.m[6][0] = 0, pp.m[6][1] = 0, pp.m[6][2] = 0, pp.m[6][3] = 0, pp.m[6][4] = 0, pp.m[6][5] = 0, pp.m[6][6] = 1;    return pp;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&aa,&bb);        if(n==1)            printf("%d\n",aa%mod);        else if(n==2)            printf("%d\n",bb%mod);        else        {            Matrix p;            p=fastm(init(),n-2);            printf("%d\n",p.m[0][0]%mod);        }    }}
0 0
原创粉丝点击