5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn;
析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无法进行运算的。好像有的思路,最后也没想出来,还是参考的大牛的博客
http://blog.csdn.net/spring371327/article/details/52973534
那是讲的很详细了,就不多说了,注意这个取模不是1e9+7,一开始忘了。。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const LL mod = 2147493647;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Matrix{
LL a[7][7];
Matrix operator * (const Matrix &p){
Matrix res;
for(int i = 0; i < 7; ++i)
for(int j = 0; j < 7; ++j){
res.a[i][j] = 0;
for(int k = 0; k < 7; ++k)
res.a[i][j] = (res.a[i][j] + a[i][k] * p.a[k][j]) % mod;
}
return res;
}
}; Matrix quick_pow(Matrix b, LL n){
Matrix res;
memset(res.a, 0, sizeof res.a);
for(int i = 0; i < 7; ++i) res.a[i][i] = 1;
while(n){
if(n & 1) res = res * b;
b = b * b;
n >>= 1;
}
return res;
} int main(){
Matrix x;
memset(x.a, 0, sizeof x.a);
x.a[0][0] = 1; x.a[0][1] = 2; x.a[0][2] = 1; x.a[0][3] = 4; x.a[0][4] = 6;
x.a[0][5] = 4; x.a[0][6] = 1; x.a[1][0] = 1; x.a[2][2] = 1; x.a[2][3] = 4;
x.a[2][4] = 6; x.a[2][5] = 4; x.a[2][6] = 1; x.a[3][3] = 1; x.a[3][4] = 3;
x.a[3][5] = 3; x.a[3][6] = 1; x.a[4][4] = 1; x.a[4][5] = 2; x.a[4][6] = 1;
x.a[5][5] = 1; x.a[5][6] = 1; x.a[6][6] = 1;
int T; cin >> T;
while(T--){
LL n, a, b;
scanf("%I64d %I64d %I64d", &n, &a, &b);
if(1 == n) printf("%I64d\n", a);
else if(2 == n) printf("%I64d\n", b);
else{
Matrix res = quick_pow(x, n-2);
LL ans = 0;
ans = (ans + res.a[0][0] * b) % mod;
ans = (ans + res.a[0][1] * a) % mod;
ans = (ans + res.a[0][2] * 16) % mod;
ans = (ans + res.a[0][3] * 8) % mod;
ans = (ans + res.a[0][4] * 4) % mod;
ans = (ans + res.a[0][5] * 2) % mod;
ans = (ans + res.a[0][6]) % mod;
printf("%I64d\n", ans);
}
}
return 0;
}
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 ...
- 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) ...
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
- HDU5950 Recursive sequence (矩阵快速幂)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 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] 类似于斐波那契数列的递推式子吧, 但 ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
随机推荐
- msp430项目编程02
msp430中项目---液晶1602显示 1.液晶1602工作原理 2.电路原理说明 3.代码(静态显示) 4.代码(动态显示) 5.项目总结 msp430项目编程 msp430入门学习
- gitlab上fork的项目如何获取源更新
1.添加上游项目地址 git remote add upstream URL 2.查看远程仓库信息 可以看到上游项目地址已经添加进来了 git remote -v 3.获取上游项目更新 获取到的更新会 ...
- React学习及实例开发(一)——开始
本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.构建一个新项目 1.命令行运行如下命令,构建一个新的react项目 npm install -g crea ...
- Spring的AOP AspectJ切入点语法详解(转)
一.Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的,在Spring AOP中目前只有执行方法这一个连接点,Spring AOP支持的AspectJ切入点指示 ...
- Org-mode五分钟教程ZZZ
Table of Contents 1 源起 2 简介 2.1 获取 org-mode 2.2 安装 3 基础用法 3.1 创建一个新文件 3.2 简单的任务列表 3.3 使用标题组织一篇文章 3.4 ...
- Meteor在手机上运行
在本章中,我们将学习如何在Android设备上运行你的应用程序.最近Meteor刚刚添加此功能适用于Windows操作系统,所以我们需要更新 Meteor 应用到 1.3测试版. 注 在写的时候本教程 ...
- 【APUE】一个fork的面试题及字符设备、块设备的区别
具体内容见:http://coolshell.cn/articles/7965.html 字符设备.块设备主要区别是:在对字符设备发出读/写请求时,实际的硬件I/O一般就紧接着发生了,而块设备则不然, ...
- jquery在ajax新加入的元素后绑定事件click
使用YII在做一个点击小图.能够在弹出窗体中显示大图的功能的时候,发现.GridView首页面的列表项按点击时一切正常,但按下了下一页后. 再点击小图,就不起作用了.原来,这是GridView使用了a ...
- hdu4941 Magical Forest
Problem Description There is a forest can be seen as N * M grid. In this forest, there is some magic ...
- Spark调研笔记第3篇 - Spark集群相应用的调度策略简单介绍
Spark集群的调度分应用间调度和应用内调度两种情况,下文分别进行说明. 1. 应用间调度 1) 调度策略1: 资源静态分区 资源静态分区是指整个集群的资源被预先划分为多个partitions,资源分 ...