HDU5950(矩阵快速幂)

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^31as 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.
         

题解:

n头牛报数,第一头报 a1,第二头报 a2,第三头报 a3=2*a1+a2+3^4,第四头报 a4=2*a2+a3+4^4……以此类推,问第n头牛报的数是多少。

数据量太大,暴力肯定会T,当时不知道该怎么写,比赛打完后,师哥说可以构造一个矩阵,然后矩阵快速幂。

初始矩阵 :

{  y ,  x , 3^4 , 3^3 , 3^2 , 3 , 1 }

目标矩阵:

{  f ( n ) , f ( n-1 ) , n^4 , n^3 , n^2 , n ,1 }

根据题意可得:f ( n ) = 2*f ( n-2 ) + f ( n-1 ) + n^4

所以构造的中间矩阵为:

  { 1 , 1 , 0 , 0 , 0 , 0 , 0 ,
     2 , 0 , 0 , 0 , 0 , 0 , 0 ,
     1 , 0 , 1 , 0 , 0 , 0 , 0 ,
     0 , 0 , 4 , 1 , 0 , 0 , 0 ,
     0 , 0 , 6 , 3 , 1 , 0 , 0 ,
     0 , 0 , 4 , 3 , 2 , 1 , 0 ,
     0 , 0 , 1 , 1 , 1 , 1 , 1   }

还有要注意的是,如果把mod定义为一个常变量,需要用long long存。

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define MAX 0x3f3f3f3f
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const LL mod=2147493647;
LL n,x,y;
struct node
{
    LL a[7][7];
};

node mul(node a,node b)           //矩阵相乘
{
    node c;
    memset(c.a,0,sizeof(c.a));
    for(int i=0; i<=6; i++)
    {
        for(int j=0; j<=6; j++)
        {
            for(int k=0; k<=6; k++)
            {
                c.a[i][j]+=((a.a[i][k]%mod)*(b.a[k][j]%mod))%mod;
                c.a[i][j]%=mod;
            }
        }
    }
    return c;
}

int main()
{
    int t;
    cin >>t;
    while(t--)
    {
        scanf("%lld%lld%lld",&n,&x,&y);
        n-=2;
        struct node s= {1,1,0,0,0,0,0,
                        2,0,0,0,0,0,0,
                        1,0,1,0,0,0,0,
                        0,0,4,1,0,0,0,
                        0,0,6,3,1,0,0,
                        0,0,4,3,2,1,0,
                        0,0,1,1,1,1,1
                        };
        struct node b= {y,x,81,27,9,3,1,
                        0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0
                        };
        while(n)                          //矩阵快速幂
        {
            if(n&1)
                b=mul(b,s);
            s=mul(s,s);
            n>>=1;
        }
        printf("%lld\n",b.a[0][0]);
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值