NYOJ-301递推求值

时间:2023-03-09 05:52:44
NYOJ-301递推求值

递推求值

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述

给你一个递推公式:

f(x)=a*f(x-2)+b*f(x-1)+c

并给你f(1),f(2)的值,请求出f(n)的值,由于f(n)的值可能过大,求出f(n)对1000007取模后的值。

注意:-1对3取模后等于2

输入
第一行是一个整数T,表示测试数据的组数(T<=10000)
随后每行有六个整数,分别表示f(1),f(2),a,b,c,n的值。
其中0<=f(1),f(2)<100,-100<=a,b,c<=100,1<=n<=100000000 (10^9)
输出
输出f(n)对1000007取模后的值
样例输入
2
1 1 1 1 0 5
1 1 -1 -10 -100 3
样例输出
5
999896
NYOJ-301递推求值
//B^(n-2)采用矩阵快速幂计算
#include <stdio.h>
#include <string.h>
#define N 3
#define mod 1000007
#define ll long long
struct Matrix{
ll mat[N][N];//乘积时(可能接近mod^2)超出int,
};
struct Matrix mul(struct Matrix a,struct Matrix b)
{
int i,j,k;
struct Matrix res;
for(i=;i<N;i++)
{
for(j=;j<N;j++)
{
res.mat[i][j] = ;
for(k=;k<N;k++)
{
res.mat[i][j] += a.mat[i][k]*b.mat[k][j];
res.mat[i][j] %= mod;//必须每次取余
}
}
}
return res;
}
struct Matrix mul_matrix(struct Matrix b,int n)
{
struct Matrix res = {
,,,
,,,
,,
};//单位阵
while(n)
{
if(n&)
res = mul(res,b);
n >>= ;
b = mul(b,b);
}
return res;
}
int main()
{
int T,f[],a,b,c,n;
struct Matrix tmp,res;
struct Matrix tmp_matrix = {//中间矩阵
,,,//每组测试将此三处的值更新为b,a,c
,,,
,,
};
memset(tmp.mat,,sizeof(tmp.mat));//tmp值只有(0,0),(1,0)需要填入f(2),f(1)的值,其余不变
tmp.mat[][] = ;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d%d",&f[],&f[],&a,&b,&c,&n);
if(n == || n == )
printf("%d\n",(f[n-]+mod)%mod);//注意对负值的处理
else{
tmp_matrix.mat[][] = b;
tmp_matrix.mat[][] = a;
tmp_matrix.mat[][] = c;
tmp.mat[][] = f[];
tmp.mat[][] = f[];
//实际上最终我们只需进行矩阵和向量(f(2),f(1),1)'的运算,但是因为在进行矩阵快速幂时我们已经定义了矩阵乘积
//所以不妨借用,最后取(0,0)的值即可
res = mul(mul_matrix(tmp_matrix,n-),tmp);
printf("%lld\n",(res.mat[][]+mod)%mod);
}
}
return ;
}

参照:http://blog.csdn.net/lyhvoyage/article/details/22926265

2017-02-28