HDU 5950:Recursive sequence(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5950
题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f(1) = a, f(2) = b.
思路:在比赛时候知道是矩阵快速幂,可是推不出矩阵.那个n^4不知道怎么解决。结束后问其他人才知道要构造一个7 * 7的矩阵,而不是3 * 3的..
转自:http://blog.csdn.net/spring371327/article/details/52973534
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f
#define MOD 2147493647
typedef long long LL; struct matrix
{
LL a[][]; void init() {
memset(a, , sizeof(a));
for(int i = ; i < ; i++) a[i][i] = ;
} matrix operator * (matrix b) {
matrix ans;
LL tmp;
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
ans.a[i][j] = ;
for(int k = ; k < ; k++) {
tmp = a[i][k] * b.a[k][j] % MOD;
ans.a[i][j] = (ans.a[i][j] + tmp % MOD) % MOD;
}
}
}
return ans;
}
}; matrix q_pow(matrix a, LL b)
{
matrix ans;
ans.init();
while(b) {
if(b & ) ans = ans * a;
b >>= ;
a = a * a;
}
return ans;
} int main()
{
matrix mo;
memset(mo.a, , sizeof(mo.a));
mo.a[][] = ;
mo.a[][] = ; mo.a[][] = , mo.a[][] = , mo.a[][] = , mo.a[][] = , mo.a[][] = , mo.a[][] = ;
mo.a[][] = , mo.a[][] = , mo.a[][] = , mo.a[][] = , mo.a[][] = ;
mo.a[][] = , mo.a[][] = , mo.a[][] = , mo.a[][] = ;
mo.a[][] = , mo.a[][] = , mo.a[][] = ;
mo.a[][] = , mo.a[][] = ;
mo.a[][] = ;
int t;
scanf("%d", &t);
while(t--) {
long long n, a, b;
scanf("%I64d%I64d%I64d", &n, &a, &b);
if(n == ) printf("%I64d\n", a);
else if(n == ) printf("%I64d\n", b);
else {
matrix ans = q_pow(mo, n - );
LL sum = ;
sum = (sum + ans.a[][] * a) % MOD;
sum = (sum + ans.a[][] * b) % MOD;
sum = (sum + ans.a[][] * ) % MOD;
sum = (sum + ans.a[][] * ) % MOD;
sum = (sum + ans.a[][] * ) % MOD;
sum = (sum + ans.a[][] * ) % MOD;
sum = (sum + ans.a[][]) % MOD;
printf("%I64d\n", sum);
}
}
return ;
}
HDU 5950:Recursive sequence(矩阵快速幂)的更多相关文章
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
- Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)
题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
随机推荐
- 第八篇 Integration Services:高级工作流管理
本篇文章是Integration Services系列的第八篇,详细内容请参考原文. 简介在前面两篇文章,我们创建了一个新的SSIS包,学习了SSIS中的脚本任务和优先约束,并检查包的MaxConcu ...
- cocoapods遇到error: RPC failed; curl 56 SSLRead() return error -36问题
在安装cocoapods遇到的问题 [!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master Cloning into ...
- mysql -prompt选项
使用-pormpt修改提示符.可以在登录时或者在登录后使用prompt选项来修改提示符 (1)使用mysql命令行参数修改提示符 # mysql -u root -p Enter password: ...
- WPF 面试题及答案(三)
一 · 路由事件的三种方式/策略(冒泡 直接 隧道) WPF中的路由事件是沿着VisualTree传递的,作用是用来调用应用程序的元素树上的各种监听器上的处理程序. (1)冒泡,这种事件处理方式是从源 ...
- SQL循环添加表中的字段
USE BookDiscuss DECLARE @num INT DECLARE @numdiff INT DECLARE @table VARCHAR(50 ...
- For嵌套输出图形
/*输出此图形 * * * * * * * * * ** * * * * * * * * * * * * * *解析:可以把此图形看成两部分----*---* *--* ...
- dataset 使用
下面有例子说明: 首先我们需要打开一个联结: string MyConnString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/ ...
- c# 获取路径的几种方法
1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.Bas ...
- Mysql自定义函数总结
存储函数 创建存储函数,需要使用CREATE FUNCTION语句,基本语法如下: CREATE FUNCTION func_name([func_parameter]) RETURNS TYPE [ ...
- DDR(一)
P-Bank:计算机早期的一个概念.目的:匹配内存芯片和CPU芯片的数据总线的宽度.方法:并联多个内存模块. L-Bank:对内部存储阵列的分割,避免寻址冲突,提高内存效率.通过ba信号选择bank, ...