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