题目描述
存在如下递推式:
F(n+1)=A1*F(n)+A2*F(n-1)+...+An*F(1)
求第K项的值对1000000007取模的结果
输入
单组测试数据
第一行输入两个整数 n , k (1<=n<=100,n<k<=10000000000)
第二行输入 n 个整数 F(1) F(2) ... F(n)
第三行输入 n 个整数A1 A2 ... An
输出
输出一个整数
样例输入
2 3
1 2
3 4
样例输出
10
提示
#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 10;
const int Mod = 1000000007;
long long n, k;
long long a[N], f[N];
struct xx{
long long a[N][N];
} ori, res;
xx mul(xx x, xx y){
xx temp;
memset(temp.a, 0, sizeof(temp.a));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int k = 0; k < n; k++){
if(!x.a[i][k]) continue;
temp.a[i][j] += x.a[i][k]*y.a[k][j]+Mod;
temp.a[i][j] %= Mod;
}
}
}
return temp;
}
void init(){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i == j && i < n-1){
ori.a[i][j+1] = 1;
}
}
}
for(int j = 0; j < n; j++){
ori.a[n-1][j] = a[n-j-1];
}
memset(res.a, 0, sizeof(res.a));
for(int i = 0; i < n; i++){
res.a[i][0] = f[i];
}
}
void calc(long long k){
while(k){
if(k&1){
res = mul(ori, res);
}
ori = mul(ori, ori);
k >>= 1;
}
printf("%lld\n", res.a[n-1][0]);
}
int main(){
while(scanf("%lld%lld", &n, &k) == 2){
for(int i = 0; i < n; i++){
scanf("%lld", &f[i]);
}
for(int i = 0; i < n; i++){
scanf("%lld", &a[i]);
}
init();
calc(k-n);
}
}