UVA10870 Recurrences —— 矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10870
题意:
典型的矩阵快速幂的运用。比一般的斐波那契数推导式多了几项而已。
代码如下:
- #include <bits/stdc++.h>
- #define rep(i,s,t) for(int (i)=(s); (i)<=(t); (i)++)
- #define ms(a,b) memset((a),(b),sizeof((a)))
- using namespace std;
- typedef long long LL;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const double eps = 1e-;
- const int mod = ;
- const int maxn = 2e5+;
- struct MA
- {
- LL mat[][];
- void init()
- {
- rep(i,,) rep(j,,)
- mat[i][j] = (i==j);
- }
- };
- LL n,d,m;
- LL a[],f[];
- MA mul(MA x, MA y)
- {
- MA tmp;
- ms(tmp.mat,);
- rep(i,,d) rep(j,,d) rep(k,,d)
- tmp.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%m, tmp.mat[i][j] %= m;
- return tmp;
- }
- MA qpow(MA x, LL y)
- {
- MA s;
- s.init();
- while(y)
- {
- if(y&) s = mul(s,x);
- x = mul(x,x);
- y >>= ;
- }
- return s;
- }
- int main()
- {
- while(scanf("%lld%lld%lld",&d,&n,&m) && (d || n||m))
- {
- rep(i,,d) scanf("%lld",&a[i]);
- rep(i,,d) scanf("%lld",&f[i]);
- if(n<=d)
- {
- printf("%lld\n", f[n]);
- continue;
- }
- MA x;
- ms(x.mat,);
- rep(i,,d) x.mat[][i] = a[i];
- rep(i,,d) x.mat[i][i-] = ;
- x = qpow(x,n-d);
- LL ans = ;
- rep(i,,d)
- ans += (1LL*x.mat[][i]*f[d-i+])%m, ans %= m;
- printf("%lld\n",ans);
- }
- }
UVA10870 Recurrences —— 矩阵快速幂的更多相关文章
- 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,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...
- UVA10870 Recurrences (矩阵快速幂及构造方法详解)
题意: F(n) = a1 * F(n-1) + a2 * F(n-2)+ ···· + ad * F(n-d). 求给你的n . 很明显这是一道矩阵快速幂的题目. 题解: [Fn-1, Fn-2, ...
- UVA - 10870 Recurrences 【矩阵快速幂】
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
随机推荐
- Codeforces Gym 100338B Spam Filter 字符串哈希+贝叶斯公式
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- Tarjan缩点+DAG图dp
题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...
- 如何设置tomcat服务器编码为utf-8编码
原文:http://blog.csdn.net/u014079773/article/details/52637057 在实际开发中我们经常遇到request请求的中文乱码,那么如何解决中文乱码问题? ...
- zip 压缩文件夹
import java.io.*; import java.util.zip.*; /** * @author Dana·Li * <p> * 程序实现了ZIP压缩[compression ...
- cocos2d-x step by step(3) Double Kill
喏,咱们已经调通hello world 了,然后呢,咱们做一些高大上的东西,那做什么呢,做一个打乒乓球的小东西,啊哈! 这就是最终界面了,没画一个球形 就用一个白色方框代替吧. 啊哈! public ...
- curl如何发送json数据?如何发送form数据?python的restfull又该如何获取这些数据?
1.python使用flask+flask_restfull框架写的api接口,做为服务 2.curl 做为客户端发送数据 from flask import request curl发送json的方 ...
- caffe版faster-RCNN环境搭建
faster-rcnn提出论文: <Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks& ...
- php 源码编译
https://cyberpersons.com/2016/08/28/install-nginx-php-php-fpm-mysql-source-run-wordpress-site-ubuntu ...
- 使用HtmlUnit登录百度
环境 java 1.7 HtmlUnit 2.18 eclipse 4.4.2 maven 配置: <dependency> <groupId>net.sourceforge. ...
- OpenCV for Python 学习笔记 二
今天主要看了OpenCV中的事件以及回调函数,这么说可能不准确,主要是下面这两个函数(OpenCV中还有很多这些函数,可以在 http://docs.opencv.org/trunk/modules/ ...