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 ...
随机推荐
- 【ABAP系列】SAP ABAP WS_DELIVERY_UPDATE 修改数量、过账日期并发货过账
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP WS_DELI ...
- 浅谈Java反射机制 之 获取类的字节码文件 Class.forName("全路径名") 、getClass()、class
另一个篇:获取 类 的 方法 和 属性(包括构造函数) 先贴上Java反射机制的概念: AVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它 ...
- 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. ...
- Netty实战之性能调优与设计模式
设计模式在Netty 中的应用(回顾): 单例模式要点回顾: 一个类在任何情况下只有一个对象,并提供一个全局访问点. 可延迟创建. 避免线程安全问题. 在我们利用netty自带的容器来管理客户端链接的 ...
- SUST_ACM_2019届暑期ACM集训热身赛题解
问题A:Hello SUST! 知识点:基本输入输出 C/C++: #include <stdio.h> int main() { int n; scanf("%d", ...
- UVA-10600.Contest and Blackout.(Kruskal + 次小生成树)
题目链接 本题思路:模版的次小生成树问题,输出MST and Second_MST的值. 参考代码: #include <cstdio> #include <cstring> ...
- java学习day3运算符
一.算数运算符 1.对于除号“/”,它的整数除和小数除是有区别的:当整数除以整数的时候,会把结果的小数部分舍弃,只保留整数部分,例如: int x=3510; x=x/1000; 输出结果为x=3; ...
- Handle Refresh Token Using ASP.NET Core 2.0 And JSON Web Token
来源: https://www.c-sharpcorner.com/article/handle-refresh-token-using-asp-net-core-2-0-and-json-web ...
- JavaScript基础3——使用Button提交表单
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Django基础之简介(二)
三板斧 from django.shortcuts import render,HttpResponse, redirect HttpResponse # 返回字符串 urls: urlpatte ...