Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 36 Accepted Submission(s): 24
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.
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 369HintIn 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
Recommend
jiangzijing2015 | We have carefully selected several similar problems for you:
5960
5959
5958
5957
5956
矩阵快速幂。
构造一个七维的矩阵。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=2147493647;
struct M
{
long long a[8][8];
};
M A,q,qq;
void debug()
{
for(int i=1; i<=7; i++)
{
for(int j=1; j<=7; j++)
cout<<qq.a[i][j]<<" ";
cout<<endl;
}
}
M mul(M a,M b)
{
M c;
int i,j,k;
for(i=1; i<=7; i++)
for(j=1; j<=7; j++)
c.a[i][j]=0;
for(i=1; i<=7; i++)
for(j=1; j<=7; j++)
for(k=1; k<=7; k++)
{
c.a[i][j]+=((a.a[i][k]*b.a[k][j]%mod))%mod;
c.a[i][j]%=mod;
}
return c;
}
M fast(M a,int n)
{
M b;
memset(b.a,0,sizeof(b.a));
for(int i=1; i<=7; i++)
b.a[i][i]=1;
while(n)
{
if(n&1)
b=mul(b,a);
a=mul(a,a);
n/=2;
}
return b;
}
ll dp[105];
void work(ll b,ll c)
{
dp[1]=b;
dp[2]=c;
for(int i=3; i<=100; i++)
{
dp[i]=(dp[i-1]%mod+dp[i-2]*2%mod)%mod+i%mod*i%mod*i%mod*i%mod;
cout<<"i="<<i<<" "<<dp[i]<<endl;
}
}
int main()
{
//work(1LL*2,1LL*5);
int t;
scanf("%d",&t);
while(t--)
{
int n,aa,bb;
cin>>n>>aa>>bb;
if(n==1)
{
cout<<aa%mod<<endl;
continue;
}
else if(n==2)
{
cout<<bb%mod<<endl;
continue;
}
M B;
n-=2;
for(int i=1; i<=7; i++)
for(int j=1; j<=7; j++)
A.a[i][j]=0,B.a[i][j]=0;
for(int i=1; i<=5; i++)
A.a[1][i]=1;
for(int i=2; i<=5; i++)
A.a[2][i]=i-1;
A.a[3][3]=1;
A.a[3][4]=3;
A.a[3][5]=6;
A.a[4][4]=1;
A.a[4][5]=4;
A.a[5][5]=1;
A.a[5][6]=1;
A.a[6][6]=1;
A.a[6][7]=1;
A.a[7][6]=2;
B.a[1][1]=1;
B.a[1][2]=3;
B.a[1][3]=9;
B.a[1][4]=27;
B.a[1][5]=81;
B.a[1][6]=bb;
B.a[1][7]=aa;
q=fast(A,n);
qq=mul(B,q);
printf("%lld\n",qq.a[1][6]%mod);
}
return 0;
}