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 ...
随机推荐
- 浅谈vue对seo的影响
不可否定的是,vue现在火.但是在实际项目中,特别是像一下交互网站,我们不可避免会考虑到的是seo问题,这直接关系到我们网站的排名,很多人说用vue搭建的网站不能做优化,那我们真的要放弃vue,放弃前 ...
- 20190908 On Java8 第十九章 类型信息
第十九章 类型信息 RTTI(RunTime Type Information,运行时类型信息)能够在程序运行时发现和使用类型信息. Java 主要有两种方式在运行时识别对象和类信息: "传 ...
- log记录日志使用说明
一. 想要让Log4net日志(以下称日志)按每月自动归类为一个文件夹,为此,学习和修改了log4net.config文件.查了资料,重点是以下这些参数: <param name="F ...
- BZOJ 4552(二分+线段树+思维)
题面 传送门 分析 此题是道好题! 首先要跳出思维定势,不是去想如何用数据结构去直接维护排序过程,而是尝试二分a[p]的值 设二分a[p]的值为x 我们将大于x的数标记为1,小于等于x的数标记为0 则 ...
- day20 博客系统开发
setting 文件加入 AUTH_USER_MODEL = "app名称.UserInfo" from django.db import models # Create ...
- ListView鼠标拖
private Point Position = new Point(0, 0); private void treeFileView_ItemDrag(object sender, ItemDrag ...
- 07-Log日志
# 1. 日志相关概念 - 日志的级别(level) - 不同的用户关注不同的程序信息 - DEBUG - INFO - NOTICE - WARNING - ERROR - CRITICAL - A ...
- 1、获取ip地址
1.获取ip地址 System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces() .Select(p => p. ...
- [译]深度学习(Yann LeCun)
深度学习 严恩·乐库 约书亚•本吉奥 杰弗里·希尔顿 摘要深度学习是计算模型,是由多个处理层学习多层次抽象表示的数据.这些方法极大地提高了语音识别.视觉识别.物体识别.目标检测和许多其他领域如药物 ...
- 关于windows服务器配置
#我是用的window service2008系统,在配置服务器时由于是用php进行搭建 #首先我安装好phpstudy,通过服务器ip访问,显示了个helloworld,我查看了phpstudy里的 ...