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 ...
随机推荐
- JMeter性能测试入门-不同类型线程组的使用
jmeter不同线程组的详解 在做性能测试之前,我们来了解一下JMeter多个不同线程组的应用.首先,JMeter提供了三个基本的线程组,分别为: Thread Group setUp Thread ...
- PHPstorm Xdebugger最全详细
0 Xdebug调试的原理(选看) 图0-1 单机调试原理示意图 图0-2 多机调试原理示意图 对于PHP开发,初来咋到,开发环境的搭建和理解感觉是最烦人的一件事了.不像JAVA,打开一个Eclips ...
- js导入外部文件
- Struts2异常:HTTP Status 404 - /Struts2/book/addBook.action
HTTP Status 404 - /Struts2/book/addBook.action 如果在Struts2的框架中访问路径出现了这个错误,可能存在的原因有如下的两个: 1. 路径写错,也就是a ...
- 优化内存_内存泄漏——C
内存泄漏: 动态申请内存,没有正常释放,后续又申请内存,也没释放内存,导致内存池被全部被占用,最终再申请内存的时候失败:严格点则每次申请内存的时候判断申请到的指针是否为空,若为空NULL则表示申请失 ...
- 解决MySQL报错:Access denied for user ‘root’@‘localhost’(using password: YES)
Windows 10(mysql5.1) 修改配置文件 找到MySQL安装目录下配置文件my.ini(在我的win10环境下,其路径为C:\ProgramData\MySQL\MySQL Server ...
- 把int 类型转化为varchar并且去掉小数点同时以千分号‘,’分割
把int 类型转化为money 类型,再转化为varchar,去掉小数点同时以千分号‘,’分割. select '$' + left(Convert(VARCHAR, cast(10000 as mo ...
- webpack之给目录起别名
1. 配置文件目录: build>webpack.base.config.js: resolve: { alias: { '@': resolve('src'), //照猫画虎 'styles' ...
- vue的请求数据方式
一,vue-resource请求数据 介绍:vue-resource请求数据方式是官方提供的一个插件 步骤: 1,npm安装 npm install vue-resource --save ...
- 1、获取ip地址
1.获取ip地址 System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces() .Select(p => p. ...