UVA Recurrences 矩阵相乘+快速幂
2024-09-30 14:52:09
题目大意:
f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d),已给递推公式,求f(n)的大小。
解题思路:
n很大,所以我们就要构造矩阵,运用矩阵快速幂来求解。//题目描述上口口声声说int范围内,但是大家一定不要天真!!!!!!
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std; #define LL long long
const int maxn = ;
LL d, m;
struct mat
{
LL p[maxn][maxn];
}; mat mul (mat a, mat b);
mat pow (LL n, mat a, mat b); int main ()
{
LL n;
mat a, b; while (scanf ("%lld %lld %lld", &d, &n, &m), n+m+d)
{
memset (a.p, , sizeof(a.p));
memset (b.p, , sizeof(b.p)); for (int i=; i<d; i++)//构造矩阵
{
scanf ("%lld", &a.p[i][]);
a.p[i][] %= m;
a.p[i][i+] = ;
}
for (int i=; i<d; i++)//这个矩阵要反过来输入!!!!!!
{
scanf ("%lld", &b.p[][d-i-]);
b.p[][i] %= m;
} if (d < n)
{
b = pow (n-d, a, b);
printf ("%lld\n", b.p[][]);
}
else
printf ("%lld\n", b.p[][d-n]);
}
return ;
} mat mul (mat a, mat b)
{
mat c;
memset (c.p, , sizeof(c.p));
for (int i=; i<d; i++)
for (int j=; j<d; j++)
{
for (int k=; k<d; k++)
c.p[i][j] = (c.p[i][j] + a.p[i][k] * b.p[k][j]) % m;
}
return c;
}
mat pow (LL n, mat a, mat b)
{
while (n)
{
if (n % )
b = mul (b, a);
a = mul (a, a);
n /= ;
}
return b;
}
最新文章
- python基础七
- iosOpenDev-install 失败官方wiki无法解决看这里(尝试有效)
- WebStorm 9 自动编译 SCSS 产出 CSS 和 source maps
- CLion注册码算法逆向分析实录
- 改变Visual Studio 2012的皮肤
- 【转】使用BBB的device tree和cape(重新整理版)
- Visual Studio发布项目到远程服务器的步骤
- jQuery 之 $(this) 出了什么问题?
- <;Win32_18>;平滑的人物走动 —— 解决闪屏
- 使用Identity Server 4建立Authorization Server (5)
- Linux如此“自私”?
- es6 ...克隆与函数深度克隆
- stm32高级定时器的应用——spwm
- 系统不支持WP开发
- linux 权限之acl
- 安卓开发_数据存储技术_sqlite
- airflow
- Python量化库大全
- gentoo intel 安装桌面
- [leetcode]Distinct Subsequences @ Python
热门文章
- 报错: The type ByteInputStream is not accessible due to restriction on required library
- 【stl学习笔记】deques
- spring mvc 整理
- RxJava系列之中的一个 初识Rxjava
- hdoj 4790 Just Random 【数学】
- Codeforces Round #221 (Div. 2) D
- 【Mongodb教程 第七课 】MongoDB 查询文档
- asp.net mvc3的静态化实现
- Android 使用图片异步载入框架Universal Image Loader的问题
- jsp_类的封装_集合的应用