HDU5950 Recursive sequence(矩阵快速幂)

来源:互联网 发布:linux的家目录 编辑:程序博客网 时间:2024/06/05 08:33

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

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.

思路

先说题意,有一个递推式:

f[n]=f[n1]+2f[n2]+n4

求第n项的值
很明显的一道矩阵快速幂的题,难点就是如何构造初始矩阵。

因为后面加的是n4,经过一次变换以后,会变成(n+1)4,所以我们要先拆分一下:

(n+1)4=n4+4n3+62+4n+1(n+1)3=n3+3n2+3n+1(n+1)2=n2+2n+1(n+1)1=n+1

由此我们可以构造出转置矩阵:

1,1,0,0,0,0,0,2,0,0,0,0,0,0,1,0,1,0,0,0,0,4,0,4,1,0,0,0,6,0,6,3,1,0,0,4,0,4,3,2,1,0,1,0,1,1,1,1,1

我们还需要一个原始矩阵,那么就是:

f2 f1 16 8 4 2 1

那么要求f(n)就把转置矩阵乘以n2次,然后再乘以原始矩阵就好。

代码

#include <cstdio>#include <cstring>#include <string>#include <set>#include <cmath>#include <iostream>#include <stack>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 2147493647#define debug() puts("what the fuck!!!")#define ll long longusing namespace std;const ll N=7;struct Matrix{    ll a[N][N];    Matrix()    {        mem(a,0);    }    void init()    {        mem(a,0);        for(ll i=0; i<N; i++)            a[i][i]=1;    }};Matrix mul(Matrix a,Matrix b){    Matrix ans;    for(ll i=0; i<N; i++)        for(ll j=0; j<N; j++)            for(ll k=0; k<N; k++)            {                ans.a[i][j]=(ans.a[i][j]%mod+a.a[i][k]*b.a[k][j]%mod)%mod;            }    return ans;}Matrix mat_pow(Matrix a,ll n){    Matrix ans;    ans.init();    while(n)    {        if(n&1)            ans=mul(ans,a);        a=mul(a,a);        n>>=1;    }    return ans;}int main(){    ll f1,f2,t,n;    scanf("%lld",&t);    while(t--)    {        scanf("%lld%lld%lld",&n,&f1,&f2);        if(n==1)            printf("%lld\n",f1);        if(n==2)            printf("%lld\n",f2);        else        {            ll zhuanzhi[N][N]=            {                1,1,0,0,0,0,0,                2,0,0,0,0,0,0,                1,0,1,0,0,0,0,                4,0,4,1,0,0,0,                6,0,6,3,1,0,0,                4,0,4,3,2,1,0,                1,0,1,1,1,1,1            };            Matrix A,B;            for(ll i=0; i<N; i++)                for(ll j=0; j<N; j++)                    B.a[i][j]=zhuanzhi[i][j];            A.a[0][0]=f2,A.a[0][1]=f1,A.a[0][2]=16;            A.a[0][3]=8,A.a[0][4]=4,A.a[0][5]=2,A.a[0][6]=1;            Matrix p=mat_pow(B,n-2);            Matrix ans=mul(A,p);            printf("%lld\n",ans.a[0][0]);        }    }    return 0;}
阅读全文
0 0
原创粉丝点击