UVA - 10870 Recurrences 【矩阵快速幂】
题目链接
https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553
题意
给出一个表达式 算法 f(n)
思路
n 很大 自然想到是 矩阵快速幂
那么问题就是 怎么构造矩阵
我们想到的一种构造方法是
n = 2 时
n = 3 时
然后大概就能够发现规律了吧 。。
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
#define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
#define syn_close ios::sync_with_stdio(false);cin.tie(0);
#define sp system("pause");
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 10;
const int MOD = 142857;
int d, n, m;
ll a[20], b[20];
struct Matrix
{
ll a[20][20];
Matrix() {}
Matrix operator * (Matrix const &b)const
{
Matrix res;
CLR(res.a, 0);
for (int i = 0; i < d; i++)
for (int j = 0; j < d; j++)
for (int k = 0; k < d; k++)
res.a[i][j] = (res.a[i][j] + this->a[i][k] * b.a[k][j]) % m;
return res;
}
};
Matrix pow_mod(Matrix ans, int n)
{
Matrix base;
CLR(base.a, 0);
for (int i = 0; i < d; ++i)
{
base.a[i][0] = a[i];
}
for (int i = 0; i < d; ++i)
{
base.a[i][i + 1] = 1;
}
while (n > 0)
{
if (n & 1)
ans = ans * base;
base = base * base;
n >>= 1;
}
return ans;
}
int main()
{
while (scanf("%d %d %d", &d, &n, &m) && (d || n || m))
{
for (int i = 0; i < d; i++)
scanf("%lld", &a[i]);
for (int i = 0; i < d; i++)
scanf("%lld", &b[i]);
if (n <= d)
{
printf("%lld\n", b[n - 1] % m);
continue;
}
Matrix ans;
for (int i = 0; i < d; i++)
for (int j = 0; j < d; j++)
ans.a[i][j] = b[d - j - 1];
ans = pow_mod(ans, n - d);
printf("%lld\n", ans.a[0][0]);
}
return 0;
}
UVA - 10870 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) .. ...
- uva 10870 递推关系矩阵快速幂模
Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...
- UVA 10870 - Recurrences(矩阵高速功率)
UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...
- UVA10870 Recurrences —— 矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用.比一般的斐波那契数推导式多了几项而已. 代码如下: #include <bit ...
- POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...
- uva 10518 - How Many Calls?(矩阵快速幂)
题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...
- Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)
题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3); 求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...
- UVA - 11149 (矩阵快速幂+倍增法)
第一道矩阵快速幂的题:模板题: #include<stack> #include<queue> #include<cmath> #include<cstdio ...
- UVA10870—Recurrences(简单矩阵快速幂)
题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...
随机推荐
- chrome浏览器提取网页视频
http://blog.csdn.net/pipisorry/article/details/37728839 在我们平时上网看视频听音乐时都会产生缓存,可是我们非常难通过一些软件把当中的视频和音乐文 ...
- [linux]iostat命令详解-监视系统输入输出设备和CPU的使用情况
部分转载https://blog.csdn.net/sunansheng/article/details/51942281 iostat命令被用于监视系统输入输出设备和CPU的使用情况.它的特点是汇报 ...
- session的两个小案例
学完了session,写两个小案例加深一下对session的巩固. 1. 用户登陆案例 登陆html页面提交后,将参数带给处理登陆的servlet,该servlet将获得登陆的用户名和密码,并将这些信 ...
- NSNotification的几点说明
1.NSNotification消息的同步性 ①NSNotification使用的是同步操作.即如果你在程序中的A位置post了一个NSNotification,在B位置注册了一个observer,通 ...
- 转 SQL行转列汇总
1.PIVOT 用于将列值旋转为列名(即行转列) PIVOT 的一般语法是:PIVOT(聚合函数(列名) FOR 列名 in (列值1,…) )AS P select * from TB pivot ...
- PHP面试题及答案解析(3)—MySQL数据库
1.mysql_num_rows()和mysql_affected_rows()的区别. mysql_num_rows()和mysql_affected_rows(),这两个函数都作用于 mysql_ ...
- 转 RabbitMQ
转自:https://blog.thankbabe.com/2017/08/03/rabbitmq-demo/?from=cnblogs 介绍 RabbitMQ是一个由erlang开发的基于AMQP( ...
- thinkphp5或3.2 php动态修改config配置文件永久保存
thinkphp默认的参数方法只能读取,或者动态修改不能永久修改. 这是自己摸索出来的特发出来给需要的朋友(懂的朋友别笑话,功能我自己使用是没任何问题).有些参数还是保存在配置文件方便快捷!不一定所有 ...
- thinkphp5.0极速搭建restful风格接口层实例
作为国内最流行的php框架thinkphp,很快就会发布v5.0正式版了,现在还是rc4版本,但已经很强大了下面是基于ThinkPHP V5.0 RC4框架,以restful风格完成的新闻查询(get ...
- sersync简介与测试报告
在分布式应用中会遇到一个问题,就是多个服务器间的文件如何能始终保持一致.一种经典的办法是将需要保持一致的文件存储在NFS上,这种方法虽然简单方便但却将本来多点的应用在文件存储上又变成了单点,这违背了分 ...