UVa 10870 - Recurrences
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1811
矩阵快速幂
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<stdexcept>
#include<bitset>
#include<cassert>
#include<deque> using namespace std; typedef long long ll;
typedef unsigned int uint;
const double eps=1e-12;
const int INF=0x3f3f3f3f;
const int N=50;
ll ma[N][N],mb[N][N],mc[N][N];
void matrixMul(ll a[][N],ll b[][N],int n,int m,int k,ll MOD)
{
memset(mc,0,sizeof(mc));
for(int i=1;i<=n;++i)
for(int j=1;j<=k;++j)
for(int l=1;l<=m;++l)
mc[i][j]=(mc[i][j]+a[i][l]*b[l][j]%MOD)%MOD;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
a[i][j]=mc[i][j];
}
int main()
{
//freopen("data.in","r",stdin);
int d,n;
ll m;
while(cin>>d>>n>>m)
{
if(!d&&!n&&!m) break;
memset(mb,0,sizeof(mb));
for(int i=1;i<=d;++i)
cin>>mb[i][1];
for(int i=2;i<=d;++i)
mb[i-1][i]=1;
for(int i=d;i>=1;--i)
cin>>ma[1][i];
if(n<=d)
{
cout<<ma[1][d+1-n]<<endl;
continue;
}
n-=d;
while(n)
{
if((n&1))
matrixMul(ma,mb,1,d,d,m);
n=n>>1;
matrixMul(mb,mb,d,d,d,m);
}
cout<<ma[1][1]<<endl;
}
return 0;
}
UVa 10870 - Recurrences的更多相关文章
- UVA 10870 - Recurrences(矩阵高速功率)
UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...
- 矩阵快速幂 UVA 10870 Recurrences
题目传送门 题意:f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d,求f (n) % m.训 ...
- UVA 10870 Recurrences(矩阵乘法)
题意 求解递推式 \(f(n)=a_1*f(n-1)+a_2*f(n-2)+....+a_d*f(n-d)\) 的第 \(n\) 项模以 \(m\). \(1 \leq n \leq 2^{31}-1 ...
- UVa 10870 Recurrences (矩阵快速幂)
题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...
- UVA - 10870 Recurrences 【矩阵快速幂】
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...
- UVa 10870 (矩阵快速幂) Recurrences
给出一个d阶线性递推关系,求f(n) mod m的值. , 求出An-dv0,该向量的最后一个元素就是所求. #include <iostream> #include <cstdio ...
- Recurrences UVA - 10870 (斐波拉契的一般形式推广)
题意:f(n) = a1f(n−1) + a2f(n−2) + a3f(n−3) + ... + adf(n−d), 计算这个f(n) 最重要的是推出矩阵. #include<cstdio> ...
- uva 10870 递推关系矩阵快速幂模
Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...
- UVA - 10870 UVA - 10870
Problem ARecurrencesInput: standard inputOutput: standard output Consider recurrent functions of the ...
随机推荐
- [转载] YouCompleteMe
原文: http://blog.marchtea.com/archives/161#rd?sukey=fc78a68049a14bb2ba33c15948d34749e1eb616df07efe977 ...
- [转载] what's goole mock
原文: https://code.google.com/p/googlemock/wiki/V1_7_ForDummies 地址被墙了, 看起来费劲, 转载一份 Google C++ Mocking ...
- Generator 函数的含义与用法
Generator 函数是协程在 ES6 的实现,最大特点就是可以交出函数的执行权(即暂停执行). function* gen(x){ var y = yield x + 2; return y; } ...
- python仿微软记事本
# -*- coding=utf-8 -*- from tkinter import filedialog import tkinter as tk import tkinter.scrolledte ...
- 用JS打开网页时自动更改css样式,可用于处理浏览器兼容
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- oracle对象类型
Oracle的对象类型 对象类型 在PL/SQL中,面向对象的程序设计师基于对象类型来完成的.对象类型是用户自定义的一种复合数据类型,它封装了数据结构和用于操纵这些数据结构的过程和函数. 数据库的对象 ...
- nfs服务器设置
前段时间重新装了一下Linux系统,结果导致NFS服务器总是挂在不成功,于是粗略学习了一下: NFS服务需要两个软件包: 1.nfs-utiles-* 2.portmap-* nfsd:它是基本的NF ...
- 如何将根文件系统制作成yaffs格式,并设置从yaffs启动
1.利用mkyaffs2image 工具,将根文件系统打包成yaffs镜像包 mkyaffs2image-128M root_qtopia root_qtopia.img 2.设置uboot参数boo ...
- phalcon: model 验证数据完整性
The above example performs a validation using the built-in validator “InclusionIn”. It checks the va ...
- xUtils更新到3.0后的基本使用规则
说实话,对于xUtils,是我最近才用到的开发框架(也是刚接触),对于其功能不得不说,简化了很多的开发步骤,可以说是非常好的开发工具,但是其最近更新到3.0也没有解决加载自定义ImageView报错的 ...