Description

存在如下递推式:

F(n+1)=A1*F(n)+A2*F(n-1)+…+An*F(1)

F(n+2)=A1*F(n+1)+A2*F(n)+…+An*F(2)



求第K项的值对1000000007取模的结果

Input

单组测试数据

第一行输入两个整数 n , k (1<=n<=100,n < k<=10000000000)

第二行输入 n 个整数 F(1) F(2) … F(n)

第三行输入 n 个整数A1 A2 … An

Output

输出一个整数

Sample Input

2 3

1 2

3 4

Sample Output

10

【题目链接】:http://oj.acmclub.cn/problem.php?cid=1162&pid=5

【题意】

【题解】



一道裸的矩阵乘法题;

构造一个系数矩阵

0       1       0    ...    0
0 0 1 ... 0
...
0 0 0 ... 1
a[n] a[n-1] a[n-2]... a[1]

(这个矩阵每乘一次(f[1],f[2],f[3]…f[n])就会往后递推一个n)



对于k>n的询问

求这个矩阵的(k-n)次幂;

然后把最后的矩阵的最后一行依次乘上f[1],f[2]…f[n]相加;

就是f[k]了;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110; const int G = 100; //矩阵大小
const LL MOD = 1e9 + 7; //模数
struct MX
{
int v[G+5][G+5];
void O() { ms(v, 0); }
void E() { ms(v, 0); for (int i = 1; i <= G; ++i)v[i][i] = 1; }
void P()
{
for (int i = 1; i <= G; ++i)
{
for (int j = 1; j <= G; ++j)printf("%d ", v[i][j]); puts("");
}
}
MX operator * (const MX &b) const
{
MX c; c.O();
for (int k = 1; k <= G; ++k)
{
for (int i = 1; i <= G; ++i) if (v[i][k])
{
for (int j = 1; j <= G; ++j)
{
c.v[i][j] = (c.v[i][j] + (LL)v[i][k] * b.v[k][j]) % MOD;
}
}
}
return c;
}
MX operator + (const MX &b) const
{
MX c; c.O();
for (int i = 1; i <= G; ++i)
{
for (int j = 1; j <= G; ++j)
{
c.v[i][j] = (v[i][j] + b.v[i][j]) % MOD;
}
}
return c;
}
MX operator ^ (LL p) const
{
MX y; y.E();
MX x; memcpy(x.v, v, sizeof(v));
while (p)
{
if (p&1) y = y*x;
x = x*x;
p>>=1;
}
return y;
}
}xishu; int n;
LL k,f[N],a[N]; int main(){
//Open();
Close();
cin >> n >> k;
rep1(i,1,n) cin >> f[i];
rep1(i,1,n) cin >> a[i];
rep1(i,1,n) xishu.v[n][i] = a[n-i+1];
rep1(i,1,n-1) xishu.v[i][i+1] = 1;
if (k<=n){
cout << f[k]%MOD << endl;
return 0;
}
xishu = xishu^(k-n);
LL ans = 0;
rep1(i,1,n)
ans = (ans + xishu.v[n][i]*f[i])%MOD;
cout << ans << endl;
return 0;
}

最新文章

  1. Openwebrtc
  2. DeepLearning之路(一)逻辑回归
  3. Jenkins 2.26 发布,可扩展的持续集成引擎
  4. 【转】详解Oracle的dual表
  5. jquery tree events didn&#39;t work
  6. 【Gym 100971A】Treasure Island
  7. java集合和scala集合互转
  8. 参考:iPhone OS 3.0中的字体列表
  9. 关于div的居中的问题
  10. Microsoft Windows 远程权限提升漏洞(CVE-2013-3175)(MS13-062)
  11. MySQL之外键约束
  12. 检测ORACLE方法汇总数据块损坏
  13. 关于异常“The &#39;Microsoft.ACE.OLEDB.12.0&#39; provider is not registered on the local machine”的处理
  14. springboot+mybatis+redis实现分布式缓存
  15. Unity-修改Debug日志文本颜色
  16. 表格列mouse经过时高亮显示
  17. java项目连接jdbc报错:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server
  18. jmeter线程组介绍
  19. 关于ueditor的使用心得
  20. Java--mysql实现分页查询--分页显示

热门文章

  1. Chrome Is The New C Runtime
  2. python--(常用模块-3-正则表达式)
  3. Git学习总结(8)——Git和SVN之间的基本区别
  4. WinServer-授权规则
  5. spring boot pom
  6. HDU 2643
  7. Hibernate的多种关系映射(oto、otm、mtm)
  8. 朝花夕拾——finally/final/finalize拨云雾见青天
  9. JavaScript实现双向链表
  10. 51nod 1649 齐头并进 (djikstra求最短路径,只用跑一次)