Recursive sequence(矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950

 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.

扫描二维码关注公众号,回复: 5373665 查看本文章
 

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.  

Source

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

 

Recommend

jiangzijing2015   |   We have carefully selected several similar problems for you:  6460 6459 6458 6457 6456 

题解:运用二项式定理得到n^4与(n-1)^4的关系,构造矩阵,运用矩阵快速幂即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Mat
{
    ll mat[10][10];
};
int x,y;
const ll mod=2147493647;
Mat operator*(Mat a,Mat b)
{
    Mat c;
    memset(c.mat,0,sizeof(c.mat));
    for(int i=1;i<=7;i++)
    {
        for(int j=1;j<=7;j++)
        {
            for(int k=1;k<=7;k++)
            {
                c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%mod;
            }
        }
    }
    return c;
}
Mat quickpow(int m)
{
    Mat res,a;
    memset(res.mat,0,sizeof(res.mat));
    memset(a.mat,0,sizeof(a.mat));
    res.mat[1][1]=y;
    res.mat[2][1]=x;
    res.mat[3][1]=16;
    res.mat[4][1]=8;
    res.mat[5][1]=4;
    res.mat[6][1]=2;
    res.mat[7][1]=1;
    a.mat[1][1]=1;a.mat[1][2]=2;a.mat[1][3]=1;a.mat[1][4]=4;a.mat[1][5]=6;a.mat[1][6]=4;a.mat[1][7]=1;
    a.mat[2][1]=1;a.mat[2][2]=0;a.mat[2][3]=0;a.mat[2][4]=0;a.mat[2][5]=0;a.mat[2][6]=0;a.mat[2][7]=0;
    a.mat[3][1]=0;a.mat[3][2]=0;a.mat[3][3]=1;a.mat[3][4]=4;a.mat[3][5]=6;a.mat[3][6]=4;a.mat[3][7]=1;
    a.mat[4][1]=0;a.mat[4][2]=0;a.mat[4][3]=0;a.mat[4][4]=1;a.mat[4][5]=3;a.mat[4][6]=3;a.mat[4][7]=1;
    a.mat[5][1]=0;a.mat[5][2]=0;a.mat[5][3]=0;a.mat[5][4]=0;a.mat[5][5]=1;a.mat[5][6]=2;a.mat[5][7]=1;
    a.mat[6][1]=0;a.mat[6][2]=0;a.mat[6][3]=0;a.mat[6][4]=0;a.mat[6][5]=0;a.mat[6][6]=1;a.mat[6][7]=1;
    a.mat[7][1]=0;a.mat[7][2]=0;a.mat[7][3]=0;a.mat[7][4]=0;a.mat[7][5]=0;a.mat[7][6]=0;a.mat[7][7]=1;
    while(m)
    {
        if(m&1)
            res=a*res;
        a=a*a;
        m>>=1;
    }
    return res;
}
int main()
{
    int n,t;
    cin>>t;
    while(t--)
    {
        cin>>n>>x>>y;
        if(n==1)
        {
            cout<<x%mod<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<y%mod<<endl;
            continue;
        }
        cout<<quickpow(n-2).mat[1][1]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43824158/article/details/87957382