# LightOJ 1096 nth Term (构造矩阵)

f(n)    =    a * f(n-1) + b * f(n-3) + c, if(n > 2)
=    0, if(n ≤ 2)

(n <= 10^8)

#include
#include

const int mod = 10007;

struct mat{
int a[5][5];
};
int n, a, b, c;

mat mul(mat a, mat b){
mat ans;
for (int i = 1; i <= 4; i++){
for (int j = 1; j <= 4; j++){
ans.a[i][j] = 0;
for (int k = 1; k <= 4; k++){
ans.a[i][j] += a.a[i][k] * b.a[k][j];
ans.a[i][j] %= mod;
}
}
}
return ans;
}

void init(mat &t){
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++)
t.a[i][j] = 0;
}

int solve(){
if (n <= 2)return 0;
mat ans, aa;
init(ans); init(aa);
aa.a[1][1] = a; aa.a[1][3] = b; aa.a[1][4] = c;
aa.a[2][1] = 1; aa.a[3][2] = 1; aa.a[4][4] = 1;
ans.a[1][1] = ans.a[2][2] = ans.a[3][3] = ans.a[4][4] = 1;
n -= 2;
while (n){
if (n & 1){
ans = mul(ans, aa);
}
n /= 2;
aa = mul(aa, aa);
}
return ans.a[1][4];
}

int main(){
int T;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++){
scanf("%d%d%d%d", &n, &a, &b, &c);
printf("Case %d: %d\n", cas, solve());
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：LightOJ 1096 nth Term (构造矩阵) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)