题意:

  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] 。

  F = 第一个矩阵 * 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 (矩阵快速幂及构造方法详解)的更多相关文章

  1. UVA10870 Recurrences —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用.比一般的斐波那契数推导式多了几项而已. 代码如下: #include <bit ...

  2. UVa 10870 Recurrences (矩阵快速幂)

    题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...

  3. UVA10870—Recurrences(简单矩阵快速幂)

    题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...

  4. UVA - 10870 Recurrences 【矩阵快速幂】

    题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...

  5. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  6. BNU29139——PvZ once again——————【矩阵快速幂】

    PvZ once again Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cla ...

  7. DecodingGenome(CodeForces-222E)【矩阵快速幂】

    题目链接:https://vjudge.net/contest/333591#problem/L 题意:用m个字符构成长度为n的串,其中存在形如“ab”(表示a后不能放置b)的条件约束,问共有多少种构 ...

  8. [题解][SHOI2013]超级跳马 动态规划/递推式/矩阵快速幂优化

    这道题... 让我见识了纪中的强大 这道题是来纪中第二天(7.2)做的,这么晚写题解是因为 我去学矩阵乘法啦啦啦啦啦对矩阵乘法一窍不通的童鞋戳链接啦 层层递推会TLE,正解矩阵快速幂 首先题意就是给你 ...

  9. POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂

    这两题属于AC自动机的第二种套路通过矩阵快速幂求方案数. 题意:给m个病毒字符串,问长度为n的DNA片段有多少种没有包含病毒串的. 根据AC自动机的tire图,我们可以获得一个可达矩阵. 关于这题的t ...

随机推荐

  1. GARENA面试

    约了2019年10月16日下午2点现场面 岗位:数据开发 下午2点准时到了公司,公司环境棒棒哒,hr小姐姐也是贴心,整个面试的过程真的棒棒哒. 在我所有的面试经历中,这个是体验感最棒的,其次是上中的面 ...

  2. linux文件io与标准io

    文件IO实际是API,Linux对文件操作主要流程为:打开(open),操作(write.read.lseek),关闭(close). 1.打开文件函数open(): 涉及的头文件:  #includ ...

  3. 在Keras中用Bert进行情感分析

    之前在BERT实战——基于Keras一文中介绍了两个库 keras_bert 和 bert4keras 但是由于 bert4keras 处于开发阶段,有些函数名称和位置等等发生了变化,那篇文章只用了 ...

  4. 版本控制工具 GIT 简要教程

    一,Git 简介 其实这个就不用说了 但是国际惯例还是介绍一下吧; Git 是一个开源的分布式版本控制系统,用于敏捷 高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助 ...

  5. #10017 传送带(SCOI 2010)(三分套三分)

    [题目描述] 在一个 2 维平面上有两条传送带,每一条传送带可以看成是一条线段.两条传送带分别为线段 AB 和线段 CD.lxhgww 在 AB上的移动速度为 P ,在 CD 上的移动速度为 Q,在平 ...

  6. 可下拉的PinnedHeaderExpandableListView的实现

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/singwhatiwanna/article/details/25546871 转载请注明出处:htt ...

  7. free野指针问题

    gdb backtrace内容如下: Program received signal SIGABRT, Aborted. (gdb) p cmd No symbol "cmd" i ...

  8. vue.js(18)--父组件向子组件传值

    子组件是不能直接使用父组件中数据的,需要进行属性绑定(v-bind:自定义属性名=“msg”),绑定后需要在子组件中使用props[‘自定义属性名’]数组来定义父组件的自定义名称. props数组中的 ...

  9. vue项目中使用高德地图(根据坐标定位点)

    前言 项目中需要根据坐标定位,将自己的实现过程写下来,废话不多说,上代码 正文 <script> var map,marker; export default { data(){ retu ...

  10. [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (4/5)