uva10870
https://vjudge.net/problem/UVA-10870
裸的矩阵快速幂 注意系数矩阵在前面 因为系数矩阵为d*d 方程矩阵为d * 1 放反了就是d * 1 d * d 不符合矩阵乘法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
struct mat {
ll a[N][N];
} x, g;
int n, m, d;
ll a[N], f[N];
mat operator * (mat a, mat b)
{
mat ret; memset(ret.a, , sizeof(ret.a));
for(int i = ; i <= d; ++i)
for(int j = ; j <= d; ++j)
for(int k = ; k <= d; ++k) ret.a[i][j] = (ret.a[i][j] + a.a[i][k] % m * b.a[k][j] % m) % m;
return ret;
}
void build()
{
memset(x.a, , sizeof(x.a)); memset(g.a, , sizeof(g.a));
for(int i = ; i <= d; ++i) x.a[i][] = f[d - i + ];
for(int i = ; i <= d; ++i) g.a[][i] = a[i];
for(int i = ; i <= d; ++i) g.a[i][i - ] = ;
}
mat power(mat A, int t)
{
mat ret; memset(ret.a, , sizeof(ret.a));
for(int i = ; i <= d; ++i) ret.a[i][i] = ;
for(; t; t >>= , A = A * A) if(t & ) ret = ret * A;
return ret;
}
int main()
{
while(scanf("%d%d%d", &d, &n, &m))
{
if(n == && d == && m == ) break;
for(int i = ; i <= d; ++i) scanf("%d", &a[i]), a[i] %= m;
for(int i = ; i <= d; ++i) scanf("%d", &f[i]), f[i] %= m;
if(n <= d)
{
printf("%d\n", f[n]);
continue;
}
build();
mat t = power(g, n - d);
t = t * x;
printf("%lld\n", t.a[][]);
}
return ;
}
uva10870的更多相关文章
- UVA10870—Recurrences(简单矩阵快速幂)
题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...
- UVA10870 Recurrences —— 矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用.比一般的斐波那契数推导式多了几项而已. 代码如下: #include <bit ...
- uva10870 矩阵
f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d, 可以用矩阵进行优化,直接构造矩阵,然后快 ...
- uva10870 递推关系Recurrences
Consider recurrent functions of the following form:f(n) = a1f(n - 1) + a2f(n - 2) + a3f(n - 3) + : : ...
- Again Prime? No Time.(uva10870+数论)
Again Prime? No time.Input: standard inputOutput: standard outputTime Limit: 1 second The problem st ...
- UVA10870 Recurrences (矩阵快速幂及构造方法详解)
题意: F(n) = a1 * F(n-1) + a2 * F(n-2)+ ···· + ad * F(n-d). 求给你的n . 很明显这是一道矩阵快速幂的题目. 题解: [Fn-1, Fn-2, ...
- UVA10870递推关系(矩阵乘法)
题意: 给以个递推f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n > d.,给你n ...
- UVa 10870 & 矩阵快速幂
题意: 求一个递推式(不好怎么概括..)的函数的值. 即 f(n)=a1f(n-1)+a2f(n-2)+...+adf(n-d); SOL: 根据矩阵乘法的定义我们可以很容易地构造出矩阵,每次乘法即可 ...
- uva 10870
https://vjudge.net/problem/UVA-10870 题意: f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n ...
随机推荐
- PID28 [Stupid]愚蠢的宠物
题链:https://www.rqnoj.cn/problem/28 题目描述 背景 大家都知道,sheep有两只可爱的宠物(一只叫神牛,一只叫神菜).有一天,sheep带着两只宠物到狗狗家时,这两只 ...
- ..net 3.5新特性之用this关键字为类添加扩展方法
具体用法如下: public static class ClassHelper { //用this 声明将要吧这个方法附加到Student对象 public static bool CheckName ...
- QueryParser
[概述] 其他工具类使用比较方便,但不够灵活.QueryParser也实现了较多的匹配方式. [QueryParser的应用] /** * 使用QueryParser进行查询 * @throws Pa ...
- About SQL Server 2016 CPT2
SQL Server 2016 CTP2已经发布,可以从以下主页进行下载. http://www.microsoft.com/en-us/server-cloud/products/sql-serve ...
- MySQL Foreign Key
ntroduction to MySQL foreign key A foreign key is a field in a table that matches another field of a ...
- POJ 1679 判最小生成树的不唯一性 或 利用次小生成树求解
题目大意: 给定一个无向图,寻找它的最小生成树,如果仅有一种最小生成树,输出所有边的和,否则输出unique! 根据kruscal原理来说,每次不断取尽可能小的边不断添加入最小生成树中,那么可知如果所 ...
- [luoguP1388] 算式(DP)
传送门 看这个n<=15本以为是个状压DP 还是too young 这个题最神奇的地方是加括号是根据贪心的策略. 发现只有在一连串的加号两边加上括号才是最优的(想一想,为什么?) f[i][j] ...
- ZOJ 4016 Mergeable Stack 链表
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, the ...
- Mysql的timestamp类型,自动记录数据的更新时间
datetime也可以设置自动更新的
- delphi异步选择模型编程TCP
Server端: unit U_FrmServer; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, ...