题意:递推公式 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 (矩阵快速幂)的更多相关文章

  1. 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 ...

  2. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  4. HDU5950 Recursive sequence —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others)   ...

  5. CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

    题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ ...

  6. HDU5950 Recursive sequence (矩阵快速幂)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  7. 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] 类似于斐波那契数列的递推式子吧, 但 ...

  8. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  9. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

随机推荐

  1. Codeforces917D. Stranger Trees

    $n \leq 100$的完全图,对每个$0 \leq K \leq n-1$问生成树中与给定的一棵树有$K$条公共边的有多少个,答案$mod \ \ 1e9+7$. 对这种“在整体中求具有某些特性的 ...

  2. Memcached 管理与监控工具 MemAdmin

    MemAdmin是一款可视化的Memcached管理与监控工具,基于 PHP5 & JQuery 开发,体积小,操作简单. 主要功能: 服务器参数监控:STATS.SETTINGS.ITEMS ...

  3. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

  4. Java的vector可实现自动增长的数组

    Vector维克多提供了向量类(vector)以实现类似动态数组的功能. 首先,在Java中并没有指针这样的概念 ,但如果正确灵活地使用指针又确实可以大大提高程序的质量.比如在c,c++中所谓的“动态 ...

  5. 使用微软的 ilasm 和 ildasm 对. net程序进行编译和反编译

    为了保证示例的完整性,请先准备好一个 c#写的 exe 程序,或者可以使用我提供的 exe 程序也可以(很简单,为了测试这里仅生成了一个带按钮的 winform,单击按钮提示弹窗) Test WinF ...

  6. 系统性能不够原因可能是cpu不够,内存不够等等

    1.Linux系统可以通过top命令查看系统的CPU.内存.运行时间.交换分区.执行的线程等信息. 通过top命令可以有效的发现系统的缺陷出在哪里.是内存不够.CPU处理能力不够.IO读写过高. 2. ...

  7. 如何在 Linux 环境下配置 Nagios Remote Plugin Executor (NRPE)

    为 NRPE 配置自定义命令 远程服务器上安装 下面列出了一些可以用于 NRPE 的自定义命令.这些命令在远程服务器的 /etc/nagios/nrpe.cfg 文件中定义. ## 当 1.5.15 ...

  8. 蚂蜂窝VS穷游最世界-自由行类App分析

    很多其它内容请关注博客: http://www.china10s.com/blog/? p=150 一.产品概述 体验环境: 机型:iPhone 6 型号:64G版 系统:iOS9.2 蚂蜂窝APP版 ...

  9. Angular2.x-主/细节组件

    此刻,HeroesComponent显示heroes列表和所选heroes的详细信息. 随着应用程序的增长保持一个组件中的所有功能将不可维护.您需要将大型组件分成更小的子组件,每个组件都专注于特定的任 ...

  10. hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)

    链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...