题意:递推公式 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. 【HDOJ6322】Euler Function(数论)

    题意: 思路: #include <stdio.h> #include <vector> #include <algorithm> #include <str ...

  2. ASP.NET程序开发中经典常用的三十三种代码实例[确实有用]

    原文发布时间为:2008-11-10 -- 来源于本人的百度文章 [由搬家工具导入] ASP.NET程序开发中经典常用的三十三种代码实例:1. 打开新的窗口并传送参数: 传送参数:response.w ...

  3. Object_c tabbar菜单栏在切换的时候,颜色变灰的问题

    在界面切换的时候,有时候tabbar整条颜色都会变灰,如下: 而正常的应该如下: 在所有的父类加上: self.navigationController.navigationBar.transluce ...

  4. 牛客网暑期ACM多校训练营(第六场)G

    https://www.nowcoder.com/acm/contest/144/G 链接:https://www.nowcoder.com/acm/contest/144/G来源:牛客网 In Vi ...

  5. POJ 1511 【heap+dij】

    题意: t组样例. 每组有n个节点,有m条单向边. 有m组输入,每组a b c 表示从a到b的单向边的权值是c. 求解,从编号为1的节点出发,有n-1个人,要求他们分别到达编号从2到n的节点再返回,所 ...

  6. 寒武纪camp Day5

    补题进度:6/10 A(状压dp) 题意: 有n个数字1,2,...,n,有m个限制(a,b),表示至少要有一个数字a排在数字b的前面 你需要构造出一个含有数字1~n的序列,数字可以重复多次,要求该序 ...

  7. 全新开始fighting

    a.python准备工作 Python种类: JPython   IronPython    JavaScriptPython   RubyPython   CPython    ********** ...

  8. Excel小tips - 如何设置表格输入数字后末尾自动添加%

    选中一列——鼠标右键——设置单元格格式——数字——自定义——0% 按照以上操作完成后,点击确定,就大功告成了.

  9. poj 1695 Magazine Delivery 记忆化搜索

    dp[a][b][c],表示三个人从小到大依次在a,b.c位置时.距离结束最少的时间. 每次选一个人走到c+1位置搜索就好了. 坑点在于不能floyd.预计题目没说清楚.意思就是假设没送Li,那么Li ...

  10. VB6 如何连接MYSQL数据库

    1 从官网下载MYSQL的ODBC,选择与自己操作系统对应的版本(前提是你安装了MYSQL) http://dev.mysql.com/downloads/connector/odbc/   2 安装 ...