题目链接:https://vjudge.net/problem/UVA-10870

题意:

典型的矩阵快速幂的运用。比一般的斐波那契数推导式多了几项而已。

代码如下:

 #include <bits/stdc++.h>
#define rep(i,s,t) for(int (i)=(s); (i)<=(t); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const double eps = 1e-;
const int mod = ;
const int maxn = 2e5+; struct MA
{
LL mat[][];
void init()
{
rep(i,,) rep(j,,)
mat[i][j] = (i==j);
}
}; LL n,d,m;
LL a[],f[]; MA mul(MA x, MA y)
{
MA tmp;
ms(tmp.mat,);
rep(i,,d) rep(j,,d) rep(k,,d)
tmp.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%m, tmp.mat[i][j] %= m;
return tmp;
} MA qpow(MA x, LL y)
{
MA s;
s.init();
while(y)
{
if(y&) s = mul(s,x);
x = mul(x,x);
y >>= ;
}
return s;
} int main()
{
while(scanf("%lld%lld%lld",&d,&n,&m) && (d || n||m))
{
rep(i,,d) scanf("%lld",&a[i]);
rep(i,,d) scanf("%lld",&f[i]); if(n<=d)
{
printf("%lld\n", f[n]);
continue;
} MA x;
ms(x.mat,);
rep(i,,d) x.mat[][i] = a[i];
rep(i,,d) x.mat[i][i-] = ;
x = qpow(x,n-d); LL ans = ;
rep(i,,d)
ans += (1LL*x.mat[][i]*f[d-i+])%m, ans %= m;
printf("%lld\n",ans);
}
}

最新文章

  1. .NET Core爬坑记 1.0 项目文件
  2. IE8下获取iframe document EVENT对象的问题
  3. hdu5314 Happy King
  4. jq实现楼层切换效果
  5. iOS之可拖拽重排的CollectionView
  6. bzoj 1070: [SCOI2007]修车 费用流
  7. [PeterDLax著泛函分析习题参考解答]第5章 赋范线性空间
  8. wamp中的httpd.conf文件设置
  9. [IOS]包含增删改查移动的tableView展示+plist文件保存+程序意外退出保存Demo
  10. 构造函时和this指针
  11. 第一部分 记事本搞定第一个C#程序和编译过程剖析
  12. ThreadPool线程池
  13. 浅谈JSONP (vue-jsonp组件 XXXtoken:报错处理)
  14. 对CCLE数据库可以做的分析--转载
  15. GTY&#39;s gay friends HDU - 5172 线段树
  16. 如何自定义JSTL标签与SpringMVC 标签的属性中套JSTL标签报错的解决方法
  17. Xception
  18. Maven01 环境准备、maven项目结构、编译/测试/打包/清除、安装、
  19. ZOJ 2676 Network Wars[01分数规划]
  20. php接口编程

热门文章

  1. 着陆攻击LAND Attack
  2. kettle变量使用
  3. TdxBarButton的FASTSCRIPT封装
  4. Go -- 接口赋值
  5. 渗透测试思路 | Linux下自动化搭建FakeAP,劫持用户在Portal认证下的所有流量
  6. JavaScript的string方法(demo)
  7. 读《疯狂Java讲义》笔记总结三
  8. 【课程笔记】比特币和数字货币技术[Bitcoin and Cryptocurrency Technologies] week1
  9. oracle ORA-06550
  10. centos7下MySQL的配置