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
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
Sample Output
Hint
大概就这样了。。。。。。
. 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.
Each case contains only one line with three numbers N, a and b where N,a,b < 231
2 3 1 2 4 1 10
85 369
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.
题意:a[i]=2*a[i-2]+a[i-1]+i^4,给出n,a1,a2,求an
思路:a[i]=2*a[i-2]+a[i-1]+i^4=2*a[i-2]+a[i-1]+((i-1)+1)^4=2*a[i-2]+a[i-1]+(i-1)^4+4*(i-1)^3+6*(i-1)^2+4*(i-1)+1
构造7*7的矩阵然后快速幂
设矩阵:
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为 martix-1
设 矩阵( a[i-1],a[i-2],(i-1)^4,(i-1)^3,(i-1)^2,(i-1),1 ) 为martix-2.
则 martix-2 * martix-1 = ( a[i],a[i-1],i^4,i^3,i^2,i,1 )
所以ac代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cmath>
using namespace std;
typedef long long LL;
const LL MOD=2147493647;
struct martix
{
LL a[7][7];
}x;
void output(martix a)
{
for(int i=0;i<7;i++)
{
for(int j=0;j<7;j++)
{
printf("%lld ",a.a[i][j]);
}
puts("");
}
puts("");
}
martix mul(martix a,martix b)
{
martix c;
for(int i=0;i<7;i++)for(int j=0;j<7;j++)c.a[i][j]=0;
for(int i=0;i<7;i++)
{
for(int j=0;j<7;j++)
{
for(int k=0;k<7;k++)
{
c.a[i][j]+=a.a[i][k]*b.a[k][j];
c.a[i][j]%=MOD;
}
}
}
// puts("C************");
// output(a);output(b);
// output(c);
return c;
}
martix quick(LL n)
{
martix e;
// printf("n=%lld********************\n",n);
for(int i=0;i<7;i++)for(int j=0;j<7;j++)if(i==j)e.a[i][j]=1;else e.a[i][j]=0;
while(n)
{
if(n&1)e=mul(e,x);
x=mul(x,x);
n>>=1;
// printf("n=%lld********************\n",n);
}
return e;
}
void init()
{
x.a[0][0]=1,x.a[0][1]=1,x.a[0][2]=0,x.a[0][3]=0,x.a[0][4]=0,x.a[0][5]=0,x.a[0][6]=0;
x.a[1][0]=2,x.a[1][1]=0,x.a[1][2]=0,x.a[1][3]=0,x.a[1][4]=0,x.a[1][5]=0,x.a[1][6]=0;
x.a[2][0]=1,x.a[2][1]=0,x.a[2][2]=1,x.a[2][3]=0,x.a[2][4]=0,x.a[2][5]=0,x.a[2][6]=0;
x.a[3][0]=4,x.a[3][1]=0,x.a[3][2]=4,x.a[3][3]=1,x.a[3][4]=0,x.a[3][5]=0,x.a[3][6]=0;
x.a[4][0]=6,x.a[4][1]=0,x.a[4][2]=6,x.a[4][3]=3,x.a[4][4]=1,x.a[4][5]=0,x.a[4][6]=0;
x.a[5][0]=4,x.a[5][1]=0,x.a[5][2]=4,x.a[5][3]=3,x.a[5][4]=2,x.a[5][5]=1,x.a[5][6]=0;
x.a[6][0]=1,x.a[6][1]=0,x.a[6][2]=1,x.a[6][3]=1,x.a[6][4]=1,x.a[6][5]=1,x.a[6][6]=1;
}
LL n,a1,a2;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld",&n,&a1,&a2);
if(n==1)printf("%lld\n",a1);
else if(n==2)printf("%lld\n",a2);
else
{
init();
// puts("X*************");
// output(x);
x=quick(n-2);
// puts("Y*************");
// output(x);
LL b[7]={a2,a1,16,8,4,2,1};
LL ans=0;
for(int i=0;i<7;i++)
{
ans=(ans+b[i]*x.a[i][0])%MOD;
}
printf("%lld\n",ans);
}
}
return 0;
}
大概就这样了。。。。。。
本人蒟蒻,如有错误,还望指正