UVA10870 Recurrences (矩阵快速幂及构造方法详解)

题意:
F(n) = a1 * F(n-1) + a2 * F(n-2)+ ···· + ad * F(n-d)。
求给你的n 。 很明显这是一道矩阵快速幂的题目。
题解:
[Fn-1, Fn-2, Fn-3, ···, Fn-d] * A(矩阵) = [Fn, Fn-1, Fn-2, ···, Fn-d+1] 。
Fn = 第一个矩阵 * A的第一列, 所以A矩阵的第一列为(a1, a2 , ··· ad)。
Fn = 第一个矩阵 * A的第二列, 所以A矩阵的第二列为(1, 0, 0,···, 0)。
同理可以推出整个A矩阵:
a1 1 0 ··· 0
a2 0 1 ··· 0
a3 0 0 ··· 0
··· 0 0 ··· 1
ad 0 0 0 0
当n 小于等于d 的时候 直接输出。
[ f(d), f(d-1), f(d-2), ···, f(2), f(1) ] * An-d = [Fn, Fn-1, Fn-2, ···, Fn-d+1] 。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int maxn = +;
int mod;
struct Matrix
{
LL c[maxn][maxn];
};//Matrix 矩阵
Matrix mult(Matrix a, Matrix b, int len)//矩阵乘法
{
Matrix hh={};
for(int i=;i<len;i++)
for(int j =;j<len;j++)
for(int k = ;k<len;k++){
hh.c[i][j] += (a.c[i][k]*b.c[k][j])%mod;
hh.c[i][j] %= mod;
}
return hh;
}
Matrix qpow_Matrix(Matrix a, int b, int len)
{
Matrix base = a;
Matrix ans;
//初始化ans = 1。
for(int i =;i<len;i++)
for(int j =;j<len;j++)
if(i==j) ans.c[i][j] = ;
else ans.c[i][j] = ;
//
while(b){
if(b&) ans = mult(ans, base, len);
base = mult(base, base, len);
b>>=;
}
return ans;
}
int a[maxn];
int f[maxn];
void solve(int d, int n, int m){
mod = m;
for(int i = ;i<=d;i++) cin >> a[i];
for(int i = ;i<=d;i++) cin >> f[i]; Matrix begin={};
for(int j = ;j<d;j++){
begin.c[][j] = f[d-j]%mod;
}
Matrix A={};
for(int j = ;j<d;j++)
A.c[j][] = a[j+]%mod;
for(int j = ;j<d;j++){
A.c[j][j+] = ;
}
if(n<=d){
cout << f[n]%mod << endl;
return;
}
Matrix temp = qpow_Matrix(A, n-d, d);
Matrix ans = mult(begin, temp, d);
cout << ans.c[][]%mod << endl;
return;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// ios::sync_with_stdio(0);
// cin.tie(0);
int d, n, m;
while(cin >> d >> n >> m){
if(d+n+m==) break;
solve(d, n, m);
}
return ;
}
UVA10870 Recurrences (矩阵快速幂及构造方法详解)的更多相关文章
- UVA10870 Recurrences —— 矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用.比一般的斐波那契数推导式多了几项而已. 代码如下: #include <bit ...
- UVa 10870 Recurrences (矩阵快速幂)
题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...
- UVA10870—Recurrences(简单矩阵快速幂)
题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...
- UVA - 10870 Recurrences 【矩阵快速幂】
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- BNU29139——PvZ once again——————【矩阵快速幂】
PvZ once again Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java cla ...
- DecodingGenome(CodeForces-222E)【矩阵快速幂】
题目链接:https://vjudge.net/contest/333591#problem/L 题意:用m个字符构成长度为n的串,其中存在形如“ab”(表示a后不能放置b)的条件约束,问共有多少种构 ...
- [题解][SHOI2013]超级跳马 动态规划/递推式/矩阵快速幂优化
这道题... 让我见识了纪中的强大 这道题是来纪中第二天(7.2)做的,这么晚写题解是因为 我去学矩阵乘法啦啦啦啦啦对矩阵乘法一窍不通的童鞋戳链接啦 层层递推会TLE,正解矩阵快速幂 首先题意就是给你 ...
- POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂
这两题属于AC自动机的第二种套路通过矩阵快速幂求方案数. 题意:给m个病毒字符串,问长度为n的DNA片段有多少种没有包含病毒串的. 根据AC自动机的tire图,我们可以获得一个可达矩阵. 关于这题的t ...
随机推荐
- day17跨文件夹导入模块,模块的两种被执行方式,包,直接使用包中模块,包的管理
复习 ''' 1.模块 -- 一系列功能的集合体,用文件来管理一系列有联系的功能,该文件我们称之为模块,文件名就是模块名 -- import | from...import 来导入模块,从而使用模块中 ...
- How are you to imagine anything if the images are always provided for you?
perdestrian: n. 行人 compliment: n. 赞扬 simply: adv. 只是,仅仅 shorten: vt. 缩短 accustom: vt. 习惯 collide: v. ...
- 应用安全-Web安全-CSRF攻防整理
原理 - 登录受信任网站A,并在本地生成Cookie.在不登出A的情况下,访问危险网站B. #csrfdemo.php <?php $data = json_decode(file_get_co ...
- WPF资源字典的使用
1.在解决方案中添加资源字典:鼠标右键——添加——资源字典 2.在资源字典中写入你需要的样式,我这里简单的写了一个窗体的边框样式 3.在App.xaml中加入刚刚新建的资源字典就好了
- [Python3] 005 列表的基本使用
目录 1. 列表概述 2. 创建列表 3. 列表常用操作 (1) 访问列表 (2) 分片操作 1) 正向操作 2) 反向操作 3) 内置函数 id() 加入队伍 1. 列表概述 一组有顺序的数据的组合 ...
- Count Color poj2777 线段树
Count Color poj2777 线段树 题意 有一个长木板,现在往上面在一定区间内刷颜色,后来刷的颜色会掩盖掉前面刷的颜色,问每次一定区间内可以看到多少种颜色. 解题思路 这里使用线段树,因为 ...
- Solr的学习使用之(九)facet.pivot实战
facet.pivot自己的理解,就是按照多个维度进行分组查询,以下是自己的实战代码,按照newsType,property两个维度统计: public List<ReportNewsTypeD ...
- poj 2187 Beauty Contest(平面最远点)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 24431 Accepted: 7459 D ...
- 分布式锁的实现【基于ZooKeeper】
引言 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...