HDU 5950 矩阵快速幂

时间:2023-03-08 16:59:02

Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30    Accepted Submission(s): 20

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.

Source
题意:f[i]=2*f[i-2]+f[i-1]+i^4
题解:[f[i],f[i-1],i^4,i^3,i^2,i,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
构造矩阵  矩阵快速幂
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
const ll k=;
struct matrix
{
ll m[][];
} ans,exm;
struct matrix matrix_mulit1(struct matrix aa,struct matrix bb)
{
struct matrix there;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
there.m[i][j]=;
for(int u=;u<;u++)
there.m[i][j]=(there.m[i][j]+aa.m[i][u]*bb.m[u][j]%k)%k;
}
}
return there;
}
struct matrix matrix_mulit2(struct matrix aa,struct matrix bb)
{
struct matrix there;
for(int j=;j<;j++)
{
there.m[][j]=;
for(int u=;u<;u++)
there.m[][j]=(there.m[][j]+aa.m[][u]*bb.m[u][j]%k)%k;
}
return there;
}
ll matrix_quick(ll aa,ll bb,ll gg)
{
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;exm.m[][]=;
ans.m[][]=bb;ans.m[][]=aa;ans.m[][]=;ans.m[][]=;ans.m[][]=;ans.m[][]=;ans.m[][]=;
gg-=;
while(gg)
{
if(gg&)
ans=matrix_mulit2(ans,exm);
exm=matrix_mulit1(exm,exm);
gg >>= ;
}
return ans.m[][];
}
int t;
ll n,a,b;
int main()
{
scanf("%d",&t);
for(int i=;i<=t;i++)
{
scanf("%I64d %I64d %I64d",&n,&a,&b);
if(n==)
printf("%I64d\n",a);
else if(n==)
printf("%I64d\n",b);
else
printf("%I64d\n",(matrix_quick(a, b, n)+k)%k);
}
return ;
}